本文主要简述了数组的特性,数组的操作,以及使用C语言实现对象数组。
以下为我使用的C语言的运行和编译环境:
System Version : Ubuntu22.04.1
Linux kernel Version : 5.19.0
GCC Version : 11.3.0
STDC Version : 201710L
摘要: 数组的访问,特性和操作,对象数组。
TODO 💤
1. 数组的性质
1.1. 定义
1.2. 特点
2. 数组的访问
3. 数组的特性和操作
4. 使用C语言创建对象数组
5. 源码
array_ds.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#ifndef _ARRAY_DS_H_ #define _ARRAY_DS_H_
#include "lang.h" #include "args.h" #include "typed.h"
EXTERN_C_BEGIN
typedef struct array_ds_t array_ds_t; struct array_ds_t { void (*assign)(size_t cnt, const void *data); void (*insert)(size_t index, size_t cnt, const void *data); void (*set)(size_t index, const void *elem); void *(*get1)(size_t index); void (*get2)(size_t index, void *elem); void (*get3)(size_t index, size_t cnt, void *elem); #ifndef get #define get(...) MACRO_CAT(get, __VA_ARGS__) #endif size_t (*capacity)(void); size_t (*elem_size)(void); void (*destory)(void); };
array_ds_t *the_array(array_ds_t *public); array_ds_t *array_ds_create(size_t capacity, size_t elem_size);
EXTERN_C_END
#endif
|
array_ds.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "array_ds.h"
typedef struct _array_ds_t _array_ds_t; struct _array_ds_t { array_ds_t public; size_t capacity; size_t elem_size; u8 *data; };
static __thread _array_ds_t *this = NULL;
static size_t array_elem_size(void) { return this->elem_size; }
static size_t array_capacity(void) { return this->capacity; }
static void array_assign(size_t cnt, const void *data) { if (cnt > this->capacity) { this->data = realloc(this->data, cnt * this->elem_size); this->capacity = cnt; }
memcpy(this->data, data, cnt * this->elem_size); }
static void array_insert(size_t index, size_t cnt, const void *data) { if (index > this->capacity) { this->data = realloc(this->data, (index + cnt) * this->elem_size); this->capacity = index + cnt; }
memcpy(&this->data[index * this->elem_size], data, cnt * this->elem_size); }
static void array_set(size_t index, const void *elem) { if (index >= this->capacity) { this->data = realloc(this->data, (index + 1) * this->elem_size); this->capacity = (index + 1); }
memcpy(&this->data[index * this->elem_size], elem, this->elem_size); }
static void *array_get1(size_t index) { if (index >= this->capacity) { return NULL; }
return &this->data[index * this->elem_size]; }
static void array_get2(size_t index, void *elem) { if (index >= this->capacity) { return; }
memcpy(elem, &this->data[index * this->elem_size], this->elem_size); }
static void array_get3(size_t index, size_t cnt, void *data) { if (index >= this->capacity || (index + cnt) >= this->capacity) { return; }
memcpy(data, &this->data[index * this->elem_size], cnt * this->elem_size); }
static void destory(void) { if(!this) return;
free(this); this = NULL;
return; }
array_ds_t *the_array(array_ds_t *public) { this = (_array_ds_t *)public;
return &this->public; }
array_ds_t *array_ds_create(size_t capacity, size_t elem_size) { _array_ds_t *_this = (_array_ds_t *)malloc(sizeof(_array_ds_t));
_this->public.elem_size= array_elem_size; _this->public.capacity = array_capacity; _this->public.assign = array_assign; _this->public.insert = array_insert; _this->public.set = array_set; _this->public.get1 = array_get1; _this->public.get2 = array_get2; _this->public.get3 = array_get3; _this->public.destory = destory; _this->data = malloc(capacity * elem_size); _this->capacity = capacity; _this->elem_size = elem_size;
return &_this->public; }
|