使用C语言实现vector动态数组的实例分享

时间:2021-05-02

下面是做项目时实现的一个动态数组,先后加入了好几个之后的项目,下面晒下代码。

头文件:

? 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 # ifndef __CVECTOR_H__ # define __CVECTOR_H__ # define MIN_LEN 256 # define CVEFAILED -1 # define CVESUCCESS 0 # define CVEPUSHBACK 1 # define CVEPOPBACK 2 # define CVEINSERT 3 # define CVERM 4 # define EXPANED_VAL 1 # define REDUSED_VAL 2 typedef void *citerator; typedef struct _cvector *cvector; # ifdef _cplusplus # define EXTERN_ extern "C" # else # define EXTERN_ extern # endif EXTERN_ cvector cvector_create (const size_t size ); EXTERN_ void cvector_destroy (const cvector cv ); EXTERN_ size_t cvector_length (const cvector cv ); EXTERN_ int cvector_pushback (const cvector cv, void *memb ); EXTERN_ int cvector_popback (const cvector cv, void *memb ); EXTERN_ size_t cvector_iter_at (const cvector cv, citerator iter ); EXTERN_ int cvector_iter_val (const cvector cv, citerator iter, void *memb); EXTERN_ citerator cvector_begin (const cvector cv ); EXTERN_ citerator cvector_end (const cvector cv ); EXTERN_ citerator cvector_next (const cvector cv, citerator iter ); EXTERN_ int cvector_val_at (const cvector cv, size_t index, void *memb ); EXTERN_ int cvector_insert (const cvector cv, citerator iter, void *memb); EXTERN_ int cvector_insert_at(const cvector cv, size_t index, void *memb ); EXTERN_ int cvector_rm (const cvector cv, citerator iter ); EXTERN_ int cvector_rm_at (const cvector cv, size_t index ); EXTERN_ void cv_info (const cvector cv ); EXTERN_ void cv_print (const cvector cv ); #endif


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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 # include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h> # define MIN_LEN 256 # define CVEFAILED -1 # define CVESUCCESS 0 # define CVEPUSHBACK 1 # define CVEPOPBACK 2 # define CVEINSERT 3 # define CVERM 4 # define EXPANED_VAL 1 # define REDUSED_VAL 2 typedef void *citerator; typedef struct _cvector { void *cv_pdata; size_t cv_len, cv_tot_len, cv_size; } *cvector; # define CWARNING_ITER(cv, iter, file, func, line) \ do {\ if ((cvector_begin(cv) > iter) || (cvector_end(cv) <= iter)) {\ fprintf(stderr, "var(" #iter ") warng out of range, "\ "at file:%s func:%s line:%d!!/n", file, func, line);\ return CVEFAILED;\ }\ } while (0) # ifdef _cplusplus # define EXTERN_ extern "C" # else # define EXTERN_ extern # endif EXTERN_ cvector cvector_create (const size_t size ); EXTERN_ void cvector_destroy (const cvector cv ); EXTERN_ size_t cvector_length (const cvector cv ); EXTERN_ int cvector_pushback (const cvector cv, void *memb ); EXTERN_ int cvector_popback (const cvector cv, void *memb ); EXTERN_ size_t cvector_iter_at (const cvector cv, citerator iter ); EXTERN_ int cvector_iter_val (const cvector cv, citerator iter, void *memb); EXTERN_ citerator cvector_begin (const cvector cv ); EXTERN_ citerator cvector_end (const cvector cv ); EXTERN_ citerator cvector_next (const cvector cv, citerator iter ); EXTERN_ int cvector_val_at (const cvector cv, size_t index, void *memb ); EXTERN_ int cvector_insert (const cvector cv, citerator iter, void *memb); EXTERN_ int cvector_insert_at(const cvector cv, size_t index, void *memb ); EXTERN_ int cvector_rm (const cvector cv, citerator iter ); EXTERN_ int cvector_rm_at (const cvector cv, size_t index ); EXTERN_ void cv_info (const cvector cv ); EXTERN_ void cv_print (const cvector cv ); cvector cvector_create(const size_t size) { cvector cv = (cvector)malloc(sizeof (struct _cvector)); if (!cv) return NULL; cv->cv_pdata = malloc(MIN_LEN * size); if (!cv->cv_pdata) { free(cv); return NULL; } cv->cv_size = size; cv->cv_tot_len = MIN_LEN; cv->cv_len = 0; return cv; } void cvector_destroy(const cvector cv) { free(cv->cv_pdata); free(cv); return; } size_t cvector_length(const cvector cv) { return cv->cv_len; } int cvector_pushback(const cvector cv, void *memb) { if (cv->cv_len >= cv->cv_tot_len) { void *pd_sav = cv->cv_pdata; cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); if (!cv->cv_pdata) { cv->cv_pdata = pd_sav; cv->cv_tot_len >>= EXPANED_VAL; return CVEPUSHBACK; } } memcpy(cv->cv_pdata + cv->cv_len * cv->cv_size, memb, cv->cv_size); cv->cv_len++; return CVESUCCESS; } int cvector_popback(const cvector cv, void *memb) { if (cv->cv_len <= 0) return CVEPOPBACK; cv->cv_len--; memcpy(memb, cv->cv_pdata + cv->cv_len * cv->cv_size, cv->cv_size); if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL)) && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL))) { void *pd_sav = cv->cv_pdata; cv->cv_tot_len >>= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); if (!cv->cv_pdata) { cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = pd_sav; return CVEPOPBACK; } } return CVESUCCESS; } size_t cvector_iter_at(const cvector cv, citerator iter) { CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); return (iter - cv->cv_pdata) / cv->cv_size; } int cvector_iter_val(const cvector cv, citerator iter, void *memb) { CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); memcpy(memb, iter, cv->cv_size); return 0; } citerator cvector_begin(const cvector cv) { return cv->cv_pdata; } citerator cvector_end(const cvector cv) { return cv->cv_pdata + (cv->cv_size * cv->cv_len); } static inline void cvmemove_foreward(const cvector cv, void *from, void *to) { size_t size = cv->cv_size; void *p; for (p = to; p >= from; p -= size) memcpy(p + size, p, size); return; } static inline void cvmemove_backward(const cvector cv, void *from, void *to) { memcpy(from, from + cv->cv_size, to - from); return; } int cvector_insert(const cvector cv, citerator iter, void *memb) { CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); if (cv->cv_len >= cv->cv_tot_len) { void *pd_sav = cv->cv_pdata; cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); if (!cv->cv_pdata) { cv->cv_pdata = pd_sav; cv->cv_tot_len >>= EXPANED_VAL; return CVEINSERT; } } cvmemove_foreward(cv, iter, cv->cv_pdata + cv->cv_len * cv->cv_size); memcpy(iter, memb, cv->cv_size); cv->cv_len++; return CVESUCCESS; } int cvector_insert_at(const cvector cv, size_t index, void *memb) { citerator iter; if (index >= cv->cv_tot_len) { cv->cv_len = index + 1; while (cv->cv_len >= cv->cv_tot_len) cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); iter = cv->cv_pdata + cv->cv_size * index; memcpy(iter, memb, cv->cv_size); } else { iter = cv->cv_pdata + cv->cv_size * index; cvector_insert(cv, iter, memb); } return 0; } citerator cvector_next(const cvector cv, citerator iter) { return iter + cv->cv_size; } int cvector_val(const cvector cv, citerator iter, void *memb) { memcpy(memb, iter, cv->cv_size); return 0; } int cvector_val_at(const cvector cv, size_t index, void *memb) { memcpy(memb, cv->cv_pdata + index * cv->cv_size, cv->cv_size); return 0; } int cvector_rm(const cvector cv, citerator iter) { citerator from; citerator end; CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); from = iter; end = cvector_end(cv); memcpy(from, from + cv->cv_size, end - from); cv->cv_len--; if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL)) && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL))) { void *pd_sav = cv->cv_pdata; cv->cv_tot_len >>= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); if (!cv->cv_pdata) { cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = pd_sav; return CVERM; } } return CVESUCCESS; } int cvector_rm_at(const cvector cv, size_t index) { citerator iter; iter = cv->cv_pdata + cv->cv_size * index; CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); return cvector_rm(cv, iter); } void cv_info(const cvector cv) { printf("/n/ntot :%s : %d/n", __func__, cv->cv_tot_len); printf("len :%s : %d/n", __func__, cv->cv_len); printf("size:%s : %d/n/n", __func__, cv->cv_size); return; } void cv_print(const cvector cv) { int num; citerator iter; if (cvector_length(cv) == 0) fprintf(stderr, "file:%s func:%s line:%d error, null length cvector!!/n", __FILE__, __func__, __LINE__); for (iter = cvector_begin(cv); iter != cvector_end(cv); iter = cvector_next(cv, iter)) { cvector_iter_val(cv, iter, &num); printf("var:%d at:%d/n", num, cvector_iter_at(cv, iter)); } return; }

改进版
上面那份代码是在Linux下写的,如果是在Windows的Visul C++环境下编译似乎会出些问题,所以特别做了个改进版:
下面是更新后的代码:

cvector.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 45 46 47 48 49 # ifndef __CVECTOR_H__ # define __CVECTOR_H__ # include <stdio.h> # include <stdlib.h> # include <string.h> # define MIN_LEN 256 # define CVEFAILED -1 # define CVESUCCESS 0 # define CVEPUSHBACK 1 # define CVEPOPBACK 2 # define CVEINSERT 3 # define CVERM 4 # define EXPANED_VAL 1 # define REDUSED_VAL 2 typedef void *citerator; typedef struct _cvector *cvector; # ifdef __cplusplus extern "C" { # endif cvector cvector_create (const size_t size ); void cvector_destroy (const cvector cv ); size_t cvector_length (const cvector cv ); int cvector_pushback (const cvector cv, void *memb ); int cvector_popback (const cvector cv, void *memb ); size_t cvector_iter_at (const cvector cv, citerator iter ); int cvector_iter_val (const cvector cv, citerator iter, void *memb); citerator cvector_begin (const cvector cv ); citerator cvector_end (const cvector cv ); citerator cvector_next (const cvector cv, citerator iter ); int cvector_val_at (const cvector cv, size_t index, void *memb ); int cvector_insert (const cvector cv, citerator iter, void *memb); int cvector_insert_at(const cvector cv, size_t index, void *memb ); int cvector_rm (const cvector cv, citerator iter ); int cvector_rm_at (const cvector cv, size_t index ); void cv_info (const cvector cv ); void cv_print (const cvector cv ); # ifdef __cplusplus } # endif #endif

cvector.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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 #include "cvector.h" #ifndef __gnu_linux__ #define __func__ "unknown" #define inline __forceinline #endif # define CWARNING_ITER(cv, iter, file, func, line) \ do {\ if ((cvector_begin(cv) > iter) || (cvector_end(cv) <= iter)) {\ fprintf(stderr, "var(" #iter ") warng out of range, "\ "at file:%s func:%s line:%d!!\n", file, func, line);\ return CVEFAILED;\ }\ } while (0) struct _cvector { void *cv_pdata; size_t cv_len, cv_tot_len, cv_size; }; cvector cvector_create(const size_t size) { cvector cv = (cvector)malloc(sizeof (struct _cvector)); if (!cv) return NULL; cv->cv_pdata = malloc(MIN_LEN * size); if (!cv->cv_pdata) { free(cv); return NULL; } cv->cv_size = size; cv->cv_tot_len = MIN_LEN; cv->cv_len = 0; return cv; } void cvector_destroy(const cvector cv) { free(cv->cv_pdata); free(cv); return; } size_t cvector_length(const cvector cv) { return cv->cv_len; } int cvector_pushback(const cvector cv, void *memb) { if (cv->cv_len >= cv->cv_tot_len) { void *pd_sav = cv->cv_pdata; cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); if (!cv->cv_pdata) { cv->cv_pdata = pd_sav; cv->cv_tot_len >>= EXPANED_VAL; return CVEPUSHBACK; } } memcpy((char *)cv->cv_pdata + cv->cv_len * cv->cv_size, memb, cv->cv_size); cv->cv_len++; return CVESUCCESS; } int cvector_popback(const cvector cv, void *memb) { if (cv->cv_len <= 0) return CVEPOPBACK; cv->cv_len--; memcpy(memb, (char *)cv->cv_pdata + cv->cv_len * cv->cv_size, cv->cv_size); if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL)) && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL))) { void *pd_sav = cv->cv_pdata; cv->cv_tot_len >>= EXPANED_VAL; cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); if (!cv->cv_pdata) { cv->cv_tot_len <<= EXPANED_VAL; cv->cv_pdata = pd_sav; return CVEPOPBACK; } } return CVESUCCESS; } size_t cvector_iter_at(const cvector cv, citerator iter) { CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); return ((char *)iter - (char *)cv->cv_pdata) / cv->cv_size; } int cvector_iter_val(const cvector cv, citerator iter, void *memb)

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章