-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
317 lines (285 loc) · 7.75 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
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
/* private functions */
bool List_is_modifiable(const List *list);
ListNode *List_get_node(const List *list, const void *value);
ListNode *ListNode_new(void *value);
void ListNode_delete(ListNode *node);
/* default comparer */
static int List_default_comparer(const void *a, const void *b) {
if (a == b) {
return 0;
} else {
return -1; // any integer except 0 is ok here.
}
}
/* [private]
* Allocates memory for the new List.
* Returns: A pointer to the allocated memory if it succeed to allocate; otherwise NULL.
*/
List *List_alloc() {
List *list = (List *)malloc(sizeof(List));
return list;
}
/* [public]
* Initializes the new List instance.
* Returns: A pointer to the new list if it succeed to allocate memory for it; otherwise NULL.
*/
List *List_new(VALUE_COMPARER comparer) {
List *list = List_alloc();
if (!list) {
return NULL;
}
list->head = NULL;
list->traversing = 0;
if (comparer) {
list->comparer = comparer;
} else {
list->comparer = List_default_comparer;
}
list->size = 0;
return list;
}
/* [public]
* Deletes the List instance.
* Remarks: This function will do nothing if NULL is given.
*/
void List_delete(List *list) {
if (list) {
assert(List_is_modifiable(list));
for (ListNode *p = list->head; p != NULL;) {
ListNode *next = p->next;
ListNode_delete(p);
p = next;
}
list->head = NULL;
list->comparer = NULL;
list->size = -1;
free(list);
}
}
/* [public]
* Gets the size of the list.
*/
inline const unsigned long List_size(const List *list) {
assert(list != NULL);
return list->size;
}
inline const bool List_is_empty(const List *list) {
assert(list != NULL);
return List_size(list) == 0;
}
/* [public]
* Adds the value to the list.
* Returns: true if succeeded to add; otherwise, false.
*/
bool List_add(List *list, void *value) {
assert(list != NULL);
assert(List_is_modifiable(list));
/* Allocate new node */
ListNode *new = ListNode_new(value);
if (!new) {
return false;
}
/* Add to the list. */
if (list->head) {
ListNode *p = list->head;
while (p->next != NULL) {
p = p->next;
}
p->next = new;
} else {
list->head = new;
}
++list->size;
return true;
}
/* [private]
* Gets the element of the given list which has the value that equals to `value`.
* Returns: The node which has the value that equals to `value` if found; otherwise, NULL.
*/
ListNode *List_get_node(const List *list, const void *value) {
assert(list != NULL);
VALUE_COMPARER comparer = list->comparer;
for (ListNode *p = list->head; p != NULL; p = p->next) {
if (comparer(p->value, value) == 0) {
return p;
}
}
return NULL;
}
/* [public]
* Gets the value from the given list.
* Returns: The value that equals to `value` if found; otherwise, NULL.
*/
void *List_get(const List *list, const void *value) {
ListNode *p = List_get_node(list, value);
if (p) {
return p->value;
}
return NULL;
}
void *List_get_at(const List *list, const unsigned long index) {
assert(list != NULL);
assert(index >= 0);
unsigned long i = 0;
ListNode *p;
for (p = list->head; p != NULL && i < index; p = p->next, ++i) { }
if (p) {
return p->value;
}
return NULL;
}
/* [public]
* Removes the value from the given list.
* Returns: true if succeeded to remove; otherwise, false.
*/
bool List_remove(List *list, void *value) {
assert(list != NULL);
assert(List_is_modifiable(list));
ListNode *prev = NULL, *p;
VALUE_COMPARER comparer = list->comparer;
for (p = list->head; p != NULL; prev = p, p = p->next) {
if (comparer(p->value, value)) {
if (prev) {
prev->next = p->next;
}
ListNode_delete(p);
--list->size;
return true;
}
}
return false;
}
/* [public]
* Removes the value at the given index in the list.
* Returns: true if succeeded to remove; otherwise, false.
*/
bool List_remove_at(List* list, unsigned long index) {
assert(list != NULL);
assert(index >= 0);
assert(List_is_modifiable(list));
unsigned long i = 0;
ListNode *prev = NULL, *p;
for (p = list->head; p != NULL && i < index; prev = p, p = p->next, ++i) { }
if (p != NULL) {
if (prev != NULL) {
prev->next = p->next;
}
ListNode_delete(p);
--list->size;
return true;
}
return false;
}
inline bool List_is_modifiable(const List *list) {
return 0 == list->traversing;
}
/* [public]
* Iterates for each elements in the list.
* Remarks:
* * When the given iterator returns false, iteration will be stopped.
* * While iterating, any functions that make any changes to the list MUST NOT be called.
*/
ListIterator *List_iterator(List *list) {
return ListIter_new(list);
}
/****************************************
* *
* ListNode *
* *
****************************************/
/*
* Allocates memory for the new ListNode.
* Returns: A pointer to the allocated memory if it succeed to allocate; otherwise NULL.
*/
ListNode *ListNode_alloc() {
return (ListNode *)malloc(sizeof(ListNode));
}
/*
* Initializes the new ListNode instance.
* Returns: A pointer to the new list if it succeed to allocate memory for it; otherwise NULL.
*/
ListNode *ListNode_new(void *value) {
ListNode *p = ListNode_alloc();
if (!p) {
return NULL;
}
p->value = value;
p->next = NULL;
return p;
}
/*
* Deletes the ListNode instance.
* Remarks: This function will do nothing if NULL is given.
*/
void ListNode_delete(ListNode *node) {
if (node) {
node->value = NULL;
node->next = NULL;
free(node);
}
}
/****************************************
* *
* ListIterator *
* *
****************************************/
ListIterator *ListIter_alloc() {
ListIterator *p = (ListIterator *)malloc(sizeof(ListIterator));
return p;
}
ListIterator *ListIter_new(List *list) {
assert(list != NULL);
ListIterator *iter = ListIter_alloc();
iter->list = list;
iter->current = NULL;
iter->state = LITST_BEFORE;
return iter;
}
void ListIter_delete(ListIterator *iter) {
if (iter) {
iter->list = NULL;
iter->current = NULL;
iter->state = LITST_UNKNOWN;
free(iter);
}
}
void *ListIter_current(const ListIterator *iter) {
assert(iter != NULL);
if (iter->state == LITST_RUNNING) {
return iter->current->value;
} else {
return NULL;
}
}
bool ListIter_move_next(ListIterator *iter) {
assert(iter != NULL);
switch (iter->state) {
case LITST_BEFORE:
if (iter->list->head) {
iter->current = iter->list->head;
iter->state = LITST_RUNNING;
return true;
} else {
iter->state = LITST_AFTER;
return false;
}
case LITST_RUNNING:
if (iter->current->next) {
iter->current = iter->current->next;
return true;
} else {
iter->current = NULL;
iter->state = LITST_AFTER;
return false;
}
case LITST_AFTER:
return false;
default:
abort(); // invalid state
}
}
/* vim: set et ts=4 sw=4 sts=4: */