-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclist.c
354 lines (292 loc) · 7.06 KB
/
clist.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "clist.h"
struct clist_node_t {
void *__ptr;
struct clist_node_t *__prev;
struct clist_node_t *__next;
};
struct clist_t {
struct clist_node_t *__front;
struct clist_node_t *__rear;
size_t __obj_size;
size_t __list_size;
};
static const char* __clist_error_msg[__MAX_ERR_SIZE] = {
"success",
"got null ptr",
"memory allocation failed",
"index out of bound"
};
const char *
clist_get_error_info(
const clist_error_t err
) {
return __clist_error_msg[err];
}
static clist_node_t*
__clist_create_node(
const void *obj,
size_t obj_size
) {
clist_node_t *node = (clist_node_t *)malloc(sizeof(clist_node_t));
if(node == NULL) return NULL;
node->__prev = NULL;
node->__next = NULL;
node->__ptr = malloc(obj_size);
if(node->__ptr == NULL) {
free(node);
return NULL;
}
memcpy(node->__ptr, obj, obj_size);
return node;
}
void*
__clist_get_raw_ptr(
const clist_node_t *n
) {
if(n == NULL) return NULL;
return n->__ptr;
}
clist_t*
clist_init(
const size_t object_size
) {
clist_t *l = (clist_t*)malloc(sizeof(clist_t));
if(l == NULL) return NULL;
l->__front = l->__rear = NULL;
l->__list_size = 0;
l->__obj_size = object_size;
return l;
}
clist_error_t
clist_insert_front(
clist_t *l,
const void *obj
) {
if(l == NULL || obj == NULL) return CLIST_GOT_NULL_PTR;
if(l->__front == NULL || l->__rear == NULL) {
l->__front = l->__rear = __clist_create_node(obj, l->__obj_size);
if(l->__front == NULL) return CLIST_MEM_ALLOC_FAIL;
l->__list_size++;
return CLIST_SUCCESS;
}
clist_node_t* new_front = __clist_create_node(obj, l->__obj_size);
if(new_front == NULL) return CLIST_MEM_ALLOC_FAIL;
new_front->__next = l->__front;
l->__front->__prev = new_front;
l->__front = new_front;
l->__list_size++;
return CLIST_SUCCESS;
}
clist_error_t
clist_insert_rear(
clist_t *l,
const void *obj
) {
if(l == NULL || obj == NULL) return CLIST_GOT_NULL_PTR;
if(l->__front == NULL || l->__rear == NULL) {
l->__front = l->__rear = __clist_create_node(obj, l->__obj_size);
if(l->__front == NULL) return CLIST_MEM_ALLOC_FAIL;
l->__list_size++;
return CLIST_SUCCESS;
}
clist_node_t* new_rear = __clist_create_node(obj, l->__obj_size);
if(new_rear == NULL) return CLIST_MEM_ALLOC_FAIL;
new_rear->__prev = l->__rear;
l->__rear->__next = new_rear;
l->__rear = new_rear;
l->__list_size++;
return CLIST_SUCCESS;
}
clist_error_t
clist_insert_at(
clist_t* l,
const void* obj,
size_t index
) {
if(l == NULL || obj == NULL) return CLIST_GOT_NULL_PTR;
if(index == 0) return clist_insert_front(l, obj);
if(index >= l->__list_size) return clist_insert_rear(l, obj);
clist_node_t *node;
if(index < l->__list_size >> 1) {
node = l->__front;
while(index-- > 1 && node)
node = node->__next;
}else{
node = l->__rear;
index = l->__list_size - index;
while(index-- > 0 && node)
node = node->__prev;
}
clist_node_t* tmp_node = __clist_create_node(obj, l->__obj_size);
if(tmp_node == NULL) return CLIST_MEM_ALLOC_FAIL;
tmp_node->__next = node->__next;
if(tmp_node->__next)
tmp_node->__next->__prev = tmp_node;
node->__next = tmp_node;
tmp_node->__prev = node;
return CLIST_SUCCESS;
}
clist_error_t
clist_remove_front(
clist_t *l
) {
if(l == NULL) return CLIST_GOT_NULL_PTR;
if(l->__front == NULL) return CLIST_SUCCESS;
clist_node_t *node = l->__front;
l->__front = l->__front->__next;
if(l->__front == NULL) l->__rear = NULL;
free(node->__ptr);
free(node);
l->__list_size--;
return CLIST_SUCCESS;
}
clist_error_t
clist_remove_rear(
clist_t *l
) {
if(l == NULL) return CLIST_GOT_NULL_PTR;
if(l->__rear == NULL) return CLIST_SUCCESS;
clist_node_t *node = l->__front;
l->__front = l->__front->__next;
if(l->__front == NULL) l->__rear = NULL;
free(node->__ptr);
free(node);
l->__list_size--;
return CLIST_SUCCESS;
}
clist_error_t
clist_remove_at(
clist_t *l,
size_t index
) {
if(l == NULL) return CLIST_GOT_NULL_PTR;
if(index == 0) return clist_remove_front(l);
if(index == l->__list_size - 1) return clist_remove_rear(l);
if(index >= l->__list_size) return CLIST_INDEX_OUT_OF_BOUND;
clist_node_t *node;
if(index < l->__list_size >> 1) {
node = l->__front;
while(index-- > 1 && node)
node = node->__next;
}else{
node = l->__rear;
index = l->__list_size - index;
while(index-- > 0 && node)
node = node->__prev;
}
node->__prev->__next = node->__next;
node->__next->__prev = node->__prev;
free(node->__ptr);
free(node);
l->__list_size--;
return CLIST_SUCCESS;
}
clist_node_t*
clist_begin(
const clist_t *l
) {
if(l == NULL) return NULL;
return l->__front;
}
clist_node_t*
clist_rbegin(
const clist_t *l
) {
if(l == NULL) return NULL;
return l->__rear;
}
clist_node_t*
clist_end(
const clist_t *l
) {
return NULL;
}
clist_node_t*
clist_rend(
const clist_t *l
) {
return NULL;
}
clist_node_t*
clist_next(
const clist_node_t* n
) {
if(n == NULL) return NULL;
return n->__next;
}
clist_node_t*
clist_prev(
const clist_node_t* n
) {
if(n == NULL) return NULL;
return n->__prev;
}
size_t
clist_size(
const clist_t* l
) {
if(l == NULL) return 0;
return l->__list_size;
}
clist_node_t*
clist_node_at(
const clist_t* l,
size_t index
) {
if(l == NULL) return NULL;
if(index >= l->__list_size) return NULL;
if(index == 0) return l->__front;
if(index == l->__list_size - 1) return l->__rear;
clist_node_t* node;
if(index < l->__list_size >> 1) {
node = l->__front;
while(index-- > 0 && node)
node = node->__next;
}else{
node = l->__rear;
index = l->__list_size - index;
while(index-- > 1 && node)
node = node->__prev;
}
return node;
}
clist_error_t
clist_map(
const clist_t *l,
void *arg,
clist_map_function_t* func
) {
if(l == NULL || func == NULL) return CLIST_GOT_NULL_PTR;
clist_node_t *n;
clist_for_each(l, n) {
func(n, arg);
}
return CLIST_SUCCESS;
}
clist_error_t
clist_update_at(
const clist_t *l,
const void *val,
const size_t index
) {
if(l == NULL || val == NULL) return CLIST_GOT_NULL_PTR;
if(index >= l->__list_size) return CLIST_INDEX_OUT_OF_BOUND;
clist_node_t *n = clist_node_at(l, index);
memcpy(n->__ptr, val, l->__obj_size);
return CLIST_SUCCESS;
}
void
clist_free(
clist_t *l
) {
if(l == NULL) return;
if(l->__front == NULL || l->__rear == NULL) return;
clist_node_t *tmp = l->__front;
while(tmp) {
l->__front = l->__front->__next;
free(tmp->__ptr);
free(tmp);
tmp = l->__front;
}
free(l);
}