ArrayList.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. #include "ArrayList.h"
  2. #include "ArrayList.h"
  3. void *new_byte(int size){
  4. void *mem = malloc(size);
  5. if(mem != NULL){
  6. memset(mem,NULL,size);
  7. return mem;
  8. }
  9. return NULL;
  10. }
  11. /****************************************************************
  12. func: new_array_list
  13. note: 创建一个new_array_list对象,并返回
  14. @head: 集合
  15. @value: value
  16. @size: value的长度
  17. return: 返回创建的 ArrayList
  18. ****************************************************************/
  19. ArrayList *new_array_list(ArrayList *head,void *value,int size){
  20. ArrayList *obj = new_object(ArrayList);
  21. if(obj != NULL){
  22. reset_object(ArrayList,obj);
  23. obj->sValue = size;
  24. obj->value = new_byte(size);
  25. if(obj->value != NULL){
  26. if(value != NULL){
  27. memcpy(obj->value,value,size);
  28. }
  29. return obj;
  30. }
  31. free(obj);
  32. }
  33. return NULL;
  34. }
  35. /****************************************************************
  36. func: add_array_list
  37. note: 添加value
  38. @head: 集合
  39. @value: value
  40. @size: value的长度
  41. return: 返回head
  42. ****************************************************************/
  43. ArrayList *add_array_list(ArrayList *head,void *value,int size){
  44. ArrayList *obj = head;
  45. if(head == NULL){
  46. return new_array_list(NULL,value,size);
  47. }else{
  48. obj = get_array_list_last(head);
  49. obj->next = new_array_list(NULL,value,size);
  50. return head;
  51. }
  52. }
  53. /****************************************************************
  54. func: put_string_map
  55. note: 修改指定的key的值
  56. @head: 集合
  57. @key: key
  58. @value: value
  59. return: 返回head
  60. ****************************************************************/
  61. ArrayList *put_array_list(ArrayList *head,int indexof,void *value,int size){
  62. ArrayList *obj = get_array_list(head,indexof);
  63. if(obj != NULL){
  64. free(obj->value);
  65. obj->sValue = size;
  66. obj->value = new_byte(size);
  67. if(value != NULL){
  68. memcpy(obj->value,value,size);
  69. }
  70. return head;
  71. }
  72. return NULL;
  73. }
  74. /****************************************************************
  75. func: get_array_list
  76. note: 获得indexof位置的对象
  77. @head: 集合
  78. @indexof: 坐标位置
  79. return: 返回获得indexof的对象,如果indexof不存在返回NULL
  80. ****************************************************************/
  81. ArrayList *get_array_list(ArrayList *head,int indexof){
  82. ArrayList *obj = head;
  83. test_object(head);
  84. for(int i=0;obj != NULL;i++){
  85. if(i == indexof){
  86. return obj;
  87. }
  88. obj = obj->next;
  89. }
  90. return NULL;
  91. }
  92. /****************************************************************
  93. func: get_array_list_previous
  94. note: 获得indexof的上一个对象
  95. @head: 集合
  96. @indexof: indexof
  97. return: 返回获得indexof的上一个对象,如果indexof不存在返回NULL
  98. ****************************************************************/
  99. ArrayList *get_array_list_previous(ArrayList *head,int indexof){
  100. ArrayList *obj = head;
  101. ArrayList *last = NULL;
  102. test_object(head);
  103. for(int i=0;obj != NULL;i++){
  104. if(i == indexof){
  105. return last;
  106. }
  107. last = obj;
  108. obj = obj->next;
  109. }
  110. return NULL;
  111. }
  112. /****************************************************************
  113. func: get_array_list_last
  114. note: 获得集合最后的对象
  115. @head: 集合
  116. return: 返回获得集合最后的对象
  117. ****************************************************************/
  118. ArrayList *get_array_list_last(ArrayList *head){
  119. ArrayList *obj = head;
  120. test_object(head);
  121. while(obj->next != NULL){
  122. obj = obj->next;
  123. }
  124. return obj;
  125. }
  126. /****************************************************************
  127. func: get_array_list_size
  128. note: 获得集合的对象数量
  129. @head: 集合
  130. return: 返回集合的对象数量
  131. ****************************************************************/
  132. int get_array_list_size(ArrayList *head){
  133. int i = 0;
  134. ArrayList *obj = head;
  135. test_object(head);
  136. for(i=0;obj != NULL;i++){
  137. obj = obj->next;
  138. }
  139. return i;
  140. }
  141. /****************************************************************
  142. func: remove_string_map
  143. note: 移除集合中的对象
  144. @head: 集合
  145. return: 移除集合中的对象后一个StringMap对象
  146. ****************************************************************/
  147. ArrayList *remove_array_list(ArrayList *head){
  148. ArrayList *obj = head->next;
  149. test_object(head);
  150. free(head->value);
  151. free(head);
  152. return obj;
  153. }
  154. /****************************************************************
  155. func: close_array_list
  156. note: 移除所有集合中的对象
  157. @head: 集合
  158. return: NULL
  159. ****************************************************************/
  160. ArrayList *close_array_list(ArrayList *head){
  161. ArrayList *obj = head;
  162. test_object(head);
  163. for(int i=0;obj != NULL;i++){
  164. obj = remove_array_list(obj);
  165. }
  166. return NULL;
  167. }
  168. /****************************************************************
  169. func: remove_array_list_index
  170. note: 移除坐标的对象
  171. @head: 集合
  172. @index: 坐标
  173. return: 返回移除后的集合,如果没有返回NULL
  174. ****************************************************************/
  175. ArrayList *remove_array_list_index(ArrayList *head,int index){
  176. ArrayList *obj = NULL;
  177. ArrayList *previous = NULL;;
  178. test_object(head);
  179. if(index < get_array_list_size(head)){
  180. previous = get_array_list_previous(head,index);
  181. if(previous == NULL || index == 0){
  182. return remove_array_list(head);
  183. }else{
  184. obj = get_array_list(head,index);
  185. previous->next = remove_array_list(obj);
  186. return head;
  187. }
  188. }
  189. return NULL;
  190. }
  191. /****************************************************************
  192. func: get_array_list_memsize
  193. note: 获得集合字符串内存块大小
  194. @head: 集合
  195. return: 字符串的内存块大小
  196. ****************************************************************/
  197. int get_array_list_memsize(ArrayList *head){
  198. int memsize = 0;
  199. ArrayList *obj = head;
  200. for(int i=0;obj != NULL;i++){
  201. memsize += obj->sValue;
  202. obj = obj->next;
  203. }
  204. return memsize;
  205. }
  206. /****************************************************************
  207. func: get_array_list_disksize
  208. note: 获得当前对象占用磁盘空间大小
  209. @head: 集合
  210. return: 占用磁盘空间大小
  211. ****************************************************************/
  212. int get_array_list_disksize(ArrayList *head){
  213. int memsize = get_array_list_memsize(head);
  214. int objsize = get_array_list_size(head) * (sizeof(int)*1);
  215. int sum = sizeof(int) + objsize + memsize;
  216. return sum;
  217. }
  218. uint8_t *write_buff_array_list(ArrayList *head,uint8_t *mBuf){
  219. if(mBuf == NULL) return NULL;
  220. memcpy(mBuf,&head->sValue,sizeof(int));
  221. mBuf = mBuf + sizeof(int);
  222. memcpy(mBuf,head->value,head->sValue);
  223. mBuf = mBuf + head->sValue;
  224. return mBuf;
  225. }
  226. uint8_t *read_buff_array_list(ArrayList *head,uint8_t *mBuf){
  227. if(mBuf == NULL) return NULL;
  228. memcpy(&head->sValue,mBuf,sizeof(int));
  229. mBuf = mBuf + sizeof(int);
  230. head->value = new_byte(head->sValue);
  231. if(head->value != NULL ){
  232. memcpy(head->value,mBuf,head->sValue);
  233. mBuf = mBuf + head->sValue;
  234. return mBuf;
  235. }
  236. return NULL;
  237. }
  238. /****************************************************************
  239. func: pack_array_list
  240. note: 打包集合数据,打包方式
  241. int(总的长度) + ( (int)sValue + (data)value ...
  242. @head: 集合
  243. return: 返回打包好的PackMap数据对象,失败返回NULL
  244. ****************************************************************/
  245. PackMap *pack_array_list(ArrayList *head){
  246. PackMap *pack = NULL;
  247. ArrayList *obj = head;
  248. int sum = get_array_list_disksize(head);
  249. uint8_t *buf = new_byte(sum);
  250. uint8_t *mBuf = buf;
  251. if(buf != NULL){
  252. memset(buf,0,sum);
  253. memcpy(mBuf,&sum,sizeof(int));
  254. mBuf = mBuf + sizeof(int);
  255. while(obj != NULL){
  256. mBuf = write_buff_array_list(obj,mBuf);
  257. obj = obj->next;
  258. }
  259. pack = new_object(PackMap);
  260. pack->sum = sum;
  261. pack->buff = buf;
  262. return pack;
  263. }
  264. return NULL;
  265. }
  266. /****************************************************************
  267. func: unpack_array_list
  268. note: 解包ArrayList集合数据
  269. @pack: PackMap对象
  270. return: 返回ArrayList集合,失败返回NULL
  271. ****************************************************************/
  272. ArrayList *unpack_array_list(PackMap *pack){
  273. ArrayList *head = NULL;
  274. ArrayList *obj = NULL;
  275. ArrayList *temp = NULL;
  276. uint8_t *mBuf = NULL;
  277. int sum = 0;
  278. if(pack != NULL){
  279. mBuf = pack->buff;
  280. memcpy(&sum,mBuf,sizeof(int));
  281. mBuf = mBuf + sizeof(int);
  282. head = new_object(ArrayList);
  283. mBuf = read_buff_array_list(head,mBuf);
  284. obj = head;
  285. if(pack->sum == sum){
  286. while((pack->buff + sum) > mBuf && mBuf != NULL){
  287. temp = new_object(ArrayList);
  288. mBuf = read_buff_array_list(temp,mBuf);
  289. obj->next = temp;
  290. obj->next->next = NULL;
  291. obj = obj->next;
  292. }
  293. return head;
  294. }
  295. }
  296. return NULL;
  297. }
  298. #if 1
  299. void test_ArrayList(void){
  300. uint8_t buf[50];
  301. ArrayList *temp;
  302. ArrayList *object = new_array_list(NULL,"0123456789",10);
  303. printf("\rArrayList --> test\r");
  304. add_array_list(object,"A123456\0",8);
  305. add_array_list(object,"B123456\0",8);
  306. add_array_list(object,"C123456\0",8);
  307. add_array_list(object,"D123456\0",8);
  308. #if 1
  309. //string 类型
  310. add_array_list(object,"123",3);
  311. temp = get_array_list(object,1);
  312. char *stp = temp->value;
  313. printf("0x%x 0x%x 0x%x\r",stp[0],stp[1],stp[2]);
  314. //int 类型
  315. int in = 1024;
  316. add_array_list(object,&in,sizeof(int));
  317. int *inp = get_array_list(object,2)->value;
  318. printf("int type p:%d\r",*inp);
  319. //double 类型
  320. double dou = 1024.5555;
  321. add_array_list(object,&dou,sizeof(double));
  322. double *pdo = get_array_list(object,3)->value;
  323. printf("double type p:%f\r",*pdo);
  324. #endif
  325. /* for(int i=0;i<1000;i++){
  326. snprintf(buf,50,"Guihui->%d \0",i);
  327. add_array_list(object,buf,strlen(buf));
  328. }*/
  329. PackMap *pack = pack_array_list(object);
  330. if(pack != NULL){
  331. printf("ArrayList PackMap Test\r");
  332. object = close_array_list(object);
  333. for(int i=0; i<pack->sum; i++){
  334. printf("%x ",pack->buff[i]);
  335. }
  336. printf("\r");
  337. /*解包*/
  338. object = unpack_array_list(pack);
  339. }
  340. temp = remove_array_list_index(object,0);
  341. if(temp == NULL){
  342. printf("ERROR remove index\r");
  343. }else{
  344. object = temp;
  345. }
  346. temp = remove_array_list_index(object,3);
  347. if(temp == NULL){
  348. printf("ERROR remove index\r");
  349. }else{
  350. object = temp;
  351. }
  352. //object = remove_array_list_index(object,3);
  353. temp = object;
  354. for(int i=0; temp != NULL; i++){
  355. snprintf(buf,temp->sValue,"%s",temp->value);
  356. printf("%0.2d -> %s\r",i,buf);
  357. temp = temp->next;
  358. }
  359. close_array_list(object);
  360. }
  361. #endif