UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
LinkedList.cpp
Go to the documentation of this file.
1/**************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 * Copyright (c) 2012 France Telecom All rights reserved.
6 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
7 * Redistribution only with this Copyright remark. Last modified: 2024-03-06
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * - Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * - Neither name of Intel Corporation nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
29 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 **************************************************************************/
43#include "LinkedList.hpp"
44
46constexpr int FREELISTSIZE{100};
47
49#include <cassert>
51
52namespace {
60int freeListNode(ListNode* node, LinkedList* list) {
61 assert(list != NULL);
62
63 return FreeListFree(&list->freeNodeList, node);
64}
65
75 void* item,
77 LinkedList* list) {
78 ListNode* temp = NULL;
79
80 assert(list != NULL);
81
82 temp = (ListNode*)FreeListAlloc(&list->freeNodeList);
83 if (temp) {
84 temp->prev = NULL;
85 temp->next = NULL;
86 temp->item = item;
87 }
88
89 return temp;
90}
91
93} // anonymous namespace
94
95int ListInit(LinkedList* list, cmp_routine cmp_func, free_function free_func) {
96 int retCode = 0;
97
98 assert(list != NULL);
99
100 if (!list)
101 return EINVAL;
102 list->size = 0;
103 list->cmp_func = cmp_func;
104 list->free_func = free_func;
105 retCode = FreeListInit(&list->freeNodeList, sizeof(ListNode), FREELISTSIZE);
106
107 assert(retCode == 0);
108
109 list->head.item = NULL;
110 list->head.next = &list->tail;
111 list->head.prev = NULL;
112 list->tail.item = NULL;
113 list->tail.prev = &list->head;
114 list->tail.next = NULL;
115
116 return retCode;
117}
118
119ListNode* ListAddHead(LinkedList* list, void* item) {
120 assert(list != NULL);
121
122 if (list == NULL)
123 return NULL;
124
125 return ListAddAfter(list, item, &list->head);
126}
127
128ListNode* ListAddTail(LinkedList* list, void* item) {
129 assert(list != NULL);
130
131 if (!list)
132 return NULL;
133
134 return ListAddBefore(list, item, &list->tail);
135}
136
137ListNode* ListAddAfter(LinkedList* list, void* item, ListNode* bnode) {
138 ListNode* newNode = NULL;
139
140 assert(list != NULL);
141
142 if (!list || !bnode)
143 return NULL;
144 newNode = CreateListNode(item, list);
145 if (newNode) {
146 ListNode* temp = bnode->next;
147
148 bnode->next = newNode;
149 newNode->prev = bnode;
150 newNode->next = temp;
151 temp->prev = newNode;
152 list->size++;
153
154 return newNode;
155 }
156
157 return NULL;
158}
159
160ListNode* ListAddBefore(LinkedList* list, void* item, ListNode* anode) {
161 ListNode* newNode = NULL;
162
163 assert(list != NULL);
164
165 if (!list || !anode)
166 return NULL;
167 newNode = CreateListNode(item, list);
168 if (newNode) {
169 ListNode* temp = anode->prev;
170
171 anode->prev = newNode;
172 newNode->next = anode;
173 newNode->prev = temp;
174 temp->next = newNode;
175 list->size++;
176
177 return newNode;
178 }
179
180 return NULL;
181}
182
183void* ListDelNode(LinkedList* list, ListNode* dnode, int freeItem) {
184 void* temp;
185
186 assert(list != NULL);
187 assert(dnode != &list->head);
188 assert(dnode != &list->tail);
189
190 if (!list || dnode == &list->head || dnode == &list->tail || !dnode)
191 return NULL;
192 temp = dnode->item;
193 dnode->prev->next = dnode->next;
194 dnode->next->prev = dnode->prev;
195 freeListNode(dnode, list);
196 list->size--;
197 if (freeItem && list->free_func) {
198 list->free_func(temp);
199 temp = NULL;
200 }
201
202 return temp;
203}
204
205int ListDestroy(LinkedList* list, int freeItem) {
206 ListNode* dnode = NULL;
207 ListNode* temp = NULL;
208
209 if (!list)
210 return EINVAL;
211
212 for (dnode = list->head.next; dnode != &list->tail;) {
213 temp = dnode->next;
214 ListDelNode(list, dnode, freeItem);
215 dnode = temp;
216 }
217 list->size = 0;
219
220 return 0;
221}
222
224 assert(list != NULL);
225
226 if (!list)
227 return NULL;
228
229 if (!list->size)
230 return NULL;
231 else
232 return list->head.next;
233}
234
236 assert(list != NULL);
237
238 if (!list)
239 return NULL;
240
241 if (!list->size)
242 return NULL;
243 else
244 return list->tail.prev;
245}
246
248 assert(list != NULL);
249 assert(node != NULL);
250
251 if (!list || !node)
252 return NULL;
253 if (node->next == &list->tail)
254 return NULL;
255 else
256 return node->next;
257}
258
260 assert(list != NULL);
261 assert(node != NULL);
262
263 if (!list || !node)
264 return NULL;
265
266 if (node->prev == &list->head)
267 return NULL;
268 else
269 return node->prev;
270}
271
272ListNode* ListFind(LinkedList* list, ListNode* start, void* item) {
273 ListNode* finger = NULL;
274
275 if (!list)
276 return NULL;
277 if (!start)
278 start = &list->head;
279
280 assert(start);
281
282 finger = start->next;
283
284 assert(finger);
285
286 while (finger != &list->tail) {
287 if (list->cmp_func) {
288 if (list->cmp_func(item, finger->item))
289 return finger;
290 } else {
291 if (item == finger->item)
292 return finger;
293 }
294 finger = finger->next;
295 }
296
297 return NULL;
298}
299
300long ListSize(LinkedList* list) {
301 assert(list != NULL);
302
303 if (!list)
304 return EINVAL;
305
306 return list->size;
307}
int FreeListInit(FreeList *free_list, size_t elementSize, int maxFreeListLength)
Initializes Free List.
Definition FreeList.cpp:50
int FreeListFree(FreeList *free_list, void *element)
Returns an item to the Free List.
Definition FreeList.cpp:83
void * FreeListAlloc(FreeList *free_list)
Allocates chunk of set size.
Definition FreeList.cpp:64
int FreeListDestroy(FreeList *free_list)
Releases the resources stored with the free list.
Definition FreeList.cpp:103
long ListSize(LinkedList *list)
Returns the size of the list.
int ListDestroy(LinkedList *list, int freeItem)
Removes all memory associated with list nodes. Does not free LinkedList *list.
ListNode * ListPrev(LinkedList *list, ListNode *node)
Returns the previous item in the list.
void * ListDelNode(LinkedList *list, ListNode *dnode, int freeItem)
Removes a node from the list. The memory for the node is freed.
ListNode * ListAddBefore(LinkedList *list, void *item, ListNode *anode)
Adds a node before the specified node. Node gets added immediately before anode.
ListNode * ListNext(LinkedList *list, ListNode *node)
Returns the next item in the list.
ListNode * ListAddAfter(LinkedList *list, void *item, ListNode *bnode)
Adds a node after the specified node. Node gets added immediately after bnode.
ListNode * ListHead(LinkedList *list)
Returns the head of the list.
ListNode * ListAddHead(LinkedList *list, void *item)
Adds a node to the head of the list. Node gets immediately after list head.
ListNode * ListAddTail(LinkedList *list, void *item)
Adds a node to the tail of the list. Node gets added immediately before list.tail.
ListNode * ListTail(LinkedList *list)
Returns the tail of the list.
ListNode * ListFind(LinkedList *list, ListNode *start, void *item)
Finds the specified item in the list.
int ListInit(LinkedList *list, cmp_routine cmp_func, free_function free_func)
Initializes LinkedList. Must be called first and only once for List.
constexpr int FREELISTSIZE
Size of a free list.
Manage a linked list (for internal use only).
cmp_routine cmp_func
compare function to use
ListNode head
head, first item is stored at: head->next
free_function free_func
free function to use
FreeList freeNodeList
free list to use
long size
size of list
void(* free_function)(void *arg)
Function for freeing list items.
ListNode tail
tail, last item is stored at: tail->prev
int(* cmp_routine)(void *itemA, void *itemB)
Function for comparing list items. Returns 1 if itemA==itemB.
Linked list node. Stores generic item and pointers to next and prev.
Linked list (no protection).
int freeListNode(ListNode *node, LinkedList *list)
Free list node.
ListNode * CreateListNode(void *item, LinkedList *list)
Dynamically creates a list node.