2020-08-11 09:10:23 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
/*
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright (C) 2009-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ulist.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "uenumimp.h"
|
|
|
|
|
|
|
|
typedef struct UListNode UListNode;
|
|
|
|
struct UListNode {
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
UListNode *next;
|
|
|
|
UListNode *previous;
|
|
|
|
|
|
|
|
/* When data is created with uprv_malloc, needs to be freed during deleteList function. */
|
|
|
|
UBool forceDelete;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UList {
|
|
|
|
UListNode *curr;
|
|
|
|
UListNode *head;
|
|
|
|
UListNode *tail;
|
|
|
|
|
|
|
|
int32_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ulist_addFirstItem(UList *list, UListNode *newItem);
|
|
|
|
|
|
|
|
U_CAPI UList *U_EXPORT2 ulist_createEmptyList(UErrorCode *status) {
|
2023-05-23 00:05:01 +00:00
|
|
|
UList *newList = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newList = (UList *)uprv_malloc(sizeof(UList));
|
2023-05-23 00:05:01 +00:00
|
|
|
if (newList == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
newList->curr = nullptr;
|
|
|
|
newList->head = nullptr;
|
|
|
|
newList->tail = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
newList->size = 0;
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function called by addItemEndList or addItemBeginList when the first item is added to the list.
|
|
|
|
* This function properly sets the pointers for the first item added.
|
|
|
|
*/
|
|
|
|
static void ulist_addFirstItem(UList *list, UListNode *newItem) {
|
2023-05-23 00:05:01 +00:00
|
|
|
newItem->next = nullptr;
|
|
|
|
newItem->previous = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
list->head = newItem;
|
|
|
|
list->tail = newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ulist_removeItem(UList *list, UListNode *p) {
|
2023-05-23 00:05:01 +00:00
|
|
|
if (p->previous == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
// p is the list head.
|
|
|
|
list->head = p->next;
|
|
|
|
} else {
|
|
|
|
p->previous->next = p->next;
|
|
|
|
}
|
2023-05-23 00:05:01 +00:00
|
|
|
if (p->next == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
// p is the list tail.
|
|
|
|
list->tail = p->previous;
|
|
|
|
} else {
|
|
|
|
p->next->previous = p->previous;
|
|
|
|
}
|
|
|
|
if (p == list->curr) {
|
|
|
|
list->curr = p->next;
|
|
|
|
}
|
|
|
|
--list->size;
|
|
|
|
if (p->forceDelete) {
|
|
|
|
uprv_free(p->data);
|
|
|
|
}
|
|
|
|
uprv_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_addItemEndList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
|
2023-05-23 00:05:01 +00:00
|
|
|
UListNode *newItem = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if (U_FAILURE(*status) || list == nullptr || data == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
|
2023-05-23 00:05:01 +00:00
|
|
|
if (newItem == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newItem->data = (void *)(data);
|
|
|
|
newItem->forceDelete = forceDelete;
|
|
|
|
|
|
|
|
if (list->size == 0) {
|
|
|
|
ulist_addFirstItem(list, newItem);
|
|
|
|
} else {
|
2023-05-23 00:05:01 +00:00
|
|
|
newItem->next = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
newItem->previous = list->tail;
|
|
|
|
list->tail->next = newItem;
|
|
|
|
list->tail = newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_addItemBeginList(UList *list, const void *data, UBool forceDelete, UErrorCode *status) {
|
2023-05-23 00:05:01 +00:00
|
|
|
UListNode *newItem = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if (U_FAILURE(*status) || list == nullptr || data == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newItem = (UListNode *)uprv_malloc(sizeof(UListNode));
|
2023-05-23 00:05:01 +00:00
|
|
|
if (newItem == nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
if (forceDelete) {
|
|
|
|
uprv_free((void *)data);
|
|
|
|
}
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newItem->data = (void *)(data);
|
|
|
|
newItem->forceDelete = forceDelete;
|
|
|
|
|
|
|
|
if (list->size == 0) {
|
|
|
|
ulist_addFirstItem(list, newItem);
|
|
|
|
} else {
|
2023-05-23 00:05:01 +00:00
|
|
|
newItem->previous = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
newItem->next = list->head;
|
|
|
|
list->head->previous = newItem;
|
|
|
|
list->head = newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2 ulist_containsString(const UList *list, const char *data, int32_t length) {
|
2023-05-23 00:05:01 +00:00
|
|
|
if (list != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
const UListNode *pointer;
|
2023-05-23 00:05:01 +00:00
|
|
|
for (pointer = list->head; pointer != nullptr; pointer = pointer->next) {
|
2020-08-11 09:10:23 +00:00
|
|
|
if (length == (int32_t)uprv_strlen((const char *)pointer->data)) {
|
|
|
|
if (uprv_memcmp(data, pointer->data, length) == 0) {
|
2022-10-28 06:11:55 +00:00
|
|
|
return true;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 06:11:55 +00:00
|
|
|
return false;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2 ulist_removeString(UList *list, const char *data) {
|
2023-05-23 00:05:01 +00:00
|
|
|
if (list != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
UListNode *pointer;
|
2023-05-23 00:05:01 +00:00
|
|
|
for (pointer = list->head; pointer != nullptr; pointer = pointer->next) {
|
2020-08-11 09:10:23 +00:00
|
|
|
if (uprv_strcmp(data, (const char *)pointer->data) == 0) {
|
|
|
|
ulist_removeItem(list, pointer);
|
|
|
|
// Remove only the first occurrence, like Java LinkedList.remove(Object).
|
2022-10-28 06:11:55 +00:00
|
|
|
return true;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 06:11:55 +00:00
|
|
|
return false;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void *U_EXPORT2 ulist_getNext(UList *list) {
|
2023-05-23 00:05:01 +00:00
|
|
|
UListNode *curr = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if (list == nullptr || list->curr == nullptr) {
|
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curr = list->curr;
|
|
|
|
list->curr = curr->next;
|
|
|
|
|
|
|
|
return curr->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2 ulist_getListSize(const UList *list) {
|
2023-05-23 00:05:01 +00:00
|
|
|
if (list != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
return list->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_resetList(UList *list) {
|
2023-05-23 00:05:01 +00:00
|
|
|
if (list != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
list->curr = list->head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_deleteList(UList *list) {
|
2023-05-23 00:05:01 +00:00
|
|
|
UListNode *listHead = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
|
2023-05-23 00:05:01 +00:00
|
|
|
if (list != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
listHead = list->head;
|
2023-05-23 00:05:01 +00:00
|
|
|
while (listHead != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
UListNode *listPointer = listHead->next;
|
|
|
|
|
|
|
|
if (listHead->forceDelete) {
|
|
|
|
uprv_free(listHead->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
uprv_free(listHead);
|
|
|
|
listHead = listPointer;
|
|
|
|
}
|
|
|
|
uprv_free(list);
|
2023-05-23 00:05:01 +00:00
|
|
|
list = nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_close_keyword_values_iterator(UEnumeration *en) {
|
2023-05-23 00:05:01 +00:00
|
|
|
if (en != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
ulist_deleteList((UList *)(en->context));
|
|
|
|
uprv_free(en);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2 ulist_count_keyword_values(UEnumeration *en, UErrorCode *status) {
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ulist_getListSize((UList *)(en->context));
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI const char * U_EXPORT2 ulist_next_keyword_value(UEnumeration *en, int32_t *resultLength, UErrorCode *status) {
|
|
|
|
const char *s;
|
|
|
|
if (U_FAILURE(*status)) {
|
2023-05-23 00:05:01 +00:00
|
|
|
return nullptr;
|
2020-08-11 09:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = (const char *)ulist_getNext((UList *)(en->context));
|
2023-05-23 00:05:01 +00:00
|
|
|
if (s != nullptr && resultLength != nullptr) {
|
2020-08-11 09:10:23 +00:00
|
|
|
*resultLength = static_cast<int32_t>(uprv_strlen(s));
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2 ulist_reset_keyword_values_iterator(UEnumeration *en, UErrorCode *status) {
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
ulist_resetList((UList *)(en->context));
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UList * U_EXPORT2 ulist_getListFromEnum(UEnumeration *en) {
|
|
|
|
return (UList *)(en->context);
|
|
|
|
}
|