cs_list.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * cs_str.c
  3. * Copyright (c) 2007-2018 ls
  4. **/
  5. #include "../inc/cs.h"
  6. #include "../inc/cs_list.h"
  7. CS_API void cs_list_init(cs_list_t *list) {
  8. list->head = list->tail = NULL;
  9. }
  10. CS_API void cs_list_add_head(cs_list_t *list, cs_list_node_t *node) {
  11. if (list->head == NULL) {
  12. list->head = list->tail = node;
  13. node->prev = node->next = NULL;
  14. } else {
  15. node->prev = NULL;
  16. node->next = list->head;
  17. list->head->prev = node;
  18. list->head = node;
  19. }
  20. }
  21. CS_API void cs_list_add_tail(cs_list_t *list, cs_list_node_t *node) {
  22. if (list->head == NULL) {
  23. list->head = list->tail = node;
  24. node->prev = node->next = NULL;
  25. } else {
  26. node->prev = list->tail;
  27. node->next = NULL;
  28. list->tail->next = node;
  29. list->tail = node;
  30. }
  31. }
  32. CS_API void cs_list_add_before(
  33. cs_list_t *list, cs_list_node_t *old, cs_list_node_t *node) {
  34. node->next = old;
  35. node->prev = old->prev;
  36. if (list->head == old) {
  37. list->head = node;
  38. }
  39. if (node->prev != NULL) {
  40. node->prev->next = node;
  41. }
  42. if (node->next != NULL) {
  43. node->next->prev = node;
  44. }
  45. }
  46. CS_API void cs_list_add_after(
  47. cs_list_t *list, cs_list_node_t *old, cs_list_node_t *node) {
  48. node->prev = old;
  49. node->next = old->next;
  50. if (list->tail == old) {
  51. list->tail = node;
  52. }
  53. if (node->prev != NULL) {
  54. node->prev->next = node;
  55. }
  56. if (node->next != NULL) {
  57. node->next->prev = node;
  58. }
  59. }
  60. CS_API void cs_list_del(cs_list_t *list, cs_list_node_t *node) {
  61. if (node->prev == NULL) {
  62. list->head = node->next;
  63. } else {
  64. node->prev->next = node->next;
  65. }
  66. if (node->next == NULL) {
  67. list->tail = node->prev;
  68. } else {
  69. node->next->prev = node->prev;
  70. }
  71. }
  72. CS_API cs_list_node_t *cs_list_find_u32(cs_list_t *list, cs_uint_t key) {
  73. cs_list_node_t *node = NULL;
  74. cs_uint_t *u;
  75. cs_list_for_each(list, node) {
  76. u = (cs_uint_t *)node->data;
  77. if (*u == key) {
  78. return (node);
  79. }
  80. }
  81. return (NULL);
  82. }
  83. CS_API cs_list_node_t *cs_list_find_u64(cs_list_t *list, cs_uint64_t key) {
  84. cs_list_node_t *node = NULL;
  85. cs_uint64_t *u;
  86. cs_list_for_each(list, node) {
  87. u = (cs_uint64_t *)node->data;
  88. if (*u == key) {
  89. return (node);
  90. }
  91. }
  92. return (NULL);
  93. }