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
|
#include "darray.h"
dynamic_arr darr_initiate(int unitsize) { dynamic_arr arr; arr.unitsize_ = unitsize; arr.listsize_ = 0; arr.data_ = NULL; return arr; }
void darr_expand(dynamic_arr_ptr arr_p, int extra) { if(arr_p->listsize_ == 0) { arr_p->data_ = (void *)malloc(extra * arr_p->unitsize_); arr_p->listsize_ += extra; return; } int cur_listsize = arr_p->listsize_; void* dst = (void *)malloc((cur_listsize + extra) * arr_p->unitsize_); for(uint8_t* p = (uint8_t*)arr_p->data_, *pdst = (uint8_t*)dst; p < (uint8_t*)arr_p->data_+cur_listsize*arr_p->unitsize_+1; p++, pdst++) { memcpy(pdst, p, 1); } arr_p->listsize_ += extra; free(arr_p->data_); arr_p->data_ = dst; dst = NULL; }
void darr_push(dynamic_arr_ptr arr_p, const void* value) { darr_expand(arr_p, 1); memcpy((uint8_t*)arr_p->data_+(arr_p->listsize_-1)*arr_p->unitsize_, (uint8_t*)value, arr_p->unitsize_); }
bool darr_insert(dynamic_arr_ptr arr_p, int i, const void* value) { if(i < 0 || i > arr_p->listsize_) return false; if(i == arr_p->listsize_) { darr_push(arr_p, value); return true; } else { darr_expand(arr_p, 1); for(int j = arr_p->listsize_-1; j >= i; j--) memcpy((uint8_t*)arr_p->data_ + j*arr_p->unitsize_, (uint8_t*)arr_p->data_ + (j-1)*arr_p->unitsize_, arr_p->unitsize_); memcpy((uint8_t*)arr_p->data_ + i*arr_p->unitsize_, (uint8_t*)value, arr_p->unitsize_); return true; } }
bool darr_erase(dynamic_arr_ptr arr_p, int i, void* element) { if(i < 0) return false; else { if(element != NULL) memcpy(element, arr_p->data_ + i, arr_p->unitsize_); if(i < arr_p->listsize_ - 1) for(int j = i; j < arr_p->listsize_; j++) memcpy(arr_p->data_+j*arr_p->unitsize_, arr_p->data_+(j+1)*arr_p->unitsize_, arr_p->unitsize_); arr_p->listsize_ --; return true; } }
bool darr_get(const dynamic_arr_ptr arr_p, int i, void* element) { if(i < 0 || i > arr_p->listsize_ - 1) return false; else { memcpy(element, (uint8_t*)arr_p->data_+i*arr_p->unitsize_, arr_p->unitsize_); return true; } }
bool darr_modify(dynamic_arr_ptr arr_p, int i, const void* value) { if(i < 0 || i > arr_p->listsize_ - 1) return false; else { memcpy(arr_p->data_+i*arr_p->unitsize_, value, arr_p->unitsize_); return true; } }
bool darr_isempty(const dynamic_arr_ptr arr_p) { return arr_p->listsize_ == 0; }
void darr_clear(dynamic_arr_ptr arr_p) { free(arr_p->data_); arr_p->data_ = NULL; arr_p->listsize_ = 0; arr_p->unitsize_=0; }
void darr_print(dynamic_arr_ptr arr_p, bool printsize) { if(printsize) { printf("(darr_print) Length of input arr is ------ %d units\n", arr_p->listsize_); printf("(darr_print) Unitsize of input arr is ---- %d bytes = %d bits\n", arr_p->unitsize_, arr_p->unitsize_*8); printf("(darr_print) Total size of input arr is -- %d bytes = %d bits\n", arr_p->listsize_*arr_p->unitsize_, (arr_p->listsize_*arr_p->unitsize_)*8); }
printf("(darr_print) Data of input arr is: "); for(uint8_t* p = (uint8_t*)arr_p->data_+arr_p->listsize_*arr_p->unitsize_-1; p > (uint8_t*)arr_p->data_-1; p--) { printf("%s%x", *p<16?"0":"", *p); if((p-(uint8_t*)arr_p->data_) % arr_p->unitsize_ == 0) printf(" "); } printf("\n"); }
|