-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathcstl_set.c
512 lines (431 loc) · 12.8 KB
/
cstl_set.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
/*
* The implementation of set.
* 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/cset.h>
#include "cstl_set_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 **/
/**
* Initialize set container.
*/
void set_init(set_t* pset_set)
{
assert(pset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init(&pset_set->_t_tree, NULL);
#else
_rb_tree_init(&pset_set->_t_tree, NULL);
#endif
}
/**
* Initialize set container with user define compare function.
*/
void set_init_ex(set_t* pset_set, bfun_t bfun_compare)
{
assert(pset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init(&pset_set->_t_tree, bfun_compare);
#else
_rb_tree_init(&pset_set->_t_tree, bfun_compare);
#endif
}
/**
* Destroy set.
*/
void set_destroy(set_t* pset_set)
{
assert(pset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_destroy(&pset_set->_t_tree);
#else
_rb_tree_destroy(&pset_set->_t_tree);
#endif
}
/**
* Initialize set container with set.
*/
void set_init_copy(set_t* pset_dest, const set_t* cpset_src)
{
assert(pset_dest != NULL);
assert(cpset_src != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init_copy(&pset_dest->_t_tree, &cpset_src->_t_tree);
#else
_rb_tree_init_copy(&pset_dest->_t_tree, &cpset_src->_t_tree);
#endif
}
/**
* Initialize set container with specific range.
*/
void set_init_copy_range(set_t* pset_dest, iterator_t it_begin, iterator_t it_end)
{
assert(pset_dest != NULL);
assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init_copy_unique_range(&pset_dest->_t_tree, it_begin, it_end);
#else
_rb_tree_init_copy_unique_range(&pset_dest->_t_tree, it_begin, it_end);
#endif
}
/**
* Initialize set container with specific array.
*/
void set_init_copy_array(set_t* pset_dest, const void* cpv_array, size_t t_count)
{
assert(pset_dest != NULL);
assert(cpv_array != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init_copy_unique_array(&pset_dest->_t_tree, cpv_array, t_count);
#else
_rb_tree_init_copy_unique_array(&pset_dest->_t_tree, cpv_array, t_count);
#endif
}
/**
* Initialize set container with specific range and compare function.
*/
void set_init_copy_range_ex(
set_t* pset_dest, iterator_t it_begin, iterator_t it_end, bfun_t bfun_compare)
{
assert(pset_dest != NULL);
assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init_copy_unique_range_ex(&pset_dest->_t_tree, it_begin, it_end, bfun_compare);
#else
_rb_tree_init_copy_unique_range_ex(&pset_dest->_t_tree, it_begin, it_end, bfun_compare);
#endif
}
/**
* Initialize set container with specific array and compare function.
*/
void set_init_copy_array_ex(
set_t* pset_dest, const void* cpv_array, size_t t_count, bfun_t bfun_compare)
{
assert(pset_dest != NULL);
assert(cpv_array != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_init_copy_unique_array_ex(&pset_dest->_t_tree, cpv_array, t_count, bfun_compare);
#else
_rb_tree_init_copy_unique_array_ex(&pset_dest->_t_tree, cpv_array, t_count, bfun_compare);
#endif
}
/**
* Assign set container.
*/
void set_assign(set_t* pset_dest, const set_t* cpset_src)
{
assert(pset_dest != NULL);
assert(cpset_src != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_assign(&pset_dest->_t_tree, &cpset_src->_t_tree);
#else
_rb_tree_assign(&pset_dest->_t_tree, &cpset_src->_t_tree);
#endif
}
/**
* Test if an set is empty.
*/
bool_t set_empty(const set_t* cpset_set)
{
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_empty(&cpset_set->_t_tree);
#else
return _rb_tree_empty(&cpset_set->_t_tree);
#endif
}
/**
* Get the number of elements int the set.
*/
size_t set_size(const set_t* cpset_set)
{
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_size(&cpset_set->_t_tree);
#else
return _rb_tree_size(&cpset_set->_t_tree);
#endif
}
/**
* Get the maximum number of elements int the set.
*/
size_t set_max_size(const set_t* cpset_set)
{
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_max_size(&cpset_set->_t_tree);
#else
return _rb_tree_max_size(&cpset_set->_t_tree);
#endif
}
/**
* Return an iterator that addresses the first element in the set.
*/
set_iterator_t set_begin(const set_t* cpset_set)
{
set_iterator_t it_begin;
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
it_begin = _avl_tree_begin(&cpset_set->_t_tree);
#else
it_begin = _rb_tree_begin(&cpset_set->_t_tree);
#endif
_ITERATOR_CONTAINER(it_begin) = (set_t*)cpset_set;
_SET_ITERATOR_CONTAINER_TYPE(it_begin) = _SET_CONTAINER;
_SET_ITERATOR_ITERATOR_TYPE(it_begin) = _BIDIRECTIONAL_ITERATOR;
return it_begin;
}
/**
* Return an iterator that addresses the location succeeding the last element in the set.
*/
set_iterator_t set_end(const set_t* cpset_set)
{
set_iterator_t it_end;
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
it_end = _avl_tree_end(&cpset_set->_t_tree);
#else
it_end = _rb_tree_end(&cpset_set->_t_tree);
#endif
_ITERATOR_CONTAINER(it_end) = (set_t*)cpset_set;
_SET_ITERATOR_CONTAINER_TYPE(it_end) = _SET_CONTAINER;
_SET_ITERATOR_ITERATOR_TYPE(it_end) = _BIDIRECTIONAL_ITERATOR;
return it_end;
}
set_iterator_t set_rbegin(const set_t* cpset_set)
{
set_iterator_t it_rbegin;
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
it_rbegin = _avl_tree_rbegin(&cpset_set->_t_tree);
#else
it_rbegin = _rb_tree_rbegin(&cpset_set->_t_tree);
#endif
_ITERATOR_CONTAINER(it_rbegin) = (set_t*)cpset_set;
_SET_ITERATOR_CONTAINER_TYPE(it_rbegin) = _SET_CONTAINER;
_SET_ITERATOR_ITERATOR_TYPE(it_rbegin) = _BIDIRECTIONAL_ITERATOR;
return it_rbegin;
}
set_iterator_t set_rend(const set_t* cpset_set)
{
set_iterator_t it_rend;
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
it_rend = _avl_tree_rend(&cpset_set->_t_tree);
#else
it_rend = _rb_tree_rend(&cpset_set->_t_tree);
#endif
_ITERATOR_CONTAINER(it_rend) = (set_t*)cpset_set;
_SET_ITERATOR_CONTAINER_TYPE(it_rend) = _SET_CONTAINER;
_SET_ITERATOR_ITERATOR_TYPE(it_rend) = _BIDIRECTIONAL_ITERATOR;
return it_rend;
}
/**
* Return the compare function of key.
*/
bfun_t set_key_comp(const set_t* cpset_set)
{
assert(cpset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_key_comp(&cpset_set->_t_tree);
#else
return _rb_tree_key_comp(&cpset_set->_t_tree);
#endif
}
/**
* Return the compare function of value.
*/
bfun_t set_value_comp(const set_t* cpset_set)
{
return set_key_comp(cpset_set);
}
/**
* Erases all the elements of an set.
*/
void set_clear(set_t* pset_set)
{
assert(pset_set != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_clear(&pset_set->_t_tree);
#else
_rb_tree_clear(&pset_set->_t_tree);
#endif
}
/**
* Tests if the two set are equal.
*/
bool_t set_equal(const set_t* cpset_first, const set_t* cpset_second)
{
assert(cpset_first != NULL);
assert(cpset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#else
return _rb_tree_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#endif
}
/**
* Tests if the two set are not equal.
*/
bool_t set_not_equal(const set_t* cpset_first, const set_t* cpset_second)
{
assert(cpset_first != NULL);
assert(cpset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_not_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#else
return _rb_tree_not_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#endif
}
/**
* Tests if the first set is less than the second set.
*/
bool_t set_less(const set_t* cpset_first, const set_t* cpset_second)
{
assert(cpset_first != NULL);
assert(cpset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_less(&cpset_first->_t_tree, &cpset_second->_t_tree);
#else
return _rb_tree_less(&cpset_first->_t_tree, &cpset_second->_t_tree);
#endif
}
/**
* Tests if the first set is less than or equal to the second set.
*/
bool_t set_less_equal(const set_t* cpset_first, const set_t* cpset_second)
{
assert(cpset_first != NULL);
assert(cpset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_less_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#else
return _rb_tree_less_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#endif
}
/**
* Tests if the first set is greater than the second set.
*/
bool_t set_greater(const set_t* cpset_first, const set_t* cpset_second)
{
assert(cpset_first != NULL);
assert(cpset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_greater(&cpset_first->_t_tree, &cpset_second->_t_tree);
#else
return _rb_tree_greater(&cpset_first->_t_tree, &cpset_second->_t_tree);
#endif
}
/**
* Tests if the first set is greater than or equal to the second set.
*/
bool_t set_greater_equal(const set_t* cpset_first, const set_t* cpset_second)
{
assert(cpset_first != NULL);
assert(cpset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
return _avl_tree_greater_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#else
return _rb_tree_greater_equal(&cpset_first->_t_tree, &cpset_second->_t_tree);
#endif
}
/**
* Swap the datas of first set and second set.
*/
void set_swap(set_t* pset_first, set_t* pset_second)
{
assert(pset_first != NULL);
assert(pset_second != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_swap(&pset_first->_t_tree, &pset_second->_t_tree);
#else
_rb_tree_swap(&pset_first->_t_tree, &pset_second->_t_tree);
#endif
}
/**
* Inserts an range of unique element into a set.
*/
void set_insert_range(set_t* pset_set, iterator_t it_begin, iterator_t it_end)
{
assert(pset_set != NULL);
assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));
#ifdef CSTL_SET_AVL_TREE
_avl_tree_insert_unique_range(&pset_set->_t_tree, it_begin, it_end);
#else
_rb_tree_insert_unique_range(&pset_set->_t_tree, it_begin, it_end);
#endif
}
/**
* Inserts an array of unique element into a set.
*/
void set_insert_array(set_t* pset_set, const void* cpv_array, size_t t_count)
{
assert(pset_set != NULL);
assert(cpv_array != NULL);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_insert_unique_array(&pset_set->_t_tree, cpv_array, t_count);
#else
_rb_tree_insert_unique_array(&pset_set->_t_tree, cpv_array, t_count);
#endif
}
/*
* Erase an element in an set from specificed position.
*/
void set_erase_pos(set_t* pset_set, set_iterator_t it_pos)
{
assert(pset_set != NULL);
assert(_SET_ITERATOR_CONTAINER_TYPE(it_pos) == _SET_CONTAINER);
assert(_SET_ITERATOR_ITERATOR_TYPE(it_pos) == _BIDIRECTIONAL_ITERATOR);
assert(_SET_ITERATOR_CONTAINER(it_pos) == pset_set);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_erase_pos(&pset_set->_t_tree, it_pos);
#else
_rb_tree_erase_pos(&pset_set->_t_tree, it_pos);
#endif
}
/*
* Erase a range of element in an set.
*/
void set_erase_range(set_t* pset_set, set_iterator_t it_begin, set_iterator_t it_end)
{
assert(pset_set != NULL);
assert(_SET_ITERATOR_CONTAINER_TYPE(it_begin) == _SET_CONTAINER);
assert(_SET_ITERATOR_ITERATOR_TYPE(it_begin) == _BIDIRECTIONAL_ITERATOR);
assert(_SET_ITERATOR_CONTAINER_TYPE(it_end) == _SET_CONTAINER);
assert(_SET_ITERATOR_ITERATOR_TYPE(it_end) == _BIDIRECTIONAL_ITERATOR);
assert(_SET_ITERATOR_CONTAINER(it_begin) == pset_set);
assert(_SET_ITERATOR_CONTAINER(it_end) == pset_set);
#ifdef CSTL_SET_AVL_TREE
_avl_tree_erase_range(&pset_set->_t_tree, it_begin, it_end);
#else
_rb_tree_erase_range(&pset_set->_t_tree, it_begin, it_end);
#endif
}
/** local function implementation section **/
/** eof **/