-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll.c
259 lines (191 loc) · 5.5 KB
/
ll.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
#include "assertf.h"
#include "ll.h"
#include <stdlib.h>
#include <string.h>
LL *ll_new(FreeCallback free_callback, CompareCallback compare_callback) {
LL *ll = calloc(1, sizeof(LL));
ll->compare_callback = compare_callback;
ll->free_callback = free_callback;
return ll;
}
bool int_compare(void *a, void *b) {
return *(int*)a == *(int*)b;
}
bool string_compare(void *a, void *b) {
char *as = *(char**)a;
char *bs = *(char**)b;
return strncmp(as, bs, strlen(as)) == 0;
}
void ll_add(LL *ll, void *data, size_t data_size) {
assertf(ll != NULL, "ll is NULL");
LLNode *node = malloc(sizeof(LLNode));
node->next = NULL;
node->data = NULL;
if (data != NULL) {
if (data_size == 0) {
node->data = data;
} else {
node->data = malloc(data_size);
memcpy(node->data, data, data_size);
}
}
if (ll->head == NULL) {
ll->head = node;
ll->tail = node;
} else {
ll->tail->next = node;
ll->tail = ll->tail->next;
}
ll->count++;
}
void ll_add_i(LL *ll, int i) {
ll_add(ll, &i, sizeof(int));
}
void ll_add_s(LL *ll, char *s) {
ll_add(ll, s, strlen(s) + 1);
}
void ll_remove_by_index(LL *ll, size_t index) {
assertf(ll != NULL, "ll is NULL");
assertf(index < ll->count, "index %ld out of range. current list size is %ld", index, ll->count);
LLNode *slow = NULL;
LLNode *fast = ll->head;
for (size_t i = 0; i < index; ++i) {
slow = fast;
fast = fast->next;
}
// middle
if (slow != NULL && fast != NULL) {
slow->next = fast->next;
}
// if it's the first item, update the head
if (index == 0) {
ll->head = ll->head->next;
}
// if it's the last item update the tail. It also works when the last is also the first one
// meaning a list with only 1 item. the slow will be null then we say that tail will be null.
if (index + 1 == ll->count) {
ll->tail = slow;
}
if (ll->free_callback != NULL) {
ll->free_callback(fast->data);
} else {
free(fast->data);
}
free(fast);
ll->count--;
}
void ll_remove_by_value(LL *ll, void *data) {
assertf(ll != NULL, "ll is NULL");
assertf(ll->compare_callback != NULL, "you should have a compare callback when removing by value");
assertf(ll->count > 0, "there is not value inside the list");
LLNode *slow = NULL;
LLNode *fast = ll->head;
bool found = false;
size_t index = 0;
// TODO: what if it's the last node?
while (fast != NULL) {
if (ll->compare_callback(fast->data, data)) {
found = true;
break;
}
slow = fast;
fast = fast->next;
++index;
}
if (!found) return;
// middle
if (slow != NULL && fast != NULL) {
slow->next = fast->next;
}
// if it's the first item, update the head
if (index == 0) {
ll->head = ll->head->next;
}
// if it's the last item update the tail. It also works when the last is also the first one
// meaning a list with only 1 item. the slow will be null then we say that tail will be null.
if (index + 1 == ll->count) {
ll->tail = slow;
}
if (ll->free_callback != NULL) {
ll->free_callback(fast->data);
} else {
free(fast->data);
}
free(fast);
ll->count--;
}
void ll_remove_by_value_i(LL *ll, int i) {
ll_remove_by_value(ll, &i);
}
void *ll_find_by_index(LL *ll, size_t index) {
assertf(ll != NULL, "ll is NULL");
assertf(index < ll->count, "index %ld out of range. current list size is %ld", index, ll->count);
LLNode *current = ll->head;
for (size_t i = 0; i < index; i++) {
current = current->next;
}
return current->data;
}
void *ll_find_by_value(LL *ll, void *data) {
assertf(ll != NULL, "ll is NULL");
assertf(ll->compare_callback != NULL, "you need to have a compare_callback to find by value");
if (ll->count == 0) return NULL;
LLNode *current = ll->head;
while (current != NULL) {
if (ll->compare_callback(current->data, data)) {
return current->data;
}
current = current->next;
}
return NULL;
}
LLIter ll_iter(LL *ll) {
assertf(ll != NULL, "ll cannot be null");
return (LLIter){
.ll = ll,
.current = ll->head,
.index = 0
};
}
bool ll_iter_has(LLIter *iter) {
assertf(iter != NULL, "iter cannot be null");
return iter->index < iter->ll->count;
}
LLIterItem ll_iter_consume(LLIter *iter) {
assertf(iter != NULL, "iter cannot be null");
assertf(iter->current != NULL, "you reached the end of this iteration");
LLNode *node = iter->current;
LLIterItem item = {
.data = node->data,
.index = iter->index
};
iter->current = iter->current->next;
iter->index++;
return item;
}
void ll_iter_flush(LLIter *iter) {
assertf(iter != NULL, "iter cannot be null");
iter->current = iter->ll->head;
iter->index = 0;
}
void ll_free(LL *ll) {
if (ll == NULL) return;
if (ll->head == NULL) {
free(ll);
return;
}
LLNode *current = ll->head;
while (current != NULL) {
LLNode *next = current->next;
if (current->data != NULL) {
if (ll->free_callback == NULL) {
free(current->data);
} else {
ll->free_callback(current->data);
}
}
free(current);
current = next;
}
free(ll);
}