1
0

list.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /** @file list.c
  2. * @brief Implementation of a bidirectional list.
  3. *
  4. * Bidirectional List stores special structures (nodes) as its elements.
  5. * Nodes are linked to each other. Node stores abstract user data (i.e. void *).
  6. *
  7. * List can be sorted or unsorted. To sort list user provides special callback
  8. * function to compare two nodes. The list will be sorted
  9. * due to this function return value that indicates "less than",
  10. * "equal", "greater than". Additionally user may provide another callback
  11. * function to free user defined data on list freeing.
  12. */
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include <string.h>
  16. #include "private.h"
  17. #include "faux/list.h"
  18. /** @brief Allocates and initializes new list node instance.
  19. *
  20. * @param [in] data User defined data to store within node.
  21. * @return Newly created list node instance or NULL on error.
  22. */
  23. static faux_list_node_t *faux_list_new_node(void *data) {
  24. faux_list_node_t *node = NULL;
  25. node = faux_zmalloc(sizeof(*node));
  26. assert(node);
  27. if (!node)
  28. return NULL;
  29. // Initialize
  30. node->prev = NULL;
  31. node->next = NULL;
  32. node->data = data;
  33. return node;
  34. }
  35. /** @brief Free list node instance.
  36. *
  37. * @param [in] node List node instance.
  38. */
  39. static void faux_list_free_node(faux_list_node_t *node) {
  40. assert(node);
  41. faux_free(node);
  42. }
  43. /** @brief Gets previous list node.
  44. *
  45. * @param [in] this List node instance.
  46. * @return List node previous in list.
  47. */
  48. faux_list_node_t *faux_list_prev_node(const faux_list_node_t *node) {
  49. assert(node);
  50. if (!node)
  51. return NULL;
  52. return node->prev;
  53. }
  54. /** @brief Gets next list node.
  55. *
  56. * @param [in] this List node instance.
  57. * @return List node next in list.
  58. */
  59. faux_list_node_t *faux_list_next_node(const faux_list_node_t *node) {
  60. assert(node);
  61. if (!node)
  62. return NULL;
  63. return node->next;
  64. }
  65. /** @brief Gets user data from list node.
  66. *
  67. * @param [in] this List node instance.
  68. * @return User data stored within specified list node.
  69. */
  70. void *faux_list_data(const faux_list_node_t *node) {
  71. assert(node);
  72. if (!node)
  73. return NULL;
  74. return node->data;
  75. }
  76. /** @brief Iterate through each list node.
  77. *
  78. * On each call to this function the iterator will change its value.
  79. * Before function using the iterator must be initialised by list head node.
  80. *
  81. * @param [in,out] iter List node ptr used as an iterator.
  82. * @return List node or NULL if list elements are over.
  83. */
  84. faux_list_node_t *faux_list_each_node(faux_list_node_t **iter) {
  85. faux_list_node_t *current_node = *iter;
  86. if (!current_node)
  87. return NULL;
  88. *iter = faux_list_next_node(current_node);
  89. return current_node;
  90. }
  91. /** @brief Iterate through each list node and returns user data.
  92. *
  93. * On each call to this function the iterator will change its value.
  94. * Before function using the iterator must be initialised by list head node.
  95. *
  96. * @param [in,out] iter List node ptr used as an iterator.
  97. * @return User data or NULL if list elements are over.
  98. */
  99. void *faux_list_each(faux_list_node_t **iter) {
  100. faux_list_node_t *current_node = NULL;
  101. if (!*iter)
  102. return NULL;
  103. current_node = faux_list_each_node(iter);
  104. return faux_list_data(current_node);
  105. }
  106. /** @brief Allocate and initialize bidirectional list.
  107. *
  108. * Prototypes for callback functions:
  109. * @code
  110. * int faux_list_compare_fn(const void *first, const void *second);
  111. * void faux_list_free_fn(void *data);
  112. * @endcode
  113. *
  114. * @param [in] compareFn Callback function to compare two user data instances
  115. * to sort list.
  116. * @param [in] freeFn Callback function to free user data.
  117. * @return Newly created bidirectional list or NULL on error.
  118. */
  119. faux_list_t *faux_list_new(faux_list_compare_fn compareFn,
  120. faux_list_free_fn freeFn) {
  121. faux_list_t *list = NULL;
  122. list = faux_zmalloc(sizeof(*list));
  123. assert(list);
  124. if (!list)
  125. return NULL;
  126. // Initialize
  127. list->head = NULL;
  128. list->tail = NULL;
  129. list->compareFn = compareFn;
  130. list->freeFn = freeFn;
  131. list->len = 0;
  132. return list;
  133. }
  134. /** @brief Free bidirectional list
  135. *
  136. * Free all nodes and user data from list and finally
  137. * free the list itself. It uses special callback
  138. * function specified by user (while faux_list_new()) to free the abstract
  139. * user data.
  140. *
  141. * @param [in] list List to free.
  142. */
  143. void faux_list_free(faux_list_t *list) {
  144. faux_list_node_t *iter = NULL;
  145. assert(list);
  146. if (!list)
  147. return;
  148. while ((iter = faux_list_head(list))) {
  149. faux_list_del(list, iter);
  150. }
  151. faux_free(list);
  152. }
  153. /** @brief Gets head of list.
  154. *
  155. * @param [in] list List.
  156. * @return List node first in list.
  157. */
  158. faux_list_node_t *faux_list_head(const faux_list_t *list) {
  159. assert(list);
  160. if (!list)
  161. return NULL;
  162. return list->head;
  163. }
  164. /** @brief Gets tail of list.
  165. *
  166. * @param [in] list List.
  167. * @return List node last in list.
  168. */
  169. faux_list_node_t *faux_list_tail(const faux_list_t *list) {
  170. assert(list);
  171. if (!list)
  172. return NULL;
  173. return list->tail;
  174. }
  175. /** @brief Gets current length of list.
  176. *
  177. * @param [in] list List.
  178. * @return Current length of list.
  179. */
  180. size_t faux_list_len(const faux_list_t *list) {
  181. assert(list);
  182. if (!list)
  183. return 0;
  184. return list->len;
  185. }
  186. /** @brief Generic static function for adding new list nodes.
  187. *
  188. * @param [in] list List to add node to.
  189. * @param [in] data User data for new list node.
  190. * @param [in] uniq - true/false Don't add entry with identical order
  191. * key (when the compareFn() returns 0)
  192. * @param [in] find - true/false Function returns list node if there is
  193. * identical entry. Or NULL if find is false.
  194. * @return Newly added list node.
  195. */
  196. static faux_list_node_t *faux_list_add_generic(faux_list_t *list, void *data,
  197. bool_t uniq, bool_t find) {
  198. faux_list_node_t *node = NULL;
  199. faux_list_node_t *iter = NULL;
  200. assert(list);
  201. assert(data);
  202. if (!list || !data)
  203. return NULL;
  204. node = faux_list_new_node(data);
  205. if (!node)
  206. return NULL;
  207. // Empty list.
  208. if (!list->head) {
  209. list->head = node;
  210. list->tail = node;
  211. list->len++;
  212. return node;
  213. }
  214. // Not sorted list. Add to the tail.
  215. if (!list->compareFn) {
  216. node->prev = list->tail;
  217. node->next = NULL;
  218. list->tail->next = node;
  219. list->tail = node;
  220. list->len++;
  221. return node;
  222. }
  223. // Sorted list.
  224. iter = list->tail;
  225. while (iter) {
  226. int res = list->compareFn(node->data, iter->data);
  227. if (uniq && (res == 0)) {
  228. faux_list_free_node(node);
  229. return (find ? iter : NULL);
  230. }
  231. if (res >= 0) {
  232. node->next = iter->next;
  233. node->prev = iter;
  234. iter->next = node;
  235. if (node->next)
  236. node->next->prev = node;
  237. break;
  238. }
  239. iter = iter->prev;
  240. }
  241. // Insert node into the list head
  242. if (!iter) {
  243. node->next = list->head;
  244. node->prev = NULL;
  245. list->head->prev = node;
  246. list->head = node;
  247. }
  248. if (!node->next)
  249. list->tail = node;
  250. list->len++;
  251. return node;
  252. }
  253. /** @brief Adds user data (not unique) to the list.
  254. *
  255. * The user data is not unique. It means that two equal user data instances
  256. * can be added to the list.
  257. *
  258. * @param [in] list List to add entry to.
  259. * @param [in] data User data.
  260. * @return Newly created list node or NULL on error.
  261. */
  262. faux_list_node_t *faux_list_add(faux_list_t *list, void *data) {
  263. return faux_list_add_generic(list, data, BOOL_FALSE, BOOL_FALSE);
  264. }
  265. /** @brief Adds user data (unique) to the list.
  266. *
  267. * The user data must be unique. It means that two equal user data instances
  268. * can not be added to the list. Function will return NULL if equal user
  269. * data is already in the list.
  270. *
  271. * @param [in] list List to add entry to.
  272. * @param [in] data User data.
  273. * @return Newly created list node or NULL on error.
  274. */
  275. faux_list_node_t *faux_list_add_uniq(faux_list_t *list, void *data) {
  276. return faux_list_add_generic(list, data, BOOL_TRUE, BOOL_FALSE);
  277. }
  278. /** @brief Adds user data (unique) to the list or return equal existent node.
  279. *
  280. * The user data must be unique in this case. Function compares list nodes
  281. * with the new one. If equal node is already in the list then function
  282. * returns this node. Else new unique node will be added to the list.
  283. *
  284. * @param [in] list List to add entry to.
  285. * @param [in] data User data.
  286. * @return Newly created list node, existent equal node or NULL on error.
  287. */
  288. faux_list_node_t *faux_list_add_find(faux_list_t *list, void *data) {
  289. return faux_list_add_generic(list, data, BOOL_TRUE, BOOL_TRUE);
  290. }
  291. /** Takes away list node from the list.
  292. *
  293. * Function removes list node from the list and returns user data
  294. * stored in this node.
  295. *
  296. * @param [in] list List to take away node from.
  297. * @param [in] node List node to take away.
  298. * @return User data from removed node or NULL on error.
  299. */
  300. void *faux_list_takeaway(faux_list_t *list, faux_list_node_t *node) {
  301. void *data = NULL;
  302. assert(list);
  303. assert(node);
  304. if (!list || !node)
  305. return NULL;
  306. if (node->prev)
  307. node->prev->next = node->next;
  308. else
  309. list->head = node->next;
  310. if (node->next)
  311. node->next->prev = node->prev;
  312. else
  313. list->tail = node->prev;
  314. list->len--;
  315. data = faux_list_data(node);
  316. faux_list_free_node(node);
  317. return data;
  318. }
  319. /** @brief Deletes list node from the list.
  320. *
  321. * Functions removes node from the list and free user data memory if
  322. * freeFn callback was defined while list creation. If freeFn callback
  323. * is not defined then function is the same as faux_list_takeaway().
  324. *
  325. * @param [in] list List to delete node from.
  326. * @param [in] node List node to delete.
  327. * @return 0 on success, < 0 on error.
  328. */
  329. int faux_list_del(faux_list_t *list, faux_list_node_t *node) {
  330. void *data = NULL;
  331. assert(list);
  332. assert(node);
  333. if (!list || !node)
  334. return -1;
  335. data = faux_list_takeaway(list, node);
  336. assert(data);
  337. if (!data) // Illegal case
  338. return -1;
  339. if (list->freeFn)
  340. list->freeFn(data);
  341. return 0;
  342. }
  343. /** @brief Search list for matching (match function).
  344. *
  345. * Function iterates through the list and executes special matching user defined
  346. * callback function matchFn for every list node. User can provide "userkey" -
  347. * the data that matchFn can use how it wants. The matchFn is arbitrary
  348. * argument. The userkey argument can be NULL. The function will immediately
  349. * return matched list node. To continue searching the saveptr argument contains
  350. * current iterator. So user can call to faux_list_match_node() for several
  351. * times and gets all matched nodes from list.
  352. *
  353. * Prototype for matchFn callback function:
  354. * @code
  355. * int faux_list_match_fn(const void *key, const void *data);
  356. * @endcode
  357. *
  358. * @param [in] list List.
  359. * @param [in] matchFn User defined matching callback function.
  360. * @param [in] userkey User defined data to use in matchFn function.
  361. * @param [in,out] saveptr Ptr to save iterator.
  362. * @return Matched list node.
  363. */
  364. faux_list_node_t *faux_list_match_node(const faux_list_t *list,
  365. faux_list_match_fn matchFn, const void *userkey,
  366. faux_list_node_t **saveptr) {
  367. faux_list_node_t *iter = NULL;
  368. assert(list);
  369. assert(matchFn);
  370. if (!list || !matchFn || !list->head)
  371. return NULL;
  372. if (saveptr)
  373. iter = *saveptr;
  374. if (!iter)
  375. iter = list->head;
  376. while (iter) {
  377. int res;
  378. faux_list_node_t *node = iter;
  379. iter = faux_list_next_node(iter);
  380. if (saveptr)
  381. *saveptr = iter;
  382. res = matchFn(userkey, faux_list_data(node));
  383. if (!res)
  384. return node;
  385. if (res < 0) // No chances to find match
  386. return NULL;
  387. }
  388. return NULL;
  389. }
  390. /** @brief Search list for matching (match function) and returns user data.
  391. *
  392. * Same as faux_list_match_node() but returns user data structure.
  393. *
  394. * @sa faux_list_match_node()
  395. */
  396. void *faux_list_match(const faux_list_t *list, faux_list_match_fn matchFn,
  397. const void *userkey, faux_list_node_t **saveptr) {
  398. faux_list_node_t *res =
  399. faux_list_match_node(list, matchFn, userkey, saveptr);
  400. if (!res)
  401. return NULL;
  402. return faux_list_data(res);
  403. }
  404. /** @brief Search list for first matching (match function).
  405. *
  406. * Same as faux_list_match_node() but search for the fisrt matching.
  407. * Doesn't use saveptr iterator.
  408. *
  409. * @sa faux_list_match_node()
  410. */
  411. faux_list_node_t *faux_list_find_node(const faux_list_t *list,
  412. faux_list_match_fn matchFn, const void *userkey) {
  413. return faux_list_match_node(list, matchFn, userkey, NULL);
  414. }
  415. /** @brief Search list for first matching (match function) and returns user data.
  416. *
  417. * Same as faux_list_match_node() but returns user data structure and search
  418. * only for the first matching. Doesn't use saveptr iterator.
  419. *
  420. * @sa faux_list_match_node()
  421. */
  422. void *faux_list_find(const faux_list_t *list, faux_list_match_fn matchFn,
  423. const void *userkey) {
  424. return faux_list_match(list, matchFn, userkey, NULL);
  425. }