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
30 new_clib_map ( clib_compare fn_clib_k, clib_destroy fn_k_d,
\r
31 clib_destroy fn_v_d) {
\r
33 struct clib_map *pMap = (struct clib_map*)malloc(sizeof(struct clib_map));
\r
34 if (pMap == (struct clib_map*)0)
\r
35 return (struct clib_map*)0;
\r
37 pMap->root = new_clib_rb (fn_clib_k, fn_k_d, fn_v_d);
\r
38 if (pMap->root == (struct clib_rb*)0)
\r
39 return (struct clib_map*)0;
\r
44 insert_clib_map ( struct clib_map *pMap, void *key, size_t key_size, void *value, size_t value_size) {
\r
45 if (pMap == (struct clib_map*)0)
\r
46 return CLIB_MAP_NOT_INITIALIZED;
\r
48 return insert_clib_rb ( pMap->root, key, key_size, value, value_size);
\r
51 exists_clib_map ( struct clib_map *pMap, void *key) {
\r
52 clib_bool found = clib_false;
\r
53 struct clib_rb_node* node;
\r
55 if (pMap == (struct clib_map*)0)
\r
58 node = find_clib_rb ( pMap->root, key);
\r
59 if ( node != (struct clib_rb_node*)0 ) {
\r
65 remove_clib_map ( struct clib_map *pMap, void *key) {
\r
66 clib_error rc = CLIB_ERROR_SUCCESS;
\r
67 struct clib_rb_node* node;
\r
68 if (pMap == (struct clib_map*)0)
\r
69 return CLIB_MAP_NOT_INITIALIZED;
\r
71 node = remove_clib_rb ( pMap->root, key );
\r
72 if ( node != (struct clib_rb_node*)0 ) {
\r
74 get_raw_clib_object ( node->key, &removed_node );
\r
75 free ( removed_node);
\r
76 delete_clib_object ( node->key );
\r
78 get_raw_clib_object ( node->value, &removed_node );
\r
79 free ( removed_node);
\r
80 delete_clib_object ( node->value);
\r
87 find_clib_map ( struct clib_map *pMap, void *key, void **value) {
\r
88 struct clib_rb_node* node;
\r
90 if (pMap == (struct clib_map*)0)
\r
93 node = find_clib_rb ( pMap->root, key);
\r
94 if ( node == (struct clib_rb_node*)0 )
\r
97 get_raw_clib_object ( node->value, value );
\r
104 delete_clib_map ( struct clib_map* x) {
\r
105 clib_error rc = CLIB_ERROR_SUCCESS;
\r
106 if ( x != (struct clib_map*)0 ){
\r
107 rc = delete_clib_rb ( x->root );
\r
113 static struct clib_rb_node *
\r
114 minimum_clib_map( struct clib_map *x ) {
\r
115 return minimum_clib_rb( x->root, x->root->root);
\r