]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/src/c_map.c
mem: add memends_str() helper for symmetry
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / src / c_map.c
1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\r
2  *  This file is part of clib library\r
3  *  Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )\r
4  *\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
11  * \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
14  * \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
21  *  THE SOFTWARE.\r
22  ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/\r
23 \r
24 #include "c_lib.h"\r
25 \r
26 #include <stdio.h>\r
27 \r
28 \r
29 struct clib_map* \r
30 new_clib_map ( clib_compare fn_clib_k, clib_destroy fn_k_d,  \r
31             clib_destroy fn_v_d) {\r
32 \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
36 \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
40 \r
41     return pMap;\r
42 }\r
43 clib_error   \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
47 \r
48     return insert_clib_rb ( pMap->root, key, key_size, value, value_size);\r
49 }\r
50 clib_bool    \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
54 \r
55     if (pMap == (struct clib_map*)0)\r
56         return clib_false;\r
57     \r
58     node = find_clib_rb ( pMap->root, key);\r
59     if ( node != (struct clib_rb_node*)0  ) {\r
60         return clib_true;\r
61     }\r
62     return found;    \r
63 }\r
64 clib_error   \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
70 \r
71     node = remove_clib_rb ( pMap->root, key );\r
72     if ( node != (struct clib_rb_node*)0  ) {\r
73         void* removed_node;\r
74         get_raw_clib_object ( node->key, &removed_node );\r
75         free ( removed_node);\r
76         delete_clib_object ( node->key );\r
77 \r
78         get_raw_clib_object ( node->value, &removed_node );\r
79         free ( removed_node);\r
80         delete_clib_object ( node->value);\r
81 \r
82         free ( node );\r
83     }\r
84     return rc;\r
85 }\r
86 clib_bool    \r
87 find_clib_map ( struct clib_map *pMap, void *key, void **value) {\r
88     struct clib_rb_node* node;\r
89 \r
90     if (pMap == (struct clib_map*)0)\r
91         return clib_false;\r
92 \r
93     node = find_clib_rb ( pMap->root, key);\r
94     if ( node == (struct clib_rb_node*)0  ) \r
95         return clib_false;\r
96 \r
97     get_raw_clib_object ( node->value, value );\r
98 \r
99     return clib_true;\r
100 \r
101 }\r
102 \r
103 clib_error    \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
108         free ( x );\r
109     }\r
110     return rc;\r
111 }\r
112 \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
116 }\r
117 \r