]> git.ozlabs.org Git - ccan/blobdiff - junkcode/dongre.avinash@gmail.com-clibutils/src/c_set.c
junkcode: upload via website.
[ccan] / junkcode / dongre.avinash@gmail.com-clibutils / src / c_set.c
diff --git a/junkcode/dongre.avinash@gmail.com-clibutils/src/c_set.c b/junkcode/dongre.avinash@gmail.com-clibutils/src/c_set.c
new file mode 100644 (file)
index 0000000..068ed31
--- /dev/null
@@ -0,0 +1,106 @@
+/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\r
+ *  This file is part of clib library\r
+ *  Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )\r
+ *\r
+ *  Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ *  of this software and associated documentation files (the "Software"), to deal\r
+ *  in the Software without restriction, including without limitation the rights\r
+ *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ *  copies of the Software, and to permit persons to whom the Software is\r
+ *  furnished to do so, subject to the following conditions:\r
+ * \r
+ *  The above copyright notice and this permission notice shall be included in\r
+ *  all copies or substantial portions of the Software.\r
+ * \r
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
+ *  THE SOFTWARE.\r
+ ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/\r
+\r
+#include "c_lib.h"\r
+\r
+#include <stdio.h>\r
+\r
+struct clib_set* \r
+new_clib_set ( clib_compare fn_c, clib_destroy fn_d) {\r
+\r
+    struct clib_set *pSet  =  (struct clib_set*)malloc(sizeof(struct clib_set));\r
+    if (pSet == (struct clib_set*)0 )\r
+        return (struct clib_set*)0 ;\r
+\r
+    pSet->root  = new_clib_rb (fn_c, fn_d, (void*)0);\r
+    if (pSet->root == (struct clib_rb*)0)\r
+        return (struct clib_set*)0 ;\r
+\r
+    return pSet;\r
+}\r
+clib_error   \r
+insert_clib_set (struct clib_set *pSet, void *key, size_t key_size) {\r
+    if (pSet == (struct clib_set*)0 )\r
+        return CLIB_SET_NOT_INITIALIZED;\r
+\r
+    return insert_clib_rb ( pSet->root, key, key_size, (void*)0, 0);\r
+}\r
+clib_bool    \r
+exists_clib_set ( struct clib_set *pSet, void *key) {\r
+    clib_bool found = clib_false;\r
+    struct clib_rb_node* node;\r
+\r
+    if (pSet == (struct clib_set*)0 )\r
+        return clib_false;\r
+    \r
+    node = find_clib_rb ( pSet->root, key);\r
+    if ( node != (struct clib_rb_node*)0  ) {\r
+        return clib_true;\r
+    }\r
+    return found;    \r
+}\r
+clib_error   \r
+remove_clib_set ( struct clib_set *pSet, void *key) {\r
+    clib_error rc = CLIB_ERROR_SUCCESS;\r
+    struct clib_rb_node* node;\r
+    if (pSet == (struct clib_set*)0 )\r
+        return CLIB_SET_NOT_INITIALIZED;\r
+\r
+    node = remove_clib_rb ( pSet->root, key );\r
+    if ( node != (struct clib_rb_node*)0  ) {\r
+        /*free ( node->raw_data.key);\r
+        free ( node );*/\r
+    }\r
+    return rc;\r
+}\r
+clib_bool    \r
+find_clib_set ( struct clib_set *pSet, void *key, void* outKey) {\r
+    struct clib_rb_node* node;\r
+\r
+    if (pSet == (struct clib_set*)0 )\r
+        return clib_false;\r
+\r
+    node = find_clib_rb ( pSet->root, key);\r
+    if ( node == (struct clib_rb_node*)0  ) \r
+        return clib_false;\r
+\r
+    get_raw_clib_object ( node->key, outKey );\r
+\r
+    return clib_true;\r
+\r
+}\r
+\r
+clib_error    \r
+delete_clib_set ( struct clib_set* x) {\r
+    clib_error rc = CLIB_ERROR_SUCCESS;\r
+    if ( x != (struct clib_set*)0  ){\r
+        rc = delete_clib_rb ( x->root );\r
+        free ( x );\r
+    }\r
+    return rc;\r
+}\r
+static struct clib_rb_node *\r
+minimum_clib_set( struct clib_set *x ) {\r
+       return minimum_clib_rb( x->root, x->root->root);\r
+}\r
+\r