]> git.ozlabs.org Git - ccan/blob - junkcode/dongre.avinash@gmail.com-clibutils/src/c_set.c
tdb2: return TDB_ERR_RDONLY if trying to start a transaction on a R/O tdb.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / src / c_set.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 struct clib_set* \r
29 new_clib_set ( clib_compare fn_c, clib_destroy fn_d) {\r
30 \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
34 \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
38 \r
39     return pSet;\r
40 }\r
41 clib_error   \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
45 \r
46     return insert_clib_rb ( pSet->root, key, key_size, (void*)0, 0);\r
47 }\r
48 clib_bool    \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
52 \r
53     if (pSet == (struct clib_set*)0 )\r
54         return clib_false;\r
55     \r
56     node = find_clib_rb ( pSet->root, key);\r
57     if ( node != (struct clib_rb_node*)0  ) {\r
58         return clib_true;\r
59     }\r
60     return found;    \r
61 }\r
62 clib_error   \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
68 \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
72         free ( node );*/\r
73     }\r
74     return rc;\r
75 }\r
76 clib_bool    \r
77 find_clib_set ( struct clib_set *pSet, void *key, void* outKey) {\r
78     struct clib_rb_node* node;\r
79 \r
80     if (pSet == (struct clib_set*)0 )\r
81         return clib_false;\r
82 \r
83     node = find_clib_rb ( pSet->root, key);\r
84     if ( node == (struct clib_rb_node*)0  ) \r
85         return clib_false;\r
86 \r
87     get_raw_clib_object ( node->key, outKey );\r
88 \r
89     return clib_true;\r
90 \r
91 }\r
92 \r
93 clib_error    \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
98         free ( x );\r
99     }\r
100     return rc;\r
101 }\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
105 }\r
106 \r