1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
\r
2 * This file is part of clib library
\r
3 * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
22 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
\r
29 new_clib_set ( clib_compare fn_c, clib_destroy fn_d) {
\r
31 struct clib_set *pSet = (struct clib_set*)malloc(sizeof(struct clib_set));
\r
32 if (pSet == (struct clib_set*)0 )
\r
33 return (struct clib_set*)0 ;
\r
35 pSet->root = new_clib_rb (fn_c, fn_d, (void*)0);
\r
36 if (pSet->root == (struct clib_rb*)0)
\r
37 return (struct clib_set*)0 ;
\r
42 insert_clib_set (struct clib_set *pSet, void *key, size_t key_size) {
\r
43 if (pSet == (struct clib_set*)0 )
\r
44 return CLIB_SET_NOT_INITIALIZED;
\r
46 return insert_clib_rb ( pSet->root, key, key_size, (void*)0, 0);
\r
49 exists_clib_set ( struct clib_set *pSet, void *key) {
\r
50 clib_bool found = clib_false;
\r
51 struct clib_rb_node* node;
\r
53 if (pSet == (struct clib_set*)0 )
\r
56 node = find_clib_rb ( pSet->root, key);
\r
57 if ( node != (struct clib_rb_node*)0 ) {
\r
63 remove_clib_set ( struct clib_set *pSet, void *key) {
\r
64 clib_error rc = CLIB_ERROR_SUCCESS;
\r
65 struct clib_rb_node* node;
\r
66 if (pSet == (struct clib_set*)0 )
\r
67 return CLIB_SET_NOT_INITIALIZED;
\r
69 node = remove_clib_rb ( pSet->root, key );
\r
70 if ( node != (struct clib_rb_node*)0 ) {
\r
71 /*free ( node->raw_data.key);
\r
77 find_clib_set ( struct clib_set *pSet, void *key, void* outKey) {
\r
78 struct clib_rb_node* node;
\r
80 if (pSet == (struct clib_set*)0 )
\r
83 node = find_clib_rb ( pSet->root, key);
\r
84 if ( node == (struct clib_rb_node*)0 )
\r
87 get_raw_clib_object ( node->key, outKey );
\r
94 delete_clib_set ( struct clib_set* x) {
\r
95 clib_error rc = CLIB_ERROR_SUCCESS;
\r
96 if ( x != (struct clib_set*)0 ){
\r
97 rc = delete_clib_rb ( x->root );
\r
102 static struct clib_rb_node *
\r
103 minimum_clib_set( struct clib_set *x ) {
\r
104 return minimum_clib_rb( x->root, x->root->root);
\r