list.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // list.c
  3. //
  4. // Copyright (c) 2010 TJ Holowaychuk <tj@vision-media.ca>
  5. //
  6. #include "list.h"
  7. /*
  8. * Allocate a new list_t. NULL on failure.
  9. */
  10. list_t *
  11. list_new() {
  12. list_t *self;
  13. if (!(self = LIST_MALLOC(sizeof(list_t))))
  14. return NULL;
  15. self->head = NULL;
  16. self->tail = NULL;
  17. self->free = NULL;
  18. self->match = NULL;
  19. self->len = 0;
  20. return self;
  21. }
  22. int list_len( list_t* self )
  23. {
  24. return self->len;
  25. }
  26. /*
  27. * Free the list.
  28. */
  29. void
  30. list_destroy(list_t *self) {
  31. unsigned int len = self->len;
  32. list_node_t *next;
  33. list_node_t *curr = self->head;
  34. while (len--) {
  35. next = curr->next;
  36. if (self->free) self->free(curr->val);
  37. LIST_FREE(curr);
  38. curr = next;
  39. }
  40. LIST_FREE(self);
  41. }
  42. /*
  43. * Append the given node to the list
  44. * and return the node, NULL on failure.
  45. */
  46. list_node_t *
  47. list_rpush(list_t *self, list_node_t *node) {
  48. if (!node) return NULL;
  49. if (self->len) {
  50. node->prev = self->tail;
  51. node->next = NULL;
  52. self->tail->next = node;
  53. self->tail = node;
  54. } else {
  55. self->head = self->tail = node;
  56. node->prev = node->next = NULL;
  57. }
  58. ++self->len;
  59. return node;
  60. }
  61. /*
  62. * Return / detach the last node in the list, or NULL.
  63. */
  64. list_node_t *
  65. list_rpop(list_t *self) {
  66. if (!self->len) return NULL;
  67. list_node_t *node = self->tail;
  68. if (--self->len) {
  69. (self->tail = node->prev)->next = NULL;
  70. } else {
  71. self->tail = self->head = NULL;
  72. }
  73. node->next = node->prev = NULL;
  74. return node;
  75. }
  76. /*
  77. * Return / detach the first node in the list, or NULL.
  78. */
  79. list_node_t *
  80. list_lpop(list_t *self) {
  81. if (!self->len) return NULL;
  82. list_node_t *node = self->head;
  83. if (--self->len) {
  84. (self->head = node->next)->prev = NULL;
  85. } else {
  86. self->head = self->tail = NULL;
  87. }
  88. node->next = node->prev = NULL;
  89. return node;
  90. }
  91. /*
  92. * Prepend the given node to the list
  93. * and return the node, NULL on failure.
  94. */
  95. list_node_t *
  96. list_lpush(list_t *self, list_node_t *node) {
  97. if (!node) return NULL;
  98. if (self->len) {
  99. node->next = self->head;
  100. node->prev = NULL;
  101. self->head->prev = node;
  102. self->head = node;
  103. } else {
  104. self->head = self->tail = node;
  105. node->prev = node->next = NULL;
  106. }
  107. ++self->len;
  108. return node;
  109. }
  110. /*
  111. * Return the node associated to val or NULL.
  112. */
  113. list_node_t *
  114. list_find(list_t *self, void *val) {
  115. list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
  116. list_node_t *node;
  117. while ((node = list_iterator_next(it))) {
  118. if (self->match) {
  119. if (self->match(val, node->val)) {
  120. list_iterator_destroy(it);
  121. return node;
  122. }
  123. } else {
  124. if (val == node->val) {
  125. list_iterator_destroy(it);
  126. return node;
  127. }
  128. }
  129. }
  130. list_iterator_destroy(it);
  131. return NULL;
  132. }
  133. /*
  134. * Return the node at the given index or NULL.
  135. */
  136. list_node_t *
  137. list_at(list_t *self, int index) {
  138. list_direction_t direction = LIST_HEAD;
  139. if (index < 0) {
  140. direction = LIST_TAIL;
  141. index = ~index;
  142. }
  143. if ((unsigned)index < self->len) {
  144. list_iterator_t *it = list_iterator_new(self, direction);
  145. list_node_t *node = list_iterator_next(it);
  146. while (index--) node = list_iterator_next(it);
  147. list_iterator_destroy(it);
  148. return node;
  149. }
  150. return NULL;
  151. }
  152. /*
  153. * Remove the given node from the list, freeing it and it's value.
  154. */
  155. void
  156. list_remove(list_t *self, list_node_t *node) {
  157. node->prev
  158. ? (node->prev->next = node->next)
  159. : (self->head = node->next);
  160. node->next
  161. ? (node->next->prev = node->prev)
  162. : (self->tail = node->prev);
  163. if (self->free) self->free(node->val);
  164. LIST_FREE(node);
  165. --self->len;
  166. }
  167. /*
  168. stdout:
  169. c
  170. js
  171. ruby
  172. */
  173. void list_example( void )
  174. {
  175. list_t *langs = list_new();
  176. list_node_t *c = list_rpush(langs, list_node_new("c"));
  177. list_node_t *js = list_rpush(langs, list_node_new("js"));
  178. list_node_t *ruby = list_rpush(langs, list_node_new("ruby"));
  179. list_node_t *node;
  180. list_iterator_t *it = list_iterator_new(langs, LIST_HEAD);
  181. while ((node = list_iterator_next(it))) {
  182. puts(node->val);
  183. }
  184. list_iterator_destroy(it);
  185. list_destroy(langs);
  186. }
  187. void list_clear(list_t *self)
  188. {
  189. list_node_t *node;
  190. list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
  191. while ((node = list_iterator_next(it))) {
  192. list_remove( self, node );
  193. }
  194. list_iterator_destroy(it);
  195. }
  196. void list_set_match(list_t* self, void* pfunc )
  197. {
  198. self->match = pfunc;
  199. }
  200. void list_sort(list_t* self)
  201. {
  202. void* tmp;
  203. int IsChange;
  204. list_node_t * inode = NULL;
  205. list_node_t * jnode = NULL;
  206. if( list_len(self) <= 1 ){
  207. return;
  208. }
  209. inode = self->head;
  210. while ( inode != self->tail ){
  211. jnode = inode->next;
  212. while ( jnode != NULL){
  213. if ( self->match( inode->val, jnode->val) == 0){
  214. tmp = inode->val;
  215. inode->val = jnode->val;
  216. jnode->val = tmp;
  217. }
  218. jnode = jnode->next;
  219. }
  220. inode = inode->next;
  221. }
  222. }