rbtree.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*====================================================================*
  2. - Copyright (C) 2001 Leptonica. All rights reserved.
  3. -
  4. - Redistribution and use in source and binary forms, with or without
  5. - modification, are permitted provided that the following conditions
  6. - are met:
  7. - 1. Redistributions of source code must retain the above copyright
  8. - notice, this list of conditions and the following disclaimer.
  9. - 2. Redistributions in binary form must reproduce the above
  10. - copyright notice, this list of conditions and the following
  11. - disclaimer in the documentation and/or other materials
  12. - provided with the distribution.
  13. -
  14. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
  18. - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *====================================================================*/
  26. /*
  27. * Modified from the excellent code here:
  28. * http://en.literateprograms.org/Red-black_tree_(C)?oldid=19567
  29. * which has been placed in the public domain under the Creative Commons
  30. * CC0 1.0 waiver (http://creativecommons.org/publicdomain/zero/1.0/).
  31. *
  32. * When the key is generated from a hash (e.g., string --> uint64),
  33. * there is always the possibility of having collisions, but to make
  34. * the collision probability very low requires using a large hash.
  35. * For that reason, the key types are 64 bit quantities, which will result
  36. * in a negligible probabililty of collisions for millions of hashed values.
  37. * Using 8 byte keys instead of 4 byte keys requires a little more
  38. * storage, but the simplification in being able to ignore collisions
  39. * with the red-black trees for most applications is worth it.
  40. */
  41. #ifndef LEPTONICA_RBTREE_H
  42. #define LEPTONICA_RBTREE_H
  43. /*! The three valid key types for red-black trees, maps and sets. */
  44. enum {
  45. L_INT_TYPE = 1,
  46. L_UINT_TYPE = 2,
  47. L_FLOAT_TYPE = 3
  48. };
  49. /*!
  50. * Storage for keys and values for red-black trees, maps and sets.
  51. * <pre>
  52. * Note:
  53. * (1) Keys and values of the valid key types are all 64-bit
  54. * (2) (void *) can be used for values but not for keys.
  55. * </pre>
  56. */
  57. union Rb_Type {
  58. l_int64 itype;
  59. l_uint64 utype;
  60. l_float64 ftype;
  61. void *ptype;
  62. };
  63. typedef union Rb_Type RB_TYPE;
  64. struct L_Rbtree {
  65. struct L_Rbtree_Node *root;
  66. l_int32 keytype;
  67. };
  68. typedef struct L_Rbtree L_RBTREE;
  69. typedef struct L_Rbtree L_AMAP; /* hide underlying implementation for map */
  70. typedef struct L_Rbtree L_ASET; /* hide underlying implementation for set */
  71. struct L_Rbtree_Node {
  72. union Rb_Type key;
  73. union Rb_Type value;
  74. struct L_Rbtree_Node *left;
  75. struct L_Rbtree_Node *right;
  76. struct L_Rbtree_Node *parent;
  77. l_int32 color;
  78. };
  79. typedef struct L_Rbtree_Node L_RBTREE_NODE;
  80. typedef struct L_Rbtree_Node L_AMAP_NODE; /* hide tree implementation */
  81. typedef struct L_Rbtree_Node L_ASET_NODE; /* hide tree implementation */
  82. #endif /* LEPTONICA_RBTREE_H */