-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcstl_hash_set.c
391 lines (316 loc) · 10.7 KB
/
cstl_hash_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
/*
* The implementation of hash_set.
* Copyright (C) 2008 - 2013 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/chash_set.h>
#include "cstl_hash_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 hash_set container.
*/
void hash_set_init(hash_set_t* phset_set)
{
assert(phset_set != NULL);
hash_set_init_ex(phset_set, 0, NULL, NULL);
}
/**
* Initialize hash_set container with user define compare function.
*/
void hash_set_init_ex(hash_set_t* phset_set, size_t t_bucketcount, ufun_t ufun_hash, bfun_t bfun_compare)
{
assert(phset_set != NULL);
_hashtable_init(&phset_set->_t_hashtable, t_bucketcount, ufun_hash, bfun_compare);
}
/**
* Destroy hash_set.
*/
void hash_set_destroy(hash_set_t* phset_set)
{
_hash_set_destroy_auxiliary(phset_set);
free(phset_set);
}
/**
* Initialize hash_set container with hash_set.
*/
void hash_set_init_copy(hash_set_t* phset_dest, const hash_set_t* cphset_src)
{
assert(phset_dest != NULL);
assert(cphset_src != NULL);
_hashtable_init_copy(&phset_dest->_t_hashtable, &cphset_src->_t_hashtable);
}
/**
* Initialize hash_set container with specific range.
*/
void hash_set_init_copy_range(hash_set_t* phset_dest, iterator_t it_begin, iterator_t it_end)
{
hash_set_init_copy_range_ex(phset_dest, it_begin, it_end, 0, NULL, NULL);
}
/**
* Initialize hash_set container with specific array.
*/
void hash_set_init_copy_array(hash_set_t* phset_dest, const void* cpv_array, size_t t_count)
{
hash_set_init_copy_array_ex(phset_dest, cpv_array, t_count, 0, NULL, NULL);
}
/**
* Initialize hash_set container with specific range and compare function.
*/
void hash_set_init_copy_range_ex(hash_set_t* phset_set, iterator_t it_begin, iterator_t it_end,
size_t t_bucketcount, ufun_t ufun_hash, bfun_t bfun_compare)
{
assert(phset_set != NULL);
assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));
_hashtable_init_copy_unique_range(&phset_set->_t_hashtable, it_begin, it_end, t_bucketcount, ufun_hash, bfun_compare);
}
/**
* Initialize hash_set container with specific array and compare function.
*/
void hash_set_init_copy_array_ex(hash_set_t* phset_set, const void* cpv_array, size_t t_count,
size_t t_bucketcount, ufun_t ufun_hash, bfun_t bfun_compare)
{
assert(phset_set != NULL);
assert(cpv_array != NULL);
_hashtable_init_copy_unique_array(&phset_set->_t_hashtable, cpv_array, t_count, t_bucketcount, ufun_hash, bfun_compare);
}
/**
* Assign hash_set container.
*/
void hash_set_assign(hash_set_t* phset_dest, const hash_set_t* cphset_src)
{
assert(phset_dest != NULL);
assert(cphset_src != NULL);
_hashtable_assign(&phset_dest->_t_hashtable, &cphset_src->_t_hashtable);
}
/**
* Swap the datas of first hash_set and second hash_set.
*/
void hash_set_swap(hash_set_t* phset_first, hash_set_t* phset_second)
{
assert(phset_first != NULL);
assert(phset_second != NULL);
_hashtable_swap(&phset_first->_t_hashtable, &phset_second->_t_hashtable);
}
/**
* Get the number of elements int the hash_set.
*/
size_t hash_set_size(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_size(&cphset_set->_t_hashtable);
}
/**
* Get the maximum number of elements int the hash_set.
*/
size_t hash_set_max_size(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_max_size(&cphset_set->_t_hashtable);
}
/**
* Test if an hash_set is empty.
*/
bool_t hash_set_empty(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_empty(&cphset_set->_t_hashtable);
}
/**
* Get the bucket number of elements int the hash_set.
*/
size_t hash_set_bucket_count(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_bucket_count(&cphset_set->_t_hashtable);
}
/**
* Return the hash function.
*/
ufun_t hash_set_hash(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_hash(&cphset_set->_t_hashtable);
}
/**
* Return the compare function of key.
*/
bfun_t hash_set_key_comp(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_key_comp(&cphset_set->_t_hashtable);
}
/**
* Return the compare function of value.
*/
bfun_t hash_set_value_comp(const hash_set_t* cphset_set)
{
assert(cphset_set != NULL);
return _hashtable_key_comp(&cphset_set->_t_hashtable);
}
/**
* Resize.
*/
void hash_set_resize(hash_set_t* phset_set, size_t t_resize)
{
assert(phset_set != NULL);
_hashtable_resize(&phset_set->_t_hashtable, t_resize);
}
/**
* Tests if the two hash_set are equal.
*/
bool_t hash_set_equal(const hash_set_t* cphset_first, const hash_set_t* cphset_second)
{
assert(cphset_first != NULL);
assert(cphset_second != NULL);
return _hashtable_equal(&cphset_first->_t_hashtable, &cphset_second->_t_hashtable);
}
/**
* Tests if the two hash_set are not equal.
*/
bool_t hash_set_not_equal(const hash_set_t* cphset_first, const hash_set_t* cphset_second)
{
assert(cphset_first != NULL);
assert(cphset_second != NULL);
return _hashtable_not_equal(&cphset_first->_t_hashtable, &cphset_second->_t_hashtable);
}
/**
* Tests if the first hash_set is less than the second hash_set.
*/
bool_t hash_set_less(const hash_set_t* cphset_first, const hash_set_t* cphset_second)
{
assert(cphset_first != NULL);
assert(cphset_second != NULL);
return _hashtable_less(&cphset_first->_t_hashtable, &cphset_second->_t_hashtable);
}
/**
* Tests if the first hash_set is less than or equal to the second hash_set.
*/
bool_t hash_set_less_equal(const hash_set_t* cphset_first, const hash_set_t* cphset_second)
{
assert(cphset_first != NULL);
assert(cphset_second != NULL);
return _hashtable_less_equal(&cphset_first->_t_hashtable, &cphset_second->_t_hashtable);
}
/**
* Tests if the first hash_set is greater than the second hash_set.
*/
bool_t hash_set_greater(const hash_set_t* cphset_first, const hash_set_t* cphset_second)
{
assert(cphset_first != NULL);
assert(cphset_second != NULL);
return _hashtable_greater(&cphset_first->_t_hashtable, &cphset_second->_t_hashtable);
}
/**
* Tests if the first hash_set is greater than or equal to the second hash_set.
*/
bool_t hash_set_greater_equal(const hash_set_t* cphset_first, const hash_set_t* cphset_second)
{
assert(cphset_first != NULL);
assert(cphset_second != NULL);
return _hashtable_greater_equal(&cphset_first->_t_hashtable, &cphset_second->_t_hashtable);
}
/**
* Return an iterator that addresses the first element in the hash_set.
*/
hash_set_iterator_t hash_set_begin(const hash_set_t* cphset_set)
{
hash_set_iterator_t it_begin;
assert(cphset_set != NULL);
it_begin = _hashtable_begin(&cphset_set->_t_hashtable);
_ITERATOR_CONTAINER(it_begin) = (hash_set_t*)cphset_set;
_HASH_SET_ITERATOR_CONTAINER_TYPE(it_begin) = _HASH_SET_CONTAINER;
_HASH_SET_ITERATOR_ITERATOR_TYPE(it_begin) = _BIDIRECTIONAL_ITERATOR;
return it_begin;
}
/**
* Return an iterator that addresses the location succeeding the last element in the hash_set.
*/
hash_set_iterator_t hash_set_end(const hash_set_t* cphset_set)
{
hash_set_iterator_t it_end;
assert(cphset_set != NULL);
it_end = _hashtable_end(&cphset_set->_t_hashtable);
_ITERATOR_CONTAINER(it_end) = (hash_set_t*)cphset_set;
_HASH_SET_ITERATOR_CONTAINER_TYPE(it_end) = _HASH_SET_CONTAINER;
_HASH_SET_ITERATOR_ITERATOR_TYPE(it_end) = _BIDIRECTIONAL_ITERATOR;
return it_end;
}
/**
* Inserts an range of unique element into a hash_set.
*/
void hash_set_insert_range(hash_set_t* phset_set, iterator_t it_begin, iterator_t it_end)
{
assert(phset_set != NULL);
assert(iterator_equal(it_begin, it_end) || _iterator_before(it_begin, it_end));
_hashtable_insert_unique_range(&phset_set->_t_hashtable, it_begin, it_end);
}
/**
* inserts an array of unique element into a hash_set.
*/
void hash_set_insert_array(hash_set_t* phset_set, const void* cpv_array, size_t t_count)
{
assert(phset_set != NULL);
assert(cpv_array != NULL);
_hashtable_insert_unique_array(&phset_set->_t_hashtable, cpv_array, t_count);
}
/*
* Erase an element in an hash_set from specificed position.
*/
void hash_set_erase_pos(hash_set_t* phset_set, hash_set_iterator_t it_pos)
{
assert(phset_set != NULL);
assert(_HASH_SET_ITERATOR_CONTAINER_TYPE(it_pos) == _HASH_SET_CONTAINER);
assert(_HASH_SET_ITERATOR_ITERATOR_TYPE(it_pos) == _BIDIRECTIONAL_ITERATOR);
assert(_HASH_SET_ITERATOR_CONTAINER(it_pos) == phset_set);
_hashtable_erase_pos(&phset_set->_t_hashtable, it_pos);
}
/*
* Erase a range of element in an hash_set.
*/
void hash_set_erase_range(hash_set_t* phset_set, hash_set_iterator_t it_begin, hash_set_iterator_t it_end)
{
assert(phset_set != NULL);
assert(_HASH_SET_ITERATOR_CONTAINER_TYPE(it_begin) == _HASH_SET_CONTAINER);
assert(_HASH_SET_ITERATOR_ITERATOR_TYPE(it_begin) == _BIDIRECTIONAL_ITERATOR);
assert(_HASH_SET_ITERATOR_CONTAINER_TYPE(it_end) == _HASH_SET_CONTAINER);
assert(_HASH_SET_ITERATOR_ITERATOR_TYPE(it_end) == _BIDIRECTIONAL_ITERATOR);
assert(_HASH_SET_ITERATOR_CONTAINER(it_begin) == phset_set);
assert(_HASH_SET_ITERATOR_CONTAINER(it_end) == phset_set);
_hashtable_erase_range(&phset_set->_t_hashtable, it_begin, it_end);
}
/**
* Erases all the elements of an hash_set.
*/
void hash_set_clear(hash_set_t* phset_set)
{
assert(phset_set != NULL);
_hashtable_clear(&phset_set->_t_hashtable);
}
/** local function implementation section **/
/** eof **/