zend_llist.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_llist.h"
  21. #include "zend_sort.h"
  22. ZEND_API void zend_llist_init(zend_llist *l, size_t size, llist_dtor_func_t dtor, unsigned char persistent)
  23. {
  24. l->head = NULL;
  25. l->tail = NULL;
  26. l->count = 0;
  27. l->size = size;
  28. l->dtor = dtor;
  29. l->persistent = persistent;
  30. }
  31. ZEND_API void zend_llist_add_element(zend_llist *l, const void *element)
  32. {
  33. zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);
  34. tmp->prev = l->tail;
  35. tmp->next = NULL;
  36. if (l->tail) {
  37. l->tail->next = tmp;
  38. } else {
  39. l->head = tmp;
  40. }
  41. l->tail = tmp;
  42. memcpy(tmp->data, element, l->size);
  43. ++l->count;
  44. }
  45. ZEND_API void zend_llist_prepend_element(zend_llist *l, const void *element)
  46. {
  47. zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);
  48. tmp->next = l->head;
  49. tmp->prev = NULL;
  50. if (l->head) {
  51. l->head->prev = tmp;
  52. } else {
  53. l->tail = tmp;
  54. }
  55. l->head = tmp;
  56. memcpy(tmp->data, element, l->size);
  57. ++l->count;
  58. }
  59. #define DEL_LLIST_ELEMENT(current, l) \
  60. if ((current)->prev) {\
  61. (current)->prev->next = (current)->next;\
  62. } else {\
  63. (l)->head = (current)->next;\
  64. }\
  65. if ((current)->next) {\
  66. (current)->next->prev = (current)->prev;\
  67. } else {\
  68. (l)->tail = (current)->prev;\
  69. }\
  70. if ((l)->dtor) {\
  71. (l)->dtor((current)->data);\
  72. }\
  73. pefree((current), (l)->persistent);\
  74. --l->count;
  75. ZEND_API void zend_llist_del_element(zend_llist *l, void *element, int (*compare)(void *element1, void *element2))
  76. {
  77. zend_llist_element *current=l->head;
  78. while (current) {
  79. if (compare(current->data, element)) {
  80. DEL_LLIST_ELEMENT(current, l);
  81. break;
  82. }
  83. current = current->next;
  84. }
  85. }
  86. ZEND_API void zend_llist_destroy(zend_llist *l)
  87. {
  88. zend_llist_element *current=l->head, *next;
  89. while (current) {
  90. next = current->next;
  91. if (l->dtor) {
  92. l->dtor(current->data);
  93. }
  94. pefree(current, l->persistent);
  95. current = next;
  96. }
  97. l->head = NULL;
  98. l->tail = NULL;
  99. l->count = 0;
  100. }
  101. ZEND_API void zend_llist_clean(zend_llist *l)
  102. {
  103. zend_llist_destroy(l);
  104. l->head = l->tail = NULL;
  105. }
  106. ZEND_API void zend_llist_remove_tail(zend_llist *l)
  107. {
  108. zend_llist_element *old_tail = l->tail;
  109. if (!old_tail) {
  110. return;
  111. }
  112. if (old_tail->prev) {
  113. old_tail->prev->next = NULL;
  114. } else {
  115. l->head = NULL;
  116. }
  117. l->tail = old_tail->prev;
  118. --l->count;
  119. if (l->dtor) {
  120. l->dtor(old_tail->data);
  121. }
  122. pefree(old_tail, l->persistent);
  123. }
  124. ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
  125. {
  126. zend_llist_element *ptr;
  127. zend_llist_init(dst, src->size, src->dtor, src->persistent);
  128. ptr = src->head;
  129. while (ptr) {
  130. zend_llist_add_element(dst, ptr->data);
  131. ptr = ptr->next;
  132. }
  133. }
  134. ZEND_API void zend_llist_apply_with_del(zend_llist *l, int (*func)(void *data))
  135. {
  136. zend_llist_element *element, *next;
  137. element=l->head;
  138. while (element) {
  139. next = element->next;
  140. if (func(element->data)) {
  141. DEL_LLIST_ELEMENT(element, l);
  142. }
  143. element = next;
  144. }
  145. }
  146. ZEND_API void zend_llist_apply(zend_llist *l, llist_apply_func_t func)
  147. {
  148. zend_llist_element *element;
  149. for (element=l->head; element; element=element->next) {
  150. func(element->data);
  151. }
  152. }
  153. static void zend_llist_swap(zend_llist_element **p, zend_llist_element **q)
  154. {
  155. zend_llist_element *t;
  156. t = *p;
  157. *p = *q;
  158. *q = t;
  159. }
  160. ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func)
  161. {
  162. size_t i;
  163. zend_llist_element **elements;
  164. zend_llist_element *element, **ptr;
  165. if (l->count == 0) {
  166. return;
  167. }
  168. elements = (zend_llist_element **) emalloc(l->count * sizeof(zend_llist_element *));
  169. ptr = &elements[0];
  170. for (element=l->head; element; element=element->next) {
  171. *ptr++ = element;
  172. }
  173. zend_sort(elements, l->count, sizeof(zend_llist_element *),
  174. (compare_func_t) comp_func, (swap_func_t) zend_llist_swap);
  175. l->head = elements[0];
  176. elements[0]->prev = NULL;
  177. for (i = 1; i < l->count; i++) {
  178. elements[i]->prev = elements[i-1];
  179. elements[i-1]->next = elements[i];
  180. }
  181. elements[i-1]->next = NULL;
  182. l->tail = elements[i-1];
  183. efree(elements);
  184. }
  185. ZEND_API void zend_llist_apply_with_argument(zend_llist *l, llist_apply_with_arg_func_t func, void *arg)
  186. {
  187. zend_llist_element *element;
  188. for (element=l->head; element; element=element->next) {
  189. func(element->data, arg);
  190. }
  191. }
  192. ZEND_API void zend_llist_apply_with_arguments(zend_llist *l, llist_apply_with_args_func_t func, int num_args, ...)
  193. {
  194. zend_llist_element *element;
  195. va_list args;
  196. va_start(args, num_args);
  197. for (element=l->head; element; element=element->next) {
  198. func(element->data, num_args, args);
  199. }
  200. va_end(args);
  201. }
  202. ZEND_API size_t zend_llist_count(zend_llist *l)
  203. {
  204. return l->count;
  205. }
  206. ZEND_API void *zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
  207. {
  208. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  209. *current = l->head;
  210. if (*current) {
  211. return (*current)->data;
  212. } else {
  213. return NULL;
  214. }
  215. }
  216. ZEND_API void *zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos)
  217. {
  218. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  219. *current = l->tail;
  220. if (*current) {
  221. return (*current)->data;
  222. } else {
  223. return NULL;
  224. }
  225. }
  226. ZEND_API void *zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
  227. {
  228. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  229. if (*current) {
  230. *current = (*current)->next;
  231. if (*current) {
  232. return (*current)->data;
  233. }
  234. }
  235. return NULL;
  236. }
  237. ZEND_API void *zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos)
  238. {
  239. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  240. if (*current) {
  241. *current = (*current)->prev;
  242. if (*current) {
  243. return (*current)->data;
  244. }
  245. }
  246. return NULL;
  247. }