zend_llist.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 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@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include "zend.h"
  21. #include "zend_llist.h"
  22. #include "zend_qsort.h"
  23. ZEND_API void zend_llist_init(zend_llist *l, size_t size, llist_dtor_func_t dtor, unsigned char persistent)
  24. {
  25. l->head = NULL;
  26. l->tail = NULL;
  27. l->count = 0;
  28. l->size = size;
  29. l->dtor = dtor;
  30. l->persistent = persistent;
  31. }
  32. ZEND_API void zend_llist_add_element(zend_llist *l, void *element)
  33. {
  34. zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);
  35. tmp->prev = l->tail;
  36. tmp->next = NULL;
  37. if (l->tail) {
  38. l->tail->next = tmp;
  39. } else {
  40. l->head = tmp;
  41. }
  42. l->tail = tmp;
  43. memcpy(tmp->data, element, l->size);
  44. ++l->count;
  45. }
  46. ZEND_API void zend_llist_prepend_element(zend_llist *l, void *element)
  47. {
  48. zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);
  49. tmp->next = l->head;
  50. tmp->prev = NULL;
  51. if (l->head) {
  52. l->head->prev = tmp;
  53. } else {
  54. l->tail = tmp;
  55. }
  56. l->head = tmp;
  57. memcpy(tmp->data, element, l->size);
  58. ++l->count;
  59. }
  60. #define DEL_LLIST_ELEMENT(current, l) \
  61. if ((current)->prev) {\
  62. (current)->prev->next = (current)->next;\
  63. } else {\
  64. (l)->head = (current)->next;\
  65. }\
  66. if ((current)->next) {\
  67. (current)->next->prev = (current)->prev;\
  68. } else {\
  69. (l)->tail = (current)->prev;\
  70. }\
  71. if ((l)->dtor) {\
  72. (l)->dtor((current)->data);\
  73. }\
  74. pefree((current), (l)->persistent);\
  75. --l->count;
  76. ZEND_API void zend_llist_del_element(zend_llist *l, void *element, int (*compare)(void *element1, void *element2))
  77. {
  78. zend_llist_element *current=l->head;
  79. while (current) {
  80. if (compare(current->data, element)) {
  81. DEL_LLIST_ELEMENT(current, l);
  82. break;
  83. }
  84. current = current->next;
  85. }
  86. }
  87. ZEND_API void zend_llist_destroy(zend_llist *l)
  88. {
  89. zend_llist_element *current=l->head, *next;
  90. while (current) {
  91. next = current->next;
  92. if (l->dtor) {
  93. l->dtor(current->data);
  94. }
  95. pefree(current, l->persistent);
  96. current = next;
  97. }
  98. l->count = 0;
  99. }
  100. ZEND_API void zend_llist_clean(zend_llist *l)
  101. {
  102. zend_llist_destroy(l);
  103. l->head = l->tail = NULL;
  104. }
  105. ZEND_API void *zend_llist_remove_tail(zend_llist *l)
  106. {
  107. zend_llist_element *old_tail;
  108. void *data;
  109. if ((old_tail = l->tail)) {
  110. if (old_tail->prev) {
  111. old_tail->prev->next = NULL;
  112. } else {
  113. l->head = NULL;
  114. }
  115. data = old_tail->data;
  116. l->tail = old_tail->prev;
  117. if (l->dtor) {
  118. l->dtor(data);
  119. }
  120. pefree(old_tail, l->persistent);
  121. --l->count;
  122. return data;
  123. }
  124. return NULL;
  125. }
  126. ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
  127. {
  128. zend_llist_element *ptr;
  129. zend_llist_init(dst, src->size, src->dtor, src->persistent);
  130. ptr = src->head;
  131. while (ptr) {
  132. zend_llist_add_element(dst, ptr->data);
  133. ptr = ptr->next;
  134. }
  135. }
  136. ZEND_API void zend_llist_apply_with_del(zend_llist *l, int (*func)(void *data))
  137. {
  138. zend_llist_element *element, *next;
  139. element=l->head;
  140. while (element) {
  141. next = element->next;
  142. if (func(element->data)) {
  143. DEL_LLIST_ELEMENT(element, l);
  144. }
  145. element = next;
  146. }
  147. }
  148. ZEND_API void zend_llist_apply(zend_llist *l, llist_apply_func_t func TSRMLS_DC)
  149. {
  150. zend_llist_element *element;
  151. for (element=l->head; element; element=element->next) {
  152. func(element->data TSRMLS_CC);
  153. }
  154. }
  155. ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func TSRMLS_DC)
  156. {
  157. size_t i;
  158. zend_llist_element **elements;
  159. zend_llist_element *element, **ptr;
  160. if (l->count <= 0) {
  161. return;
  162. }
  163. elements = (zend_llist_element **) emalloc(l->count * sizeof(zend_llist_element *));
  164. ptr = &elements[0];
  165. for (element=l->head; element; element=element->next) {
  166. *ptr++ = element;
  167. }
  168. zend_qsort(elements, l->count, sizeof(zend_llist_element *), (compare_func_t) comp_func TSRMLS_CC);
  169. l->head = elements[0];
  170. elements[0]->prev = NULL;
  171. for (i = 1; i < l->count; i++) {
  172. elements[i]->prev = elements[i-1];
  173. elements[i-1]->next = elements[i];
  174. }
  175. elements[i-1]->next = NULL;
  176. l->tail = elements[i-1];
  177. efree(elements);
  178. }
  179. ZEND_API void zend_llist_apply_with_argument(zend_llist *l, llist_apply_with_arg_func_t func, void *arg TSRMLS_DC)
  180. {
  181. zend_llist_element *element;
  182. for (element=l->head; element; element=element->next) {
  183. func(element->data, arg TSRMLS_CC);
  184. }
  185. }
  186. ZEND_API void zend_llist_apply_with_arguments(zend_llist *l, llist_apply_with_args_func_t func TSRMLS_DC, int num_args, ...)
  187. {
  188. zend_llist_element *element;
  189. va_list args;
  190. va_start(args, num_args);
  191. for (element=l->head; element; element=element->next) {
  192. func(element->data, num_args, args TSRMLS_CC);
  193. }
  194. va_end(args);
  195. }
  196. ZEND_API int zend_llist_count(zend_llist *l)
  197. {
  198. return l->count;
  199. }
  200. ZEND_API void *zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
  201. {
  202. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  203. *current = l->head;
  204. if (*current) {
  205. return (*current)->data;
  206. } else {
  207. return NULL;
  208. }
  209. }
  210. ZEND_API void *zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos)
  211. {
  212. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  213. *current = l->tail;
  214. if (*current) {
  215. return (*current)->data;
  216. } else {
  217. return NULL;
  218. }
  219. }
  220. ZEND_API void *zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
  221. {
  222. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  223. if (*current) {
  224. *current = (*current)->next;
  225. if (*current) {
  226. return (*current)->data;
  227. }
  228. }
  229. return NULL;
  230. }
  231. ZEND_API void *zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos)
  232. {
  233. zend_llist_position *current = pos ? pos : &l->traverse_ptr;
  234. if (*current) {
  235. *current = (*current)->prev;
  236. if (*current) {
  237. return (*current)->data;
  238. }
  239. }
  240. return NULL;
  241. }
  242. /*
  243. * Local variables:
  244. * tab-width: 4
  245. * c-basic-offset: 4
  246. * indent-tabs-mode: t
  247. * End:
  248. */