list.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <string.h>
  4. #include "private.h"
  5. /*--------------------------------------------------------- */
  6. static void lub_list_init(lub_list_t *this,
  7. lub_list_compare_fn compareFn,
  8. lub_list_free_fn freeFn)
  9. {
  10. this->head = NULL;
  11. this->tail = NULL;
  12. this->compareFn = compareFn;
  13. this->freeFn = freeFn;
  14. this->len = 0;
  15. }
  16. /*--------------------------------------------------------- */
  17. lub_list_t *lub_list_new(lub_list_compare_fn compareFn,
  18. lub_list_free_fn freeFn)
  19. {
  20. lub_list_t *this;
  21. this = malloc(sizeof(*this));
  22. assert(this);
  23. lub_list_init(this, compareFn, freeFn);
  24. return this;
  25. }
  26. /*--------------------------------------------------------- */
  27. inline void lub_list_free(lub_list_t *this)
  28. {
  29. free(this);
  30. }
  31. /*--------------------------------------------------------- */
  32. /* Free all nodes and data from list and finally
  33. * free the list itself. It uses special callback
  34. * function specified by user to free the abstract
  35. * data.
  36. */
  37. void lub_list_free_all(lub_list_t *this)
  38. {
  39. lub_list_node_t *iter;
  40. while ((iter = lub_list__get_head(this))) {
  41. lub_list_del(this, iter);
  42. if (this->freeFn)
  43. this->freeFn(lub_list_node__get_data(iter));
  44. lub_list_node_free(iter);
  45. }
  46. lub_list_free(this);
  47. }
  48. /*--------------------------------------------------------- */
  49. inline lub_list_node_t *lub_list__get_head(lub_list_t *this)
  50. {
  51. return this->head;
  52. }
  53. /*--------------------------------------------------------- */
  54. inline lub_list_node_t *lub_list__get_tail(lub_list_t *this)
  55. {
  56. return this->tail;
  57. }
  58. /*--------------------------------------------------------- */
  59. static void lub_list_node_init(lub_list_node_t *this,
  60. void *data)
  61. {
  62. this->prev = this->next = NULL;
  63. this->data = data;
  64. }
  65. /*--------------------------------------------------------- */
  66. lub_list_node_t *lub_list_node_new(void *data)
  67. {
  68. lub_list_node_t *this;
  69. this = malloc(sizeof(*this));
  70. assert(this);
  71. lub_list_node_init(this, data);
  72. return this;
  73. }
  74. /*--------------------------------------------------------- */
  75. inline lub_list_node_t *lub_list_iterator_init(lub_list_t *this)
  76. {
  77. return this->head;
  78. }
  79. /*--------------------------------------------------------- */
  80. inline lub_list_node_t *lub_list_node__get_prev(lub_list_node_t *this)
  81. {
  82. return this->prev;
  83. }
  84. /*--------------------------------------------------------- */
  85. inline lub_list_node_t *lub_list_node__get_next(lub_list_node_t *this)
  86. {
  87. return this->next;
  88. }
  89. /*--------------------------------------------------------- */
  90. inline lub_list_node_t *lub_list_iterator_next(lub_list_node_t *this)
  91. {
  92. return lub_list_node__get_next(this);
  93. }
  94. /*--------------------------------------------------------- */
  95. inline lub_list_node_t *lub_list_iterator_prev(lub_list_node_t *this)
  96. {
  97. return lub_list_node__get_prev(this);
  98. }
  99. /*--------------------------------------------------------- */
  100. inline void lub_list_node_free(lub_list_node_t *this)
  101. {
  102. free(this);
  103. }
  104. /*--------------------------------------------------------- */
  105. inline void *lub_list_node__get_data(lub_list_node_t *this)
  106. {
  107. return this->data;
  108. }
  109. /*--------------------------------------------------------- */
  110. lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
  111. {
  112. lub_list_node_t *node = lub_list_node_new(data);
  113. lub_list_node_t *iter;
  114. this->len++;
  115. /* Empty list */
  116. if (!this->head) {
  117. this->head = node;
  118. this->tail = node;
  119. return node;
  120. }
  121. /* Not sorted list. Add to the tail. */
  122. if (!this->compareFn) {
  123. node->prev = this->tail;
  124. node->next = NULL;
  125. this->tail->next = node;
  126. this->tail = node;
  127. return node;
  128. }
  129. /* Sorted list */
  130. iter = this->tail;
  131. while (iter) {
  132. if (this->compareFn(node->data, iter->data) >= 0) {
  133. node->next = iter->next;
  134. node->prev = iter;
  135. iter->next = node;
  136. if (node->next)
  137. node->next->prev = node;
  138. break;
  139. }
  140. iter = iter->prev;
  141. }
  142. /* Insert node into the list head */
  143. if (!iter) {
  144. node->next = this->head;
  145. node->prev = NULL;
  146. this->head->prev = node;
  147. this->head = node;
  148. }
  149. if (!node->next)
  150. this->tail = node;
  151. return node;
  152. }
  153. /*--------------------------------------------------------- */
  154. void lub_list_del(lub_list_t *this, lub_list_node_t *node)
  155. {
  156. if (node->prev)
  157. node->prev->next = node->next;
  158. else
  159. this->head = node->next;
  160. if (node->next)
  161. node->next->prev = node->prev;
  162. else
  163. this->tail = node->prev;
  164. this->len--;
  165. }
  166. /*--------------------------------------------------------- */
  167. inline void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src)
  168. {
  169. memcpy(dst, src, sizeof(lub_list_node_t));
  170. }
  171. /*--------------------------------------------------------- */
  172. lub_list_node_t *lub_list_search_node(lub_list_t *this, void *data)
  173. {
  174. lub_list_node_t *iter;
  175. int res;
  176. /* Empty list */
  177. if (!this->head)
  178. return NULL;
  179. /* Not sorted list. Can't search. */
  180. if (!this->compareFn)
  181. return NULL;
  182. /* Sorted list */
  183. iter = this->head;
  184. while (iter) {
  185. res = this->compareFn(data, iter->data);
  186. if (!res)
  187. return iter;
  188. if (res < 0)
  189. break; // No chances to find it
  190. iter = iter->next;
  191. }
  192. return NULL;
  193. }
  194. /*--------------------------------------------------------- */
  195. void *lub_list_search(lub_list_t *this, void *data)
  196. {
  197. lub_list_node_t *iter = lub_list_search_node(this, data);
  198. return iter->data;
  199. }
  200. /*--------------------------------------------------------- */
  201. inline unsigned int lub_list_len(lub_list_t *this)
  202. {
  203. return this->len;
  204. }
  205. /*--------------------------------------------------------- */