list.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include "private.h"
  4. /*--------------------------------------------------------- */
  5. static inline void lub_list_init(lub_list_t * this,
  6. lub_list_compare_fn compareFn)
  7. {
  8. this->head = NULL;
  9. this->compareFn = compareFn;
  10. }
  11. /*--------------------------------------------------------- */
  12. lub_list_t *lub_list_new(lub_list_compare_fn compareFn)
  13. {
  14. lub_list_t *this;
  15. this = malloc(sizeof(*this));
  16. assert(this);
  17. lub_list_init(this, compareFn);
  18. return this;
  19. }
  20. /*--------------------------------------------------------- */
  21. void inline lub_list_free(lub_list_t *this)
  22. {
  23. free(this);
  24. }
  25. /*--------------------------------------------------------- */
  26. inline lub_list_node_t *lub_list__get_head(lub_list_t *this)
  27. {
  28. return this->head;
  29. }
  30. /*--------------------------------------------------------- */
  31. inline lub_list_node_t *lub_list__get_tail(lub_list_t *this)
  32. {
  33. return this->tail;
  34. }
  35. /*--------------------------------------------------------- */
  36. static inline void lub_list_node_init(lub_list_node_t *this,
  37. void *data)
  38. {
  39. this->prev = this->next = NULL;
  40. this->data = data;
  41. }
  42. /*--------------------------------------------------------- */
  43. lub_list_node_t *lub_list_node_new(void *data)
  44. {
  45. lub_list_node_t *this;
  46. this = malloc(sizeof(*this));
  47. assert(this);
  48. lub_list_node_init(this, data);
  49. return this;
  50. }
  51. /*--------------------------------------------------------- */
  52. inline lub_list_node_t *lub_list_iterator_init(lub_list_t *this)
  53. {
  54. return this->head;
  55. }
  56. /*--------------------------------------------------------- */
  57. inline lub_list_node_t *lub_list_iterator_next(lub_list_node_t *this)
  58. {
  59. return lub_list_node__get_next(this);
  60. }
  61. /*--------------------------------------------------------- */
  62. inline lub_list_node_t *lub_list_iterator_prev(lub_list_node_t *this)
  63. {
  64. return lub_list_node__get_prev(this);
  65. }
  66. /*--------------------------------------------------------- */
  67. void inline lub_list_node_free(lub_list_node_t *this)
  68. {
  69. free(this);
  70. }
  71. /*--------------------------------------------------------- */
  72. inline lub_list_node_t *lub_list_node__get_prev(lub_list_node_t *this)
  73. {
  74. return this->prev;
  75. }
  76. /*--------------------------------------------------------- */
  77. inline lub_list_node_t *lub_list_node__get_next(lub_list_node_t *this)
  78. {
  79. return this->next;
  80. }
  81. /*--------------------------------------------------------- */
  82. inline void *lub_list_node__get_data(lub_list_node_t *this)
  83. {
  84. return this->data;
  85. }
  86. /*--------------------------------------------------------- */
  87. lub_list_node_t *lub_list_add(lub_list_t *this, void *data)
  88. {
  89. lub_list_node_t *node = lub_list_node_new(data);
  90. lub_list_node_t *iter;
  91. /* Empty list */
  92. if (!this->head) {
  93. this->head = node;
  94. this->tail = node;
  95. return node;
  96. }
  97. /* Not sorted list. Add to the tail. */
  98. if (!this->compareFn) {
  99. node->prev = this->tail;
  100. node->next = NULL;
  101. this->tail->next = node;
  102. this->tail = node;
  103. return node;
  104. }
  105. /* Sorted list */
  106. iter = this->tail;
  107. while (iter) {
  108. if (this->compareFn(node->data, iter->data) >= 0) {
  109. node->next = iter->next;
  110. node->prev = iter;
  111. iter->next = node;
  112. if (node->next)
  113. node->next->prev = node;
  114. break;
  115. }
  116. iter = iter->prev;
  117. }
  118. /* Insert node into the list head */
  119. if (!iter) {
  120. node->next = this->head;
  121. node->prev = NULL;
  122. this->head->prev = node;
  123. this->head = node;
  124. }
  125. if (!node->next)
  126. this->tail = node;
  127. return node;
  128. }
  129. /*--------------------------------------------------------- */
  130. void lub_list_del(lub_list_t *this, lub_list_node_t *node)
  131. {
  132. if (node->prev)
  133. node->prev->next = node->next;
  134. else
  135. this->head = node->next;
  136. if (node->next)
  137. node->next->prev = node->prev;
  138. else
  139. this->tail = node->prev;
  140. }
  141. /*--------------------------------------------------------- */