list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * (C) 2000 UNIVERSITY OF CHICAGO
  3. * See COPYRIGHT in top-level directory.
  4. */
  5. #ifdef SYSDARWIN
  6. #include <sys/malloc.h>
  7. #else
  8. #include <malloc.h>
  9. #endif
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include "listops.h"
  13. #include "listP.h"
  14. /*
  15. * list management code
  16. *
  17. * For storing singly-linked lists of pointers.
  18. *
  19. */
  20. static int itemcount=0;
  21. static int headcount=0;
  22. /*
  23. * AP_listitem_malloc()
  24. *
  25. * malloc a new ilist item and return a pointer to it.
  26. *
  27. */
  28. static pListitem AP_listitem_malloc(void)
  29. {
  30. pListitem item;
  31. itemcount++;
  32. item=(pListitem)malloc( (unsigned) sizeof(Listitem) );
  33. if (!item)
  34. {
  35. perror("AP_listitem_malloc: malloc failure");
  36. abort();
  37. }
  38. return(item);
  39. }
  40. /*
  41. * AP_listitem_free(listitem)
  42. *
  43. * Free a listitem generated by AP_listitem_malloc()
  44. *
  45. */
  46. static void AP_listitem_free(pListitem listitem)
  47. {
  48. free(listitem);
  49. itemcount--;
  50. }
  51. /*
  52. * AP_listitem_verify(void)
  53. *
  54. * Checks to see if there are any outstanding listitems that have been
  55. * malloc'd. Returns true if there are any.
  56. *
  57. */
  58. int AP_listitem_verify(void)
  59. {
  60. if (itemcount!=0)
  61. fprintf(stderr,"AP_list_verify: outstanding items, count=%d\n",
  62. itemcount);
  63. if (headcount!=0)
  64. fprintf(stderr,"AP_list_verify: outstanding lists, count=%d\n",
  65. headcount);
  66. return( (itemcount!=0) || (headcount!=0) );
  67. }
  68. pListitem AP_listitem_prev(pListitem listitem)
  69. {
  70. return(listitem->prev);
  71. }
  72. pListitem AP_listitem_next(pListitem listitem)
  73. {
  74. return(listitem->next);
  75. }
  76. void *AP_listitem_data(pListitem listitem)
  77. {
  78. return(listitem->data );
  79. }
  80. /***************************************************************/
  81. /*
  82. * AP_list_new(void)
  83. *
  84. * allocate an empty list return a pointer to it
  85. *
  86. */
  87. pList AP_list_new(void)
  88. {
  89. pList list;
  90. list=(pList)malloc(sizeof(List));
  91. if (!list)
  92. {
  93. perror("AP_list_new: malloc failure\n");
  94. abort();
  95. }
  96. list->head=NULL;
  97. list->tail=NULL;
  98. list->count=0;
  99. headcount++;
  100. return(list);
  101. }
  102. /*
  103. * AP_list_free(list)
  104. *
  105. * Free an entire list
  106. *
  107. */
  108. void AP_list_free(pList list)
  109. {
  110. pListitem next,cur;
  111. int count;
  112. count=0;
  113. cur=list->head;
  114. while(cur)
  115. {
  116. next=cur->next;
  117. AP_listitem_free(cur);
  118. count++;
  119. cur=next;
  120. }
  121. if (count!=list->count)
  122. {
  123. fprintf(stderr,"AP_list_free: count %d does not match actual length %d\n",
  124. list->count,count);
  125. abort();
  126. }
  127. headcount--;
  128. free(list);
  129. }
  130. /*
  131. * AP_list_size(list)
  132. *
  133. * return the number of items in an ilist
  134. *
  135. */
  136. int AP_list_size(pList list)
  137. {
  138. return(list->count);
  139. }
  140. /*
  141. * AP_list_prepend(list,data)
  142. *
  143. * Prepend item to the front of list.
  144. *
  145. */
  146. pListitem AP_list_prepend(pList list, void *data)
  147. {
  148. pListitem new;
  149. new=AP_listitem_malloc();
  150. new->data=data;
  151. new->prev=NULL;
  152. new->next=list->head;
  153. #ifdef CHECKS
  154. new->list=list;
  155. #endif
  156. if (list->head)
  157. list->head->prev=new;
  158. list->head=new;
  159. if (!list->tail)
  160. list->tail=new;
  161. (list->count)++;
  162. return(new);
  163. }
  164. /*
  165. * AP_list_append(list,data)
  166. *
  167. * append item to end of list
  168. *
  169. */
  170. pListitem AP_list_append(pList list, void *data)
  171. {
  172. pListitem new;
  173. new=AP_listitem_malloc();
  174. new->data=data;
  175. new->prev=list->tail;
  176. new->next= NULL;
  177. #ifdef CHECKS
  178. new->list= list;
  179. #endif
  180. if (list->tail)
  181. list->tail->next=new;
  182. else
  183. list->head=new;
  184. list->tail=new;
  185. (list->count)++;
  186. return(new);
  187. }
  188. /*
  189. * AP_list_delete(list,data)
  190. *
  191. * delete item from list; return TRUE if successful
  192. *
  193. */
  194. int AP_list_delete(pList list, void *data)
  195. {
  196. pListitem item;
  197. if (item=AP_list_search(list,data))
  198. {
  199. AP_list_delete_item(list,item);
  200. return(1);
  201. }
  202. return(0);
  203. }
  204. void AP_list_delete_item(pList list, pListitem item)
  205. {
  206. #ifdef CHECKS
  207. if (item->list != list)
  208. {
  209. fprintf(stderr,"AP_list_delete_item: item is not in list\n");
  210. abort();
  211. }
  212. #endif
  213. /* set pointer of prior listitem */
  214. if (item == list->head)
  215. list->head = item->next;
  216. else
  217. item->prev->next = item->next;
  218. /* set pointer of following listitem */
  219. if (item == list->tail)
  220. list->tail = item->prev;
  221. else
  222. item->next->prev = item->prev;
  223. AP_listitem_free(item);
  224. (list->count)--;
  225. }
  226. pListitem AP_list_head_item(pList list)
  227. {
  228. return(list->head);
  229. }
  230. int AP_list_head(pList list, void **data)
  231. {
  232. if (list->head)
  233. {
  234. *data=list->head->data;
  235. return(1);
  236. }
  237. else
  238. return(0);
  239. }
  240. int AP_list_tail(pList list, void **data)
  241. {
  242. if (list->tail)
  243. {
  244. *data=list->tail->data;
  245. return(1);
  246. }
  247. else
  248. return(0);
  249. }
  250. /*
  251. * AP_list_print(str,list)
  252. *
  253. * Print out the message string followed by the
  254. * items in the list
  255. *
  256. */
  257. void AP_list_print(char *str, pList list)
  258. {
  259. pListitem cur;
  260. printf("%s (%d items): ",str,list->count);
  261. cur=list->head;
  262. while(cur)
  263. {
  264. printf("%d ",(int)cur->data);
  265. cur=cur->next;
  266. }
  267. printf("\n");
  268. }
  269. /*
  270. * AP_list_revprint(str,list)
  271. *
  272. * Print out the message string followed by the
  273. * items in the list
  274. *
  275. */
  276. void AP_list_revprint(char *str, pList list)
  277. {
  278. pListitem cur;
  279. printf("%s (%d items): ",str,list->count);
  280. cur=list->tail;
  281. while(cur)
  282. {
  283. printf("%d ",(int)cur->data);
  284. cur=cur->prev;
  285. }
  286. printf("\n");
  287. }
  288. /*
  289. * AP_list_search(list,data)
  290. *
  291. * Returns listitem if item appears in the list, otherwise NULL.
  292. *
  293. */
  294. pListitem AP_list_search(pList list, void *data)
  295. {
  296. pListitem cur;
  297. cur=list->head;
  298. while (cur)
  299. {
  300. if (cur->data == data)
  301. return(cur);
  302. cur=cur->next;
  303. }
  304. return(NULL);
  305. }
  306. /*
  307. * AP_list_search_func(list,func,data)
  308. *
  309. * Returns listitem if func(listitem->data,data) returns true
  310. *
  311. */
  312. pListitem AP_list_search_func(pList list,
  313. int (*func)(void *item_data, void *fixed_data),
  314. void *fixed_data)
  315. {
  316. pListitem cur;
  317. cur=list->head;
  318. while (cur)
  319. {
  320. if ( (*func)(cur->data,fixed_data) )
  321. return(cur);
  322. cur=cur->next;
  323. }
  324. return(NULL);
  325. }
  326. /*
  327. * AP_list_next(list,data,temp)
  328. *
  329. * like PList_next() except handles NULL pointers properly.
  330. *
  331. * initially, pass in (void **) NULL in 'temp'
  332. * returns next list item through 'item'
  333. * returns nonzero if there is a next item
  334. *
  335. */
  336. int AP_list_next(pList list, void **data, void **temp)
  337. {
  338. pListitem cur;
  339. if (*temp) /* temp is previous item */
  340. {
  341. cur=(pListitem)(*temp);
  342. cur=cur->next;
  343. }
  344. else /* First item */
  345. cur=list->head;
  346. if (cur)
  347. {
  348. *temp=(void *)cur;
  349. *data=cur->data;
  350. return(1);
  351. }
  352. else
  353. return(0);
  354. }
  355. /*
  356. * Compatibility routine for scorec list traversal
  357. * Does not provide any way to differentiate
  358. * between NULL in the list, and the end of the list
  359. *
  360. */
  361. void *AP_list_braindead_next(pList list, void **temp)
  362. {
  363. void *item;
  364. if (AP_list_next(list,&item,temp))
  365. return(item);
  366. else
  367. return(NULL);
  368. }
  369. /*
  370. * AP_list_duplicate(list)
  371. *
  372. * return a copy of the list
  373. * (Note: caller is responsible for freeing this list)
  374. *
  375. */
  376. pList AP_list_duplicate(pList list)
  377. {
  378. pList newlist;
  379. pListitem cur,new,prev;
  380. newlist=AP_list_new();
  381. prev=NULL;
  382. cur=list->head;
  383. while(cur)
  384. {
  385. new=AP_listitem_malloc();
  386. new->data=cur->data;
  387. new->prev=prev;
  388. if (prev)
  389. prev->next=new;
  390. else
  391. newlist->head=new;
  392. prev=new;
  393. cur=cur->next;
  394. }
  395. if (prev)
  396. prev->next=NULL;
  397. newlist->tail=prev;
  398. newlist->count=list->count;
  399. return(newlist);
  400. }
  401. int AP_list_apply(pList list,
  402. int (*func)(void *item_data, void *fixed_data),
  403. void *fixed_data)
  404. {
  405. pListitem cur;
  406. int total;
  407. total=0;
  408. cur=list->head;
  409. while (cur)
  410. {
  411. total += (*func)(cur->data,fixed_data);
  412. cur=cur->next;
  413. }
  414. return(total);
  415. }
  416. /*
  417. * main for debugging
  418. *
  419. */
  420. #ifdef LISTMAIN
  421. int main()
  422. {
  423. pList mylist, list2;
  424. int i;
  425. void *temp,*item;
  426. pListitem next;
  427. mylist=AP_list_new();
  428. for (i=1; i<10; i++)
  429. {
  430. AP_list_prepend(mylist,(void *)i);
  431. AP_list_print("current",mylist);
  432. AP_list_revprint(" rev",mylist);
  433. }
  434. printf("Size %d\n",AP_list_size(mylist));
  435. for (i=10; i<15; i++)
  436. {
  437. AP_list_append(mylist,(void *)i);
  438. AP_list_print("new",mylist);
  439. AP_list_revprint(" rev",mylist);
  440. }
  441. AP_list_delete(mylist,(void *)5);
  442. AP_list_print("less 5",mylist);
  443. AP_list_revprint(" rev",mylist);
  444. AP_list_delete(mylist,(void *)9);
  445. AP_list_print("less 9",mylist);
  446. AP_list_revprint(" rev",mylist);
  447. AP_list_delete(mylist,(void *)14);
  448. AP_list_print("less 14",mylist);
  449. AP_list_revprint(" rev",mylist);
  450. AP_list_delete(mylist,(void *)2);
  451. AP_list_print("less 2",mylist);
  452. AP_list_revprint(" rev",mylist);
  453. if (!AP_list_delete(mylist,(void *)0))
  454. printf("(did not delete 0)\n");
  455. else
  456. printf("ERROR - found 0\n");
  457. AP_list_print("less 0",mylist);
  458. AP_list_revprint(" rev",mylist);
  459. if (AP_list_search(mylist,(void *)4))
  460. printf("Found 4\n");
  461. else
  462. printf("Did not find 4\n");
  463. if (AP_list_search(mylist,(void *)9))
  464. printf("Found 9\n");
  465. else
  466. printf("Did not find 9\n");
  467. printf("Traversal by AP_list_next()\n");
  468. temp=NULL;
  469. while (AP_list_next(mylist,&item,&temp))
  470. printf(" Got item %d\n",(int)item);
  471. printf("Traversal by AP_listitem_next()\n");
  472. for (item=AP_list_head_item(mylist); item; item=AP_listitem_next(item))
  473. printf(" Got item %d\n",(int)(AP_listitem_data(item)));
  474. list2=AP_list_duplicate(mylist);
  475. AP_list_print("Original list",mylist);
  476. AP_list_revprint(" rev",mylist);
  477. AP_list_print("Duplicate ",list2);
  478. AP_list_revprint(" rev",list2);
  479. AP_list_append(list2,(void *)99);
  480. AP_list_print("Dup add 99 ",list2);
  481. AP_list_revprint(" rev",list2);
  482. printf("Traversal by AP_listitem_next(), deleting\n");
  483. i=0;
  484. for (item=AP_list_head_item(list2); item; )
  485. {
  486. printf(" Got item %d",(int)(AP_listitem_data(item)));
  487. next=AP_listitem_next(item);
  488. if (i%2)
  489. {
  490. AP_list_delete_item(list2,item);
  491. printf(" - deleted\n");
  492. }
  493. else
  494. printf("\n");
  495. item=next;
  496. i++;
  497. }
  498. AP_list_print("After delete-traversal",list2);
  499. AP_list_free(mylist);
  500. AP_list_print("After del ",list2);
  501. AP_list_revprint(" rev",list2);
  502. AP_list_free(list2);
  503. AP_listitem_verify();
  504. return(0);
  505. }
  506. #endif