--- /dev/null
+/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **\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
+\r
+struct clib_map* \r
+new_clib_map ( clib_compare fn_clib_k, clib_destroy fn_k_d, \r
+ clib_destroy fn_v_d) {\r
+\r
+ struct clib_map *pMap = (struct clib_map*)malloc(sizeof(struct clib_map));\r
+ if (pMap == (struct clib_map*)0)\r
+ return (struct clib_map*)0;\r
+\r
+ pMap->root = new_clib_rb (fn_clib_k, fn_k_d, fn_v_d);\r
+ if (pMap->root == (struct clib_rb*)0)\r
+ return (struct clib_map*)0;\r
+\r
+ return pMap;\r
+}\r
+clib_error \r
+insert_clib_map ( struct clib_map *pMap, void *key, size_t key_size, void *value, size_t value_size) {\r
+ if (pMap == (struct clib_map*)0)\r
+ return CLIB_MAP_NOT_INITIALIZED;\r
+\r
+ return insert_clib_rb ( pMap->root, key, key_size, value, value_size);\r
+}\r
+clib_bool \r
+exists_clib_map ( struct clib_map *pMap, void *key) {\r
+ clib_bool found = clib_false;\r
+ struct clib_rb_node* node;\r
+\r
+ if (pMap == (struct clib_map*)0)\r
+ return clib_false;\r
+ \r
+ node = find_clib_rb ( pMap->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_map ( struct clib_map *pMap, void *key) {\r
+ clib_error rc = CLIB_ERROR_SUCCESS;\r
+ struct clib_rb_node* node;\r
+ if (pMap == (struct clib_map*)0)\r
+ return CLIB_MAP_NOT_INITIALIZED;\r
+\r
+ node = remove_clib_rb ( pMap->root, key );\r
+ if ( node != (struct clib_rb_node*)0 ) {\r
+ void* removed_node;\r
+ get_raw_clib_object ( node->key, &removed_node );\r
+ free ( removed_node);\r
+ delete_clib_object ( node->key );\r
+\r
+ get_raw_clib_object ( node->value, &removed_node );\r
+ free ( removed_node);\r
+ delete_clib_object ( node->value);\r
+\r
+ free ( node );\r
+ }\r
+ return rc;\r
+}\r
+clib_bool \r
+find_clib_map ( struct clib_map *pMap, void *key, void **value) {\r
+ struct clib_rb_node* node;\r
+\r
+ if (pMap == (struct clib_map*)0)\r
+ return clib_false;\r
+\r
+ node = find_clib_rb ( pMap->root, key);\r
+ if ( node == (struct clib_rb_node*)0 ) \r
+ return clib_false;\r
+\r
+ get_raw_clib_object ( node->value, value );\r
+\r
+ return clib_true;\r
+\r
+}\r
+\r
+clib_error \r
+delete_clib_map ( struct clib_map* x) {\r
+ clib_error rc = CLIB_ERROR_SUCCESS;\r
+ if ( x != (struct clib_map*)0 ){\r
+ rc = delete_clib_rb ( x->root );\r
+ free ( x );\r
+ }\r
+ return rc;\r
+}\r
+\r
+static struct clib_rb_node *\r
+minimum_clib_map( struct clib_map *x ) {\r
+ return minimum_clib_rb( x->root, x->root->root);\r
+}\r
+\r