c_set.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_set*
  26. new_clib_set ( clib_compare fn_c, clib_destroy fn_d) {
  27. struct clib_set *pSet = (struct clib_set*)malloc(sizeof(struct clib_set));
  28. if (pSet == (struct clib_set*)0 )
  29. return (struct clib_set*)0 ;
  30. pSet->root = new_clib_rb (fn_c, fn_d, (void*)0);
  31. if (pSet->root == (struct clib_rb*)0)
  32. return (struct clib_set*)0 ;
  33. return pSet;
  34. }
  35. clib_error
  36. insert_clib_set (struct clib_set *pSet, void *key, size_t key_size) {
  37. if (pSet == (struct clib_set*)0 )
  38. return CLIB_SET_NOT_INITIALIZED;
  39. return insert_clib_rb ( pSet->root, key, key_size, (void*)0, 0);
  40. }
  41. clib_bool
  42. exists_clib_set ( struct clib_set *pSet, void *key) {
  43. clib_bool found = clib_false;
  44. struct clib_rb_node* node;
  45. if (pSet == (struct clib_set*)0 )
  46. return clib_false;
  47. node = find_clib_rb ( pSet->root, key);
  48. if ( node != (struct clib_rb_node*)0 ) {
  49. return clib_true;
  50. }
  51. return found;
  52. }
  53. clib_error
  54. remove_clib_set ( struct clib_set *pSet, void *key) {
  55. clib_error rc = CLIB_ERROR_SUCCESS;
  56. struct clib_rb_node* node;
  57. if (pSet == (struct clib_set*)0 )
  58. return CLIB_SET_NOT_INITIALIZED;
  59. node = remove_clib_rb ( pSet->root, key );
  60. if ( node != (struct clib_rb_node*)0 ) {
  61. /*free ( node->raw_data.key);
  62. free ( node );*/
  63. }
  64. return rc;
  65. }
  66. clib_bool
  67. find_clib_set ( struct clib_set *pSet, void *key, void* outKey) {
  68. struct clib_rb_node* node;
  69. if (pSet == (struct clib_set*)0 )
  70. return clib_false;
  71. node = find_clib_rb ( pSet->root, key);
  72. if ( node == (struct clib_rb_node*)0 )
  73. return clib_false;
  74. get_raw_clib_object ( node->key, outKey );
  75. return clib_true;
  76. }
  77. clib_error
  78. delete_clib_set ( struct clib_set* x) {
  79. clib_error rc = CLIB_ERROR_SUCCESS;
  80. if ( x != (struct clib_set*)0 ){
  81. rc = delete_clib_rb ( x->root );
  82. free ( x );
  83. }
  84. return rc;
  85. }
  86. static struct clib_rb_node *
  87. minimum_clib_set( struct clib_set *x ) {
  88. return minimum_clib_rb( x->root, x->root->root);
  89. }