20 #include <winpr/config.h>
22 #include <winpr/collections.h>
23 #include <winpr/assert.h>
25 typedef struct s_wLinkedListItem wLinkedListNode;
27 struct s_wLinkedListItem
30 wLinkedListNode* prev;
31 wLinkedListNode* next;
38 wLinkedListNode* head;
39 wLinkedListNode* tail;
40 wLinkedListNode* current;
59 size_t LinkedList_Count(wLinkedList* list)
69 void* LinkedList_First(wLinkedList* list)
73 return list->head->value;
82 void* LinkedList_Last(wLinkedList* list)
86 return list->tail->value;
99 BOOL LinkedList_Contains(wLinkedList* list,
const void* value)
101 wLinkedListNode* item = NULL;
102 OBJECT_EQUALS_FN keyEquals = NULL;
109 keyEquals = list->object.fnObjectEquals;
113 if (keyEquals(item->value, value))
119 return (item) ? TRUE : FALSE;
122 static wLinkedListNode* LinkedList_FreeNode(wLinkedList* list, wLinkedListNode* node)
124 wLinkedListNode* next = NULL;
125 wLinkedListNode* prev = NULL;
138 if (node == list->head)
139 list->head = node->next;
141 if (node == list->tail)
142 list->tail = node->prev;
144 if (list->object.fnObjectUninit)
145 list->object.fnObjectUninit(node);
147 if (list->object.fnObjectFree)
148 list->object.fnObjectFree(node);
159 void LinkedList_Clear(wLinkedList* list)
161 wLinkedListNode* node = NULL;
169 node = LinkedList_FreeNode(list, node);
171 list->head = list->tail = NULL;
175 static wLinkedListNode* LinkedList_Create(wLinkedList* list,
const void* value)
177 wLinkedListNode* node = NULL;
180 node = (wLinkedListNode*)calloc(1,
sizeof(wLinkedListNode));
185 if (list->object.fnObjectNew)
186 node->value = list->object.fnObjectNew(value);
195 node->value = cnv.pv;
198 if (list->object.fnObjectInit)
199 list->object.fnObjectInit(node);
207 BOOL LinkedList_AddFirst(wLinkedList* list,
const void* value)
209 wLinkedListNode* node = LinkedList_Create(list, value);
216 list->tail = list->head = node;
220 list->head->prev = node;
221 node->next = list->head;
233 BOOL LinkedList_AddLast(wLinkedList* list,
const void* value)
235 wLinkedListNode* node = LinkedList_Create(list, value);
242 list->head = list->tail = node;
246 list->tail->next = node;
247 node->prev = list->tail;
259 BOOL LinkedList_Remove(wLinkedList* list,
const void* value)
261 wLinkedListNode* node = NULL;
262 OBJECT_EQUALS_FN keyEquals = NULL;
265 keyEquals = list->object.fnObjectEquals;
270 if (keyEquals(node->value, value))
272 LinkedList_FreeNode(list, node);
286 void LinkedList_RemoveFirst(wLinkedList* list)
290 LinkedList_FreeNode(list, list->head);
297 void LinkedList_RemoveLast(wLinkedList* list)
301 LinkedList_FreeNode(list, list->tail);
308 void LinkedList_Enumerator_Reset(wLinkedList* list)
312 list->current = list->head;
319 void* LinkedList_Enumerator_Current(wLinkedList* list)
326 return list->current->value;
335 BOOL LinkedList_Enumerator_MoveNext(wLinkedList* list)
340 else if (list->current)
341 list->current = list->current->next;
349 static BOOL default_equal_function(
const void* objA,
const void* objB)
358 wLinkedList* LinkedList_New(
void)
360 wLinkedList* list = NULL;
361 list = (wLinkedList*)calloc(1,
sizeof(wLinkedList));
365 list->object.fnObjectEquals = default_equal_function;
371 void LinkedList_Free(wLinkedList* list)
375 LinkedList_Clear(list);
380 wObject* LinkedList_Object(wLinkedList* list)
384 return &list->object;
This struct contains function pointer to initialize/free objects.