ulist.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 2009-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ******************************************************************************
  8. */
  9. #include "ulist.h"
  10. #include "cmemory.h"
  11. #include "cstring.h"
  12. #include "uenumimp.h"
  13. typedef struct UListNode UListNode;
  14. struct UListNode {
  15. void *data;
  16. UListNode *next;
  17. UListNode *previous;
  18. /* When data is created with uprv_malloc, needs to be freed during deleteList function. */
  19. UBool forceDelete;
  20. };
  21. struct UList {
  22. UListNode *curr;
  23. UListNode *head;
  24. UListNode *tail;
  25. int32_t size;
  26. };
  27. static void ulist_addFirstItem(UList *list, UListNode *newItem);
  28. U_CAPI UList *U_EXPORT2 ulist_createEmptyList(UErrorCode *status) {
  29. UList *newList = nullptr;
  30. if (U_FAILURE(*status)) {
  31. return nullptr;
  32. }
  33. newList = (UList *)uprv_malloc(sizeof(UList));
  34. if (newList == nullptr) {
  35. *status = U_MEMORY_ALLOCATION_ERROR;
  36. return nullptr;
  37. }
  38. newList->curr = nullptr;
  39. newList->head = nullptr;
  40. newList->tail = nullptr;
  41. newList->size = 0;
  42. return newList;
  43. }
  44. /*
  45. * Function called by addItemEndList or addItemBeginList when the first item is added to the list.
  46. * This function properly sets the pointers for the first item added.
  47. */
  48. static void ulist_addFirstItem(UList *list, UListNode *newItem) {
  49. newItem->next = nullptr;
  50. newItem->previous = nullptr;
  51. list->head = newItem;
  52. list->tail = newItem;
  53. }
  54. static void ulist_removeItem(UList *list, UListNode *p) {
  55. if (p->previous == nullptr) {
  56. // p is the list head.
  57. list->head = p->next;
  58. } else {
  59. p->previous->next = p->next;
  60. }
  61. if (p->next == nullptr) {
  62. // p is the list tail.
  63. list->tail = p->previous;
  64. } else {
  65. p->next->previous = p->previous;
  66. }
  67. if (p == list->curr) {
  68. list->curr = p->next;
  69. }
  70. --list->size;
  71. if (p->forceDelete) {
  72. uprv_free(p->data);
  73. }
  74. uprv_free(p);
  75. }
  76. U_CAPI void U_EXPORT2 ulist_addItemEndList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
  77. UListNode *newItem = nullptr;
  78. if (U_FAILURE(*status) || list == nullptr || data == nullptr) {
  79. if (forceDelete) {
  80. uprv_free((void *)data);
  81. }
  82. return;
  83. }
  84. newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
  85. if (newItem == nullptr) {
  86. if (forceDelete) {
  87. uprv_free((void *)data);
  88. }
  89. *status = U_MEMORY_ALLOCATION_ERROR;
  90. return;
  91. }
  92. newItem->data = (void *)(data);
  93. newItem->forceDelete = forceDelete;
  94. if (list->size == 0) {
  95. ulist_addFirstItem(list, newItem);
  96. } else {
  97. newItem->next = nullptr;
  98. newItem->previous = list->tail;
  99. list->tail->next = newItem;
  100. list->tail = newItem;
  101. }
  102. list->size++;
  103. }
  104. U_CAPI void U_EXPORT2 ulist_addItemBeginList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
  105. UListNode *newItem = nullptr;
  106. if (U_FAILURE(*status) || list == nullptr || data == nullptr) {
  107. if (forceDelete) {
  108. uprv_free((void *)data);
  109. }
  110. return;
  111. }
  112. newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
  113. if (newItem == nullptr) {
  114. if (forceDelete) {
  115. uprv_free((void *)data);
  116. }
  117. *status = U_MEMORY_ALLOCATION_ERROR;
  118. return;
  119. }
  120. newItem->data = (void *)(data);
  121. newItem->forceDelete = forceDelete;
  122. if (list->size == 0) {
  123. ulist_addFirstItem(list, newItem);
  124. } else {
  125. newItem->previous = nullptr;
  126. newItem->next = list->head;
  127. list->head->previous = newItem;
  128. list->head = newItem;
  129. }
  130. list->size++;
  131. }
  132. U_CAPI UBool U_EXPORT2 ulist_containsString(const UList *list, const char *data, int32_t length) {
  133. if (list != nullptr) {
  134. const UListNode *pointer;
  135. for (pointer = list->head; pointer != nullptr; pointer = pointer->next) {
  136. if (length == (int32_t)uprv_strlen((const char *)pointer->data)) {
  137. if (uprv_memcmp(data, pointer->data, length) == 0) {
  138. return true;
  139. }
  140. }
  141. }
  142. }
  143. return false;
  144. }
  145. U_CAPI UBool U_EXPORT2 ulist_removeString(UList *list, const char *data) {
  146. if (list != nullptr) {
  147. UListNode *pointer;
  148. for (pointer = list->head; pointer != nullptr; pointer = pointer->next) {
  149. if (uprv_strcmp(data, (const char *)pointer->data) == 0) {
  150. ulist_removeItem(list, pointer);
  151. // Remove only the first occurrence, like Java LinkedList.remove(Object).
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. U_CAPI void *U_EXPORT2 ulist_getNext(UList *list) {
  159. UListNode *curr = nullptr;
  160. if (list == nullptr || list->curr == nullptr) {
  161. return nullptr;
  162. }
  163. curr = list->curr;
  164. list->curr = curr->next;
  165. return curr->data;
  166. }
  167. U_CAPI int32_t U_EXPORT2 ulist_getListSize(const UList *list) {
  168. if (list != nullptr) {
  169. return list->size;
  170. }
  171. return -1;
  172. }
  173. U_CAPI void U_EXPORT2 ulist_resetList(UList *list) {
  174. if (list != nullptr) {
  175. list->curr = list->head;
  176. }
  177. }
  178. U_CAPI void U_EXPORT2 ulist_deleteList(UList *list) {
  179. UListNode *listHead = nullptr;
  180. if (list != nullptr) {
  181. listHead = list->head;
  182. while (listHead != nullptr) {
  183. UListNode *listPointer = listHead->next;
  184. if (listHead->forceDelete) {
  185. uprv_free(listHead->data);
  186. }
  187. uprv_free(listHead);
  188. listHead = listPointer;
  189. }
  190. uprv_free(list);
  191. list = nullptr;
  192. }
  193. }
  194. U_CAPI void U_EXPORT2 ulist_close_keyword_values_iterator(UEnumeration *en) {
  195. if (en != nullptr) {
  196. ulist_deleteList((UList *)(en->context));
  197. uprv_free(en);
  198. }
  199. }
  200. U_CAPI int32_t U_EXPORT2 ulist_count_keyword_values(UEnumeration *en, UErrorCode *status) {
  201. if (U_FAILURE(*status)) {
  202. return -1;
  203. }
  204. return ulist_getListSize((UList *)(en->context));
  205. }
  206. U_CAPI const char * U_EXPORT2 ulist_next_keyword_value(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
  207. const char *s;
  208. if (U_FAILURE(*status)) {
  209. return nullptr;
  210. }
  211. s = (const char *)ulist_getNext((UList *)(en->context));
  212. if (s != nullptr && resultLength != nullptr) {
  213. *resultLength = static_cast<int32_t>(uprv_strlen(s));
  214. }
  215. return s;
  216. }
  217. U_CAPI void U_EXPORT2 ulist_reset_keyword_values_iterator(UEnumeration *en, UErrorCode *status) {
  218. if (U_FAILURE(*status)) {
  219. return ;
  220. }
  221. ulist_resetList((UList *)(en->context));
  222. }
  223. U_CAPI UList * U_EXPORT2 ulist_getListFromEnum(UEnumeration *en) {
  224. return (UList *)(en->context);
  225. }