c_map.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  2. * This file is part of clib library
  3. * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  23. #include "c_lib.h"
  24. #include <stdio.h>
  25. struct clib_map*
  26. new_clib_map ( clib_compare fn_clib_k, clib_destroy fn_k_d,
  27. clib_destroy fn_v_d) {
  28. struct clib_map *pMap = (struct clib_map*)malloc(sizeof(struct clib_map));
  29. if (pMap == (struct clib_map*)0)
  30. return (struct clib_map*)0;
  31. pMap->root = new_clib_rb (fn_clib_k, fn_k_d, fn_v_d);
  32. if (pMap->root == (struct clib_rb*)0)
  33. return (struct clib_map*)0;
  34. return pMap;
  35. }
  36. clib_error
  37. insert_clib_map ( struct clib_map *pMap, void *key, size_t key_size, void *value, size_t value_size) {
  38. if (pMap == (struct clib_map*)0)
  39. return CLIB_MAP_NOT_INITIALIZED;
  40. return insert_clib_rb ( pMap->root, key, key_size, value, value_size);
  41. }
  42. clib_bool
  43. exists_clib_map ( struct clib_map *pMap, void *key) {
  44. clib_bool found = clib_false;
  45. struct clib_rb_node* node;
  46. if (pMap == (struct clib_map*)0)
  47. return clib_false;
  48. node = find_clib_rb ( pMap->root, key);
  49. if ( node != (struct clib_rb_node*)0 ) {
  50. return clib_true;
  51. }
  52. return found;
  53. }
  54. clib_error
  55. remove_clib_map ( struct clib_map *pMap, void *key) {
  56. clib_error rc = CLIB_ERROR_SUCCESS;
  57. struct clib_rb_node* node;
  58. if (pMap == (struct clib_map*)0)
  59. return CLIB_MAP_NOT_INITIALIZED;
  60. node = remove_clib_rb ( pMap->root, key );
  61. if ( node != (struct clib_rb_node*)0 ) {
  62. void* removed_node;
  63. get_raw_clib_object ( node->key, &removed_node );
  64. free ( removed_node);
  65. delete_clib_object ( node->key );
  66. get_raw_clib_object ( node->value, &removed_node );
  67. free ( removed_node);
  68. delete_clib_object ( node->value);
  69. free ( node );
  70. }
  71. return rc;
  72. }
  73. clib_bool
  74. find_clib_map ( struct clib_map *pMap, void *key, void **value) {
  75. struct clib_rb_node* node;
  76. if (pMap == (struct clib_map*)0)
  77. return clib_false;
  78. node = find_clib_rb ( pMap->root, key);
  79. if ( node == (struct clib_rb_node*)0 )
  80. return clib_false;
  81. get_raw_clib_object ( node->value, value );
  82. return clib_true;
  83. }
  84. clib_error
  85. delete_clib_map ( struct clib_map* x) {
  86. clib_error rc = CLIB_ERROR_SUCCESS;
  87. if ( x != (struct clib_map*)0 ){
  88. rc = delete_clib_rb ( x->root );
  89. free ( x );
  90. }
  91. return rc;
  92. }
  93. static struct clib_rb_node *
  94. minimum_clib_map( struct clib_map *x ) {
  95. return minimum_clib_rb( x->root, x->root->root);
  96. }