-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcstl_list_private.c
520 lines (428 loc) · 17.1 KB
/
cstl_list_private.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* The implement of list private interface.
* Copyright (C) 2008 - 2014 Wangbo
*
* This 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 2.1 of the License, or (at your option) any later version.
*
* This 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author e-mail: activesys.wb@gmail.com
* activesys@sina.com.cn
*/
/** include section **/
#include <cstl/cstl_def.h>
#include <cstl/cstl_alloc.h>
#include <cstl/cstl_types.h>
#include <cstl/citerator.h>
#include <cstl/cfunctional.h>
#include <cstl/cstring.h>
#include <cstl/clist.h>
#include "cstl_list_aux.h"
/** local constant declaration and local macro section **/
/** local data type declaration and local struct, union, enum section **/
/** local function prototype section **/
/** exported global variable definition section **/
/** local global variable definition section **/
/** exported function implementation section **/
/**
* Create list container.
*/
list_t* _create_list(const char* s_typename)
{
list_t* plist_new = NULL;
assert(s_typename != NULL);
if ((plist_new = (list_t*)malloc(sizeof(list_t))) == NULL) {
return NULL;
}
if (!_create_list_auxiliary(plist_new, s_typename)) {
free(plist_new);
return NULL;
}
return plist_new;
}
/**
* Create list container auxiliary function.
*/
bool_t _create_list_auxiliary(list_t* plist_list, const char* s_typename)
{
assert(plist_list != NULL);
assert(s_typename != NULL);
_type_get_type(&plist_list->_t_typeinfo, s_typename);
if (plist_list->_t_typeinfo._t_style == _TYPE_INVALID) {
return false;
}
plist_list->_pt_node = NULL;
_alloc_init(&plist_list->_t_allocator);
return true;
}
/**
* Initialize list with specified element.
*/
void _list_init_elem(list_t* plist_list, size_t t_count, ...)
{
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_created(plist_list));
va_start(val_elemlist, t_count);
_list_init_elem_varg(plist_list, t_count, val_elemlist);
va_end(val_elemlist);
}
/**
* Initialize list with variable argument list of specified element.
*/
void _list_init_elem_varg(list_t* plist_list, size_t t_count, va_list val_elemlist)
{
_listnode_t* pt_varg = NULL;
assert(plist_list != NULL);
assert(_list_is_created(plist_list));
/* allocate the end element */
/*
* _pt_node --+
* V
* +-->+------+<--+
* +---| prev | |
* +------+ |
* | next |---+
* +------+
* | data |
* +------+
*/
plist_list->_pt_node = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(plist_list->_pt_node != NULL);
plist_list->_pt_node->_pt_next = plist_list->_pt_node;
plist_list->_pt_node->_pt_prev = plist_list->_pt_node;
if (t_count > 0) {
size_t i = 0;
_listnode_t* pt_node = NULL;
bool_t b_result = false;
/* get varg value only once */
pt_varg = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_varg != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_varg);
for (i = 0; i < t_count; ++i) {
pt_node = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_node != NULL);
_list_init_node_auxiliary(plist_list, pt_node);
b_result = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_COPY_FUNCTION(plist_list)(pt_node->_pby_data, pt_varg->_pby_data, &b_result);
assert(b_result);
pt_node->_pt_next = plist_list->_pt_node;
pt_node->_pt_prev = plist_list->_pt_node->_pt_prev;
plist_list->_pt_node->_pt_prev->_pt_next = pt_node;
plist_list->_pt_node->_pt_prev = pt_node;
pt_node = NULL;
}
_list_destroy_varg_value_auxiliary(plist_list, pt_varg);
_alloc_deallocate(&plist_list->_t_allocator, pt_varg, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
}
}
/**
* Destroy list container auxiliary function.
*/
void _list_destroy_auxiliary(list_t* plist_list)
{
_listnode_t* pt_node = NULL; /* for destroy element */
_listnode_t* pt_destroynode = NULL; /* for destroy node itself */
bool_t b_result = false;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list) || _list_is_created(plist_list));
if (plist_list->_pt_node != NULL) {
pt_node = plist_list->_pt_node->_pt_next;
/* destroy all element and node except the end node */
while (pt_node != plist_list->_pt_node) {
pt_destroynode = pt_node;
pt_node = pt_node->_pt_next;
/* delete the destroy node from the link list */
pt_node->_pt_prev = pt_destroynode->_pt_prev;
pt_destroynode->_pt_prev->_pt_next = pt_node;
/* destroy the node */
b_result = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_DESTROY_FUNCTION(plist_list)(pt_destroynode->_pby_data, &b_result);
assert(b_result);
_alloc_deallocate(&plist_list->_t_allocator, pt_destroynode, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
}
/* destroy the end node */
_alloc_deallocate(&plist_list->_t_allocator, plist_list->_pt_node, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
}
_alloc_destroy(&plist_list->_t_allocator);
plist_list->_pt_node = NULL;
}
/**
* Assign list with specificed element.
*/
void _list_assign_elem(list_t* plist_list, size_t t_count, ...)
{
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
va_start(val_elemlist, t_count);
_list_assign_elem_varg(plist_list, t_count, val_elemlist);
va_end(val_elemlist);
}
/**
* Assign list with variable argument of specificed element.
*/
void _list_assign_elem_varg(list_t* plist_list, size_t t_count, va_list val_elemlist)
{
_listnode_t* pt_varg = NULL;
bool_t b_result = false;
list_iterator_t it_iter;
list_iterator_t it_begin;
list_iterator_t it_end;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
list_resize(plist_list, t_count);
/* get varg value */
pt_varg = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_varg != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_varg);
it_begin = list_begin(plist_list);
it_end = list_end(plist_list);
/* copy value form varg */
for (it_iter = it_begin; !iterator_equal(it_iter, it_end); it_iter = iterator_next(it_iter)) {
b_result = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_COPY_FUNCTION(plist_list)(((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data, pt_varg->_pby_data, &b_result);
assert(b_result);
}
/* destroy varg value */
_list_destroy_varg_value_auxiliary(plist_list, pt_varg);
_alloc_deallocate(&plist_list->_t_allocator, pt_varg, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
}
/**
* Insert multiple copys of element befor specificed position.
*/
list_iterator_t _list_insert_n(list_t* plist_list, list_iterator_t it_pos, size_t t_count, ...)
{
list_iterator_t it_iter;
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
assert(_list_iterator_belong_to_list(plist_list, it_pos));
va_start(val_elemlist, t_count);
it_iter = _list_insert_n_varg(plist_list, it_pos, t_count, val_elemlist);
va_end(val_elemlist);
return it_iter;
}
/**
* Insert multiple copys of element befor specificed position, the element is from variable argument list.
*/
list_iterator_t _list_insert_n_varg(list_t* plist_list, list_iterator_t it_pos, size_t t_count, va_list val_elemlist)
{
_listnode_t* pt_node = NULL; /* the insert node */
size_t i = 0;
_listnode_t* pt_varg = NULL;
bool_t b_result = false;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
assert(_list_iterator_belong_to_list(plist_list, it_pos));
if (t_count == 0) {
return it_pos;
}
/* get varg value only once */
pt_varg = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_varg != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_varg);
for (i = 0; i < t_count; ++i) {
/* allocate the memory for insert node */
pt_node = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_node != NULL);
_list_init_node_auxiliary(plist_list, pt_node);
/* copy value from varg */
b_result = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_COPY_FUNCTION(plist_list)(pt_node->_pby_data, pt_varg->_pby_data, &b_result);
assert(b_result);
/* insert the element in the position it_pos */
pt_node->_pt_next = (_listnode_t*)_LIST_ITERATOR_COREPOS(it_pos);
pt_node->_pt_prev = ((_listnode_t*)_LIST_ITERATOR_COREPOS(it_pos))->_pt_prev;
((_listnode_t*)_LIST_ITERATOR_COREPOS(it_pos))->_pt_prev->_pt_next = pt_node;
((_listnode_t*)_LIST_ITERATOR_COREPOS(it_pos))->_pt_prev = pt_node;
pt_node = NULL;
}
/* destroy varg value */
_list_destroy_varg_value_auxiliary(plist_list, pt_varg);
_alloc_deallocate(&plist_list->_t_allocator, pt_varg, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
return iterator_advance(it_pos, -(int)t_count);
}
/**
* Add specificed element at the end of list container.
*/
void _list_push_back(list_t* plist_list, ...)
{
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
va_start(val_elemlist, plist_list);
_list_push_back_varg(plist_list, val_elemlist);
va_end(val_elemlist);
}
/**
* Add specificed element from variable argument list at the end of list container.
*/
void _list_push_back_varg(list_t* plist_list, va_list val_elemlist)
{
_listnode_t* pt_node = NULL; /* the insert node */
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
/* allocate the insert node */
pt_node = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_node != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_node);
/* insert the node before the end node */
pt_node->_pt_next = plist_list->_pt_node;
pt_node->_pt_prev = plist_list->_pt_node->_pt_prev;
plist_list->_pt_node->_pt_prev->_pt_next = pt_node;
plist_list->_pt_node->_pt_prev = pt_node;
}
/**
* Add specificed element at the begin of list container.
*/
void _list_push_front(list_t* plist_list, ...)
{
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
va_start(val_elemlist, plist_list);
_list_push_front_varg(plist_list, val_elemlist);
va_end(val_elemlist);
}
/**
* Add specificed element from variable argument list at the begin of list container.
*/
void _list_push_front_varg(list_t* plist_list, va_list val_elemlist)
{
_listnode_t* pt_node = NULL; /* the allocate node */
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
/* allocate the node */
pt_node = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_node != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_node);
/* insert the node into the first position */
pt_node->_pt_next = plist_list->_pt_node->_pt_next;
pt_node->_pt_prev = plist_list->_pt_node;
plist_list->_pt_node->_pt_next->_pt_prev = pt_node;
plist_list->_pt_node->_pt_next = pt_node;
}
/**
* Remove specificed element from list container.
*/
void _list_remove(list_t* plist_list, ...)
{
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
va_start(val_elemlist, plist_list);
_list_remove_varg(plist_list, val_elemlist);
va_end(val_elemlist);
}
/**
* Remove element that specificed by variable argument list from list container.
*/
void _list_remove_varg(list_t* plist_list, va_list val_elemlist)
{
list_iterator_t it_pos; /* the remove element position */
list_iterator_t it_end;
_listnode_t* pt_varg = NULL;
bool_t b_less = false;
bool_t b_greater = false;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
pt_varg = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_varg != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_varg);
it_pos = list_begin(plist_list);
it_end = list_end(plist_list);
while (!iterator_equal(it_pos, it_end)) {
b_less = b_greater = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_LESS_FUNCTION(plist_list)(((_listnode_t*)_LIST_ITERATOR_COREPOS(it_pos))->_pby_data, pt_varg->_pby_data, &b_less);
_GET_LIST_TYPE_LESS_FUNCTION(plist_list)(pt_varg->_pby_data, ((_listnode_t*)_LIST_ITERATOR_COREPOS(it_pos))->_pby_data, &b_greater);
it_pos = (b_less || b_greater) ? iterator_next(it_pos) : list_erase(plist_list, it_pos);
}
_list_destroy_varg_value_auxiliary(plist_list, pt_varg);
_alloc_deallocate(&plist_list->_t_allocator, pt_varg, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
}
/**
* Reset the size of list elements.
*/
void _list_resize_elem(list_t* plist_list, size_t t_resize, ...)
{
va_list val_elemlist;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
va_start(val_elemlist, t_resize);
_list_resize_elem_varg(plist_list, t_resize, val_elemlist);
va_end(val_elemlist);
}
/**
* Reset the size of list elements, and filled element is from variable argument list.
*/
void _list_resize_elem_varg(list_t* plist_list, size_t t_resize, va_list val_elemlist)
{
_listnode_t* pt_node = NULL; /* the node for allocate */
size_t t_listsize = 0; /* the list size */
size_t i = 0;
_listnode_t* pt_varg = NULL;
bool_t b_result = false;
assert(plist_list != NULL);
assert(_list_is_inited(plist_list));
t_listsize = list_size(plist_list);
if (t_resize == t_listsize) {
return;
} else if (t_resize < t_listsize) {
for (i = 0; i < t_listsize - t_resize; ++i) {
list_pop_back(plist_list);
}
} else {
/* get varg value only once */
pt_varg = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_varg != NULL);
_list_get_varg_value_auxiliary(plist_list, val_elemlist, pt_varg);
for (i = 0; i < t_resize - t_listsize; ++i) {
pt_node = _alloc_allocate(&plist_list->_t_allocator, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
assert(pt_node != NULL);
_list_init_node_auxiliary(plist_list, pt_node);
b_result = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_COPY_FUNCTION(plist_list)(pt_node->_pby_data, pt_varg->_pby_data, &b_result);
assert(b_result);
pt_node->_pt_next = plist_list->_pt_node;
pt_node->_pt_prev = plist_list->_pt_node->_pt_prev;
plist_list->_pt_node->_pt_prev->_pt_next = pt_node;
plist_list->_pt_node->_pt_prev = pt_node;
pt_node = NULL;
}
_list_destroy_varg_value_auxiliary(plist_list, pt_varg);
_alloc_deallocate(&plist_list->_t_allocator, pt_varg, _LIST_NODE_SIZE(_GET_LIST_TYPE_SIZE(plist_list)), 1);
}
}
/**
* Initialize element with list element type auxiliary function.
*/
void _list_init_elem_auxiliary(list_t* plist_list, void* pv_elem)
{
assert(plist_list != NULL);
assert(pv_elem != NULL);
assert(_list_is_inited(plist_list) || _list_is_created(plist_list));
/* initialize new elements */
if (_GET_LIST_TYPE_STYLE(plist_list) == _TYPE_CSTL_BUILTIN) {
/* get element type name */
char s_elemtypename[_TYPE_NAME_SIZE + 1];
_type_get_elem_typename(_GET_LIST_TYPE_NAME(plist_list), s_elemtypename);
_GET_LIST_TYPE_INIT_FUNCTION(plist_list)(pv_elem, s_elemtypename);
} else {
bool_t b_result = _GET_LIST_TYPE_SIZE(plist_list);
_GET_LIST_TYPE_INIT_FUNCTION(plist_list)(pv_elem, &b_result);
assert(b_result);
}
}
/** local function implementation section **/
/** eof **/