-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
339 lines (300 loc) · 8.47 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Copyright 2012-2014 Infinitycoding all rights reserved
This file is part of the universe standart c library.
The universe standart c library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
The universe standart c library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the universe standart c library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Simon Diepold aka. Tdotu <[email protected]>
* @author Andreas Hahn aka. thedrakor
* @copyright GNU Lesser Public License
*/
#include <stdlib.h>
#include <stdint.h>
#include <c/list.h>
/**
* @brief Creates a linked list.
* @return new list
*/
list_t *list_create(void)
{
list_t *list = (list_t *) malloc(sizeof(list_t));
struct list_node *dummy = (struct list_node *) malloc(sizeof(struct list_node));
list->head = dummy;
dummy->next = dummy;
dummy->prev = dummy;
dummy->element = (void *) 0;
return list;
}
/**
* @brief Destroys a list.
* @param list the list to be destroied
*/
void list_destroy(list_t *list)
{
struct list_node *node = list->head->next;
struct list_node *head = list->head;
while (node != head)
{
node = node->next;
free(node);
}
free(list);
return;
}
/**
* @brief Moves all elements between start and end after target.
* @param start Start of the element chain
* @param end end of the element chain
* @param target Target place
*/
void list_splice(struct list_node *start, struct list_node *end, struct list_node *target)
{
start->prev->next = end->next;
end->next->prev = start->prev;
start->prev = target;
end->next = target->next;
target->next->prev = end;
target->next = start;
return;
}
/**
* @brief Adds an element before the dummy element (...|last element|new element|dummy|first entry|...).
* @param list the list
* @param element the element to be added to the list
* @return pointer to the list
*/
list_t *list_push_back(list_t *list, void *element)
{
struct list_node *node = (struct list_node *) malloc(sizeof(struct list_node));
node->element = element;
node->next = node;
node->prev = node;
list_splice(node, node, list->head->prev);
return list;
}
/**
* @brief Adds an element after of the dummy to a list (...|last element|dummy|new element|first entry|...).
* @param list the list
* @param element the element to be added to the list
* @return pointer to the list
*/
list_t *list_push_front(list_t *list, void *element)
{
struct list_node *node = (struct list_node *) malloc(sizeof(struct list_node));
node->element = element;
node->next = node;
node->prev = node;
list_splice(node, node, list->head);
return list;
}
/**
* @brief Internal function which removes a specific node from a list.
* @param node the node to be removed
* @return pointer to the content of the removed element
*/
void *list_remove_node(struct list_node *node)
{
void *element = node->element;
node->prev->next = node->next;
node->next->prev = node->prev;
free(node);
return element;
}
/**
* @brief Removes the element before the dummy (complementary to list_pop_back).
* @param the list
* @pointer to the removed element
*/
void *list_pop_back(list_t *list)
{
struct list_node *last = list->head->prev;
void *element = last->element;
last->prev->next = last->next;
last->next->prev = last->prev;
free(last);
return element;
}
/**
* @brief Removes the element after the dummy (complementary to list_pop_front).
* @param the list
* @pointer to the removed element
*/
void *list_pop_front(list_t *list)
{
struct list_node *first = list->head->next;
void *element = first->element;
first->prev->next = first->next;
first->next->prev = first->prev;
free(first);
return element;
}
void* list_get_by_int(list_t *list, uintptr_t off, int value)
{
struct list_node *node = list->head->next;
struct list_node *head = list->head;
while (node != head)
{
int val1 = *((int*) ((uintptr_t) node->element + off));
if(val1 == value)
{
return node->element;
}
node = node->next;
}
return NULL;
}
struct list_node* list_get_node_by_int(list_t *list, uintptr_t off, int value)
{
struct list_node *node = list->head->next;
struct list_node *head = list->head;
while (node != head)
{
int val1 = *((int*) ((uintptr_t) node->element + off));
if(val1 == value)
{
return node;
}
node = node->next;
}
return NULL;
}
/**
* @brief Counts the number of elements in the given list.
* @brief list the list
* @return number of elements
*/
int list_length(list_t *list)
{
struct list_node *node = list->head->next;
struct list_node *head = list->head;
size_t size = 0;
while (node != head)
{
node = node->next;
size++;
}
return size;
}
/**
* @brief Checks if the given list is empty.
* @param list the list
* @return true if the list is empty, false if the list contains elements
*/
bool list_is_empty(list_t *list)
{
return (list->head == list->head->next);
}
/**
* @breif Creates a new iterator fot a list.
* @param list the list
* @return the new iterator
*/
iterator_t iterator_create(list_t *list)
{
iterator_t new_iterator;
new_iterator.list = list;
new_iterator.current = list->head->next;
return new_iterator;
}
/**
* @brief Inserts an element after the current element which is selected by the iterator.
* @param iterator the iterator
* @param element the element to be inserted
*/
void list_insert_after(iterator_t *it, void *element)
{
struct list_node *node = (struct list_node *) malloc(sizeof(struct list_node));
node->element = element;
node->next = node;
node->prev = node;
list_splice(node, node, it->current);
}
/**
* @brief Inserts an element before the current element which is selected by the iterator.
* @param iterator the iterator
* @param element the element to be inserted
*/
void list_insert_before(iterator_t *it, void *element)
{
struct list_node *node = (struct list_node *) malloc(sizeof(struct list_node));
node->element = element;
node->next = node;
node->prev = node;
list_splice(node, node, it->current->prev);
}
/**
* @brief Get the current element of a list selected by an iterator.
* @param the iterator
* @return the current element
*/
void *list_get_current(iterator_t *it)
{
if(it)
if(it->current)
return it->current->element;
return NULL;
}
/**
* @brief Switches the current element of an iterator to the next element of it's list. (forward)
* @param iterator the iterator
*/
void list_next(iterator_t *it)
{
it->current = it->current->next;
}
/**
* @brief Switches the current element of an iterator to the previous element of it's list.
* @param iterator the iterator
*/
void list_previous(iterator_t *it)
{
it->current = it->current->prev;
}
/**
* @brief Checks if the current element is the last before the dummy element or if the list is empty.
* @return true if it's the last element, false if it's not.
*/
bool list_is_last(iterator_t *it)
{
return (it->current == it->list->head);
}
/**
* @brief Sets the first element after the dummy as current element of an iterator.
* @param iterator the iterator
*/
void list_set_first(iterator_t *it)
{
it->current = it->list->head->next;
}
/**
* @brief Sets the first element before the dummy as current element of an iterator.
* @param iterator the iterator
*/
void list_set_last(iterator_t *it)
{
it->current = it->list->head->prev;
}
/**
* @brief Removes the current element from the list and returns it.
* @param iterator the iterator
* @return the corrent element
*/
void *list_remove(iterator_t *it)
{
void *element = list_get_current(it);
struct list_node *node = it->current;
node->prev->next = node->next;
node->next->prev = node->prev;
it->current = node->next;
free(node);
return element;
}