utils_list.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "utils_list.h"
  19. #include "uiot_import.h"
  20. /*
  21. * 创建List. 失败则返回NULL.
  22. */
  23. List *list_new(void)
  24. {
  25. List *self;
  26. self = (List *)HAL_Malloc(sizeof(List));
  27. if (!self) {
  28. return NULL;
  29. }
  30. self->head = NULL;
  31. self->tail = NULL;
  32. self->free = NULL;
  33. self->match = NULL;
  34. self->len = 0;
  35. return self;
  36. }
  37. /*
  38. * 失败List的内存.
  39. */
  40. void list_destroy(List *self)
  41. {
  42. unsigned int len = self->len;
  43. ListNode *next;
  44. ListNode *curr = self->head;
  45. while (len--) {
  46. next = curr->next;
  47. if (self->free) {
  48. self->free(curr->val);
  49. }
  50. HAL_Free(curr);
  51. curr = next;
  52. }
  53. HAL_Free(self);
  54. }
  55. /*
  56. * 将给定节点附加到列表并返回该节点,在失败时返回NULL.
  57. */
  58. ListNode *list_rpush(List *self, ListNode *node)
  59. {
  60. if (!node) {
  61. return NULL;
  62. }
  63. if (self->len) {
  64. node->prev = self->tail;
  65. node->next = NULL;
  66. self->tail->next = node;
  67. self->tail = node;
  68. } else {
  69. self->head = self->tail = node;
  70. node->prev = node->next = NULL;
  71. }
  72. ++self->len;
  73. return node;
  74. }
  75. /*
  76. * 弹出列表中的最后一个节点, 失败返回NULL.
  77. */
  78. ListNode *list_rpop(List *self)
  79. {
  80. ListNode *node = NULL;
  81. if (!self->len) {
  82. return NULL;
  83. }
  84. node = self->tail;
  85. if (--self->len) {
  86. (self->tail = node->prev)->next = NULL;
  87. } else {
  88. self->tail = self->head = NULL;
  89. }
  90. node->next = node->prev = NULL;
  91. return node;
  92. }
  93. /*
  94. * 弹出列表中的首个节点, 失败返回NULL.
  95. */
  96. ListNode *list_lpop(List *self)
  97. {
  98. ListNode *node = NULL;
  99. if (!self->len) {
  100. return NULL;
  101. }
  102. node = self->head;
  103. if (--self->len) {
  104. (self->head = node->next)->prev = NULL;
  105. } else {
  106. self->head = self->tail = NULL;
  107. }
  108. node->next = node->prev = NULL;
  109. return node;
  110. }
  111. /*
  112. * 预先将给定的节点添加到列表中,并返回该节点,在失败时返回NULL.
  113. */
  114. ListNode *list_lpush(List *self, ListNode *node)
  115. {
  116. if (!node) {
  117. return NULL;
  118. }
  119. if (self->len) {
  120. node->next = self->head;
  121. node->prev = NULL;
  122. self->head->prev = node;
  123. self->head = node;
  124. } else {
  125. self->head = self->tail = node;
  126. node->prev = node->next = NULL;
  127. }
  128. ++self->len;
  129. return node;
  130. }
  131. /*
  132. * 根据val返回对应的节点,没有则返回NULL.
  133. */
  134. ListNode *list_find(List *self, void *val)
  135. {
  136. ListIterator *it;
  137. ListNode *node;
  138. if (NULL == (it = list_iterator_new(self, LIST_HEAD))) {
  139. return NULL;
  140. }
  141. node = list_iterator_next(it);
  142. while (node) {
  143. if (self->match) {
  144. if (self->match(val, node->val)) {
  145. list_iterator_destroy(it);
  146. return node;
  147. }
  148. } else {
  149. if (val == node->val) {
  150. list_iterator_destroy(it);
  151. return node;
  152. }
  153. }
  154. node = list_iterator_next(it);
  155. }
  156. list_iterator_destroy(it);
  157. return NULL;
  158. }
  159. /*
  160. * 根据index返回对应的节点,没有则返回NULL.
  161. */
  162. ListNode *list_at(List *self, int index)
  163. {
  164. ListDirection direction = LIST_HEAD;
  165. if (index < 0) {
  166. direction = LIST_TAIL;
  167. index = ~index;
  168. }
  169. if ((unsigned) index < self->len) {
  170. ListIterator *it;
  171. ListNode *node;
  172. if (NULL == (it = list_iterator_new(self, direction))) {
  173. return NULL;
  174. }
  175. node = list_iterator_next(it);
  176. while (index--) {
  177. node = list_iterator_next(it);
  178. }
  179. list_iterator_destroy(it);
  180. return node;
  181. }
  182. return NULL;
  183. }
  184. /*
  185. * 从列表中删除给定的节点,释放它和它的值.
  186. */
  187. void list_remove(List *self, ListNode *node)
  188. {
  189. node->prev ? (node->prev->next = node->next) : (self->head = node->next);
  190. node->next ? (node->next->prev = node->prev) : (self->tail = node->prev);
  191. if (self->free) {
  192. self->free(node->val);
  193. }
  194. HAL_Free(node);
  195. --self->len;
  196. }
  197. /*
  198. * 创建一个新的ListIterator,失败返回NULL, 并且设置其ListDirection.
  199. */
  200. ListIterator *list_iterator_new(List *list, ListDirection direction)
  201. {
  202. ListNode *node = direction == LIST_HEAD ? list->head : list->tail;
  203. return list_iterator_new_from_node(node, direction);
  204. }
  205. /*
  206. * 创建一个新的ListIterator, 并设置初始节点. 失败则返回NULL.
  207. */
  208. ListIterator *list_iterator_new_from_node(ListNode *node, ListDirection direction)
  209. {
  210. ListIterator *self;
  211. self = HAL_Malloc(sizeof(ListIterator));
  212. if (!self) {
  213. return NULL;
  214. }
  215. self->next = node;
  216. self->direction = direction;
  217. return self;
  218. }
  219. /*
  220. * 返回下一个节点, 如果没有更多的节点则返回NULL.
  221. */
  222. ListNode *list_iterator_next(ListIterator *self)
  223. {
  224. ListNode *curr = self->next;
  225. if (curr) {
  226. self->next = self->direction == LIST_HEAD ? curr->next : curr->prev;
  227. }
  228. return curr;
  229. }
  230. /*
  231. * 释放列表迭代器.
  232. */
  233. void list_iterator_destroy(ListIterator *self)
  234. {
  235. HAL_Free(self);
  236. self = NULL;
  237. }
  238. /*
  239. * 根据预设值来创建新节点, 失败则返回NULL.
  240. */
  241. ListNode *list_node_new(void *val)
  242. {
  243. ListNode *self;
  244. self = HAL_Malloc(sizeof(ListNode));
  245. if (!self) {
  246. return NULL;
  247. }
  248. self->prev = NULL;
  249. self->next = NULL;
  250. self->val = val;
  251. return self;
  252. }
  253. #ifdef __cplusplus
  254. }
  255. #endif