list.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. static const char version[] = "$Id$";
  2. /*
  3. * Copyright 2001-2003, Meiosys (www.meiosys.com). All rights reserved.
  4. *
  5. * See the COPYING file for the terms of usage and distribution.
  6. */
  7. #include <sd/list.h>
  8. #include <sd/malloc.h>
  9. #include <stdlib.h>
  10. struct __sd_list {
  11. sd_list_iter_t* head;
  12. sd_list_iter_t* tail;
  13. size_t nelem;
  14. };
  15. /******************************************************************************/
  16. extern sd_list_t* sd_list_new(size_t a_capacity)
  17. {
  18. sd_list_t* ptrThis;
  19. ptrThis = (sd_list_t*)sd_calloc(1, sizeof(sd_list_t));
  20. ptrThis->head = 0;
  21. ptrThis->tail = 0;
  22. ptrThis->nelem = 0;
  23. return ptrThis;
  24. }
  25. /******************************************************************************/
  26. extern void sd_list_delete(sd_list_t* a_this)
  27. {
  28. sd_list_iter_t *a_next;
  29. sd_list_iter_t *a_current;
  30. if (!a_this)
  31. return;
  32. /* Free the iterators */
  33. if (a_this->nelem > 0){
  34. a_current = a_this->head;
  35. do {
  36. a_next = a_current->__next;
  37. free(a_current);
  38. a_current = a_next;
  39. } while (a_current);
  40. }
  41. free(a_this);
  42. }
  43. /******************************************************************************/
  44. extern void sd_list_clear(sd_list_t* a_this)
  45. {
  46. a_this->head = 0;
  47. a_this->tail = 0;
  48. a_this->nelem = 0;
  49. }
  50. /******************************************************************************/
  51. extern size_t sd_list_get_nelem(sd_list_t* a_this)
  52. {
  53. return (a_this ? a_this->nelem : 0);
  54. }
  55. /******************************************************************************/
  56. extern sd_list_iter_t* sd_list_begin(sd_list_t* a_this)
  57. {
  58. return (a_this ? a_this->head : 0);
  59. }
  60. /******************************************************************************/
  61. extern sd_list_iter_t* sd_list_end(sd_list_t* a_this)
  62. {
  63. return 0;
  64. }
  65. /******************************************************************************/
  66. extern sd_list_iter_t* sd_list_rbegin(sd_list_t* a_this)
  67. {
  68. return (a_this ? a_this->tail : 0);
  69. }
  70. /******************************************************************************/
  71. extern sd_list_iter_t* sd_list_rend(sd_list_t* a_this)
  72. {
  73. return 0;
  74. }
  75. /******************************************************************************/
  76. extern sd_list_iter_t* sd_list_lookup(sd_list_t* a_this, void* a_data)
  77. {
  78. sd_list_iter_t* i;
  79. if (! a_this) return 0;
  80. for (i = a_this->head; i; i = i->__next)
  81. if (a_data == i->data)
  82. return i;
  83. return 0;
  84. }
  85. /******************************************************************************/
  86. extern sd_list_iter_t* sd_list_lookadd(sd_list_t* a_this, void* a_data)
  87. {
  88. sd_list_iter_t* i;
  89. if (! a_this) return 0;
  90. if ((i = sd_list_lookup(a_this, a_data)) != 0)
  91. return i;
  92. return sd_list_add(a_this, a_data);
  93. }
  94. /******************************************************************************/
  95. extern sd_list_iter_t* sd_list_add(sd_list_t* a_this, void* a_data)
  96. {
  97. sd_list_iter_t* i;
  98. if (! a_this) return 0;
  99. if ((i = (sd_list_iter_t*)sd_calloc(1, sizeof(*i))) == 0)
  100. return 0;
  101. i->data = a_data;
  102. i->list = a_this;
  103. i->__next = a_this->head;
  104. i->__prev = 0;
  105. a_this->head = i;
  106. if (i->__next) i->__next->__prev = i;
  107. if (!a_this->tail) a_this->tail = i;
  108. a_this->nelem++;
  109. return i;
  110. }
  111. /******************************************************************************/
  112. extern sd_list_iter_t* sd_list_sortadd(sd_list_t* a_this,
  113. sd_list_func_t a_func, void* a_data)
  114. {
  115. sd_list_iter_t* i;
  116. if (! a_this || ! a_func) return 0;
  117. for (i = a_this->head; i; i = i->__next)
  118. if ((*a_func)(i->data, a_data) > 0)
  119. break;
  120. if (i)
  121. return sd_list_iter_insert(i, a_data);
  122. else
  123. return sd_list_append(a_this, a_data);
  124. }
  125. /******************************************************************************/
  126. extern int sd_list_del(sd_list_t* a_this, void* a_data)
  127. {
  128. sd_list_iter_t* i;
  129. if (!a_this)
  130. return -1;
  131. for (i = a_this->head; i; i = i->__next)
  132. if (a_data == i->data)
  133. break;
  134. if (!i)
  135. return -1;
  136. sd_list_iter_del(i);
  137. return 0;
  138. }
  139. /******************************************************************************/
  140. extern void sd_list_foreach(sd_list_t* a_this, sd_list_func_t a_func,
  141. void* a_userdata)
  142. {
  143. sd_list_iter_t* i;
  144. sd_list_iter_t* j;
  145. if (!a_this || !a_func)
  146. return;
  147. for (i = a_this->head; i; i = j) {
  148. int ret;
  149. i->__foreach = 1;
  150. ret = (*a_func)(i->data, a_userdata);
  151. j = i->__next;
  152. if (i->__foreach == 0)
  153. sd_list_iter_del(i);
  154. else
  155. i->__foreach = 0;
  156. if (ret) return;
  157. }
  158. }
  159. /******************************************************************************/
  160. extern void sd_list_rforeach(sd_list_t* a_this, sd_list_func_t a_func,
  161. void* a_userdata)
  162. {
  163. sd_list_iter_t* i;
  164. sd_list_iter_t* j;
  165. if (!a_this || !a_func)
  166. return;
  167. for (i = a_this->tail; i; i = j) {
  168. int ret;
  169. i->__foreach = 1;
  170. ret = (*a_func)(i->data, a_userdata);
  171. j = i->__prev;
  172. if (i->__foreach == 0)
  173. sd_list_iter_del(i);
  174. else
  175. i->__foreach = 0;
  176. if (ret) return;
  177. }
  178. }
  179. /******************************************************************************/
  180. extern sd_list_iter_t* sd_list_iter_next(sd_list_iter_t* a_this)
  181. {
  182. return (a_this ? a_this->__next : 0);
  183. }
  184. /******************************************************************************/
  185. extern sd_list_iter_t* sd_list_iter_prev(sd_list_iter_t* a_this)
  186. {
  187. return (a_this ? a_this->__prev : 0);
  188. }
  189. /******************************************************************************/
  190. extern void sd_list_iter_del(sd_list_iter_t* a_this)
  191. {
  192. if (!a_this)
  193. return;
  194. if (a_this->__foreach == 1) {
  195. a_this->__foreach = 0;
  196. return;
  197. }
  198. if (a_this->__next)
  199. a_this->__next->__prev = a_this->__prev;
  200. else
  201. a_this->list->tail = a_this->__prev;
  202. if (a_this->__prev)
  203. a_this->__prev->__next = a_this->__next;
  204. else
  205. a_this->list->head = a_this->__next;
  206. a_this->list->nelem--;
  207. free(a_this);
  208. }
  209. /******************************************************************************/
  210. extern sd_list_iter_t* sd_list_iter_insert(sd_list_iter_t* a_this,
  211. void* a_data)
  212. {
  213. sd_list_iter_t* i;
  214. if (! a_this) return 0;
  215. if (a_this->list->head == a_this)
  216. return sd_list_prepend(a_this->list, a_data);
  217. if ((i = (sd_list_iter_t*)sd_calloc(1, sizeof(*i))) == 0)
  218. return 0;
  219. i->data = a_data;
  220. i->list = a_this->list;
  221. i->__prev = a_this->__prev;
  222. i->__next = a_this;
  223. /* CAUTION: always exists since a_this is not the head */
  224. a_this->__prev->__next = i;
  225. a_this->__prev = i;
  226. a_this->list->nelem++;
  227. return i;
  228. }
  229. /******************************************************************************/
  230. extern sd_list_iter_t* sd_list_prepend(sd_list_t* a_this, void* a_data)
  231. {
  232. sd_list_iter_t* i;
  233. if (! a_this) return 0;
  234. if ((i = (sd_list_iter_t*)sd_calloc(1, sizeof(*i))) == 0)
  235. return 0;
  236. i->list = a_this;
  237. i->data = a_data;
  238. i->__prev = 0;
  239. i->__next = a_this->head;
  240. a_this->head = i;
  241. if (i->__next)
  242. i->__next->__prev = i;
  243. else
  244. a_this->tail = i;
  245. a_this->nelem++;
  246. return i;
  247. }
  248. /******************************************************************************/
  249. extern sd_list_iter_t* sd_list_append(sd_list_t* a_this, void* a_data)
  250. {
  251. sd_list_iter_t* i;
  252. if (! a_this) return 0;
  253. if ((i = (sd_list_iter_t*)sd_calloc(1, sizeof(*i))) == 0)
  254. return 0;
  255. i->list = a_this;
  256. i->data = a_data;
  257. i->__prev = a_this->tail;
  258. i->__next = 0;
  259. a_this->tail = i;
  260. if (i->__prev)
  261. i->__prev->__next = i;
  262. else
  263. a_this->head = i;
  264. a_this->nelem++;
  265. return i;
  266. }