archive_rb.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*-
  2. * Copyright (c) 2001 The NetBSD Foundation, Inc.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to The NetBSD Foundation
  6. * by Matt Thomas <[email protected]>.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * Based on NetBSD: rb.h,v 1.13 2009/08/16 10:57:01 yamt Exp
  30. */
  31. #ifndef ARCHIVE_RB_H_INCLUDED
  32. #define ARCHIVE_RB_H_INCLUDED
  33. struct archive_rb_node {
  34. struct archive_rb_node *rb_nodes[2];
  35. /*
  36. * rb_info contains the two flags and the parent back pointer.
  37. * We put the two flags in the low two bits since we know that
  38. * rb_node will have an alignment of 4 or 8 bytes.
  39. */
  40. uintptr_t rb_info;
  41. };
  42. #define ARCHIVE_RB_DIR_LEFT 0
  43. #define ARCHIVE_RB_DIR_RIGHT 1
  44. #define ARCHIVE_RB_TREE_MIN(T) \
  45. __archive_rb_tree_iterate((T), NULL, ARCHIVE_RB_DIR_LEFT)
  46. #define ARCHIVE_RB_TREE_MAX(T) \
  47. __archive_rb_tree_iterate((T), NULL, ARCHIVE_RB_DIR_RIGHT)
  48. #define ARCHIVE_RB_TREE_NEXT(T, N) \
  49. __archive_rb_tree_iterate((T), (N), ARCHIVE_RB_DIR_RIGHT)
  50. #define ARCHIVE_RB_TREE_PREV(T, N) \
  51. __archive_rb_tree_iterate((T), (N), ARCHIVE_RB_DIR_LEFT)
  52. #define ARCHIVE_RB_TREE_FOREACH(N, T) \
  53. for ((N) = ARCHIVE_RB_TREE_MIN(T); (N); \
  54. (N) = ARCHIVE_RB_TREE_NEXT((T), (N)))
  55. #define ARCHIVE_RB_TREE_FOREACH_REVERSE(N, T) \
  56. for ((N) = ARCHIVE_RB_TREE_MAX(T); (N); \
  57. (N) = ARCHIVE_RB_TREE_PREV((T), (N)))
  58. #define ARCHIVE_RB_TREE_FOREACH_SAFE(N, T, S) \
  59. for ((N) = ARCHIVE_RB_TREE_MIN(T); \
  60. (N) && ((S) = ARCHIVE_RB_TREE_NEXT((T), (N)), 1); \
  61. (N) = (S))
  62. #define ARCHIVE_RB_TREE_FOREACH_REVERSE_SAFE(N, T, S) \
  63. for ((N) = ARCHIVE_RB_TREE_MAX(T); \
  64. (N) && ((S) = ARCHIVE_RB_TREE_PREV((T), (N)), 1); \
  65. (N) = (S))
  66. /*
  67. * archive_rbto_compare_nodes_fn:
  68. * return a positive value if the first node < the second node.
  69. * return a negative value if the first node > the second node.
  70. * return 0 if they are considered same.
  71. *
  72. * archive_rbto_compare_key_fn:
  73. * return a positive value if the node < the key.
  74. * return a negative value if the node > the key.
  75. * return 0 if they are considered same.
  76. */
  77. typedef signed int (*const archive_rbto_compare_nodes_fn)(const struct archive_rb_node *,
  78. const struct archive_rb_node *);
  79. typedef signed int (*const archive_rbto_compare_key_fn)(const struct archive_rb_node *,
  80. const void *);
  81. struct archive_rb_tree_ops {
  82. archive_rbto_compare_nodes_fn rbto_compare_nodes;
  83. archive_rbto_compare_key_fn rbto_compare_key;
  84. };
  85. struct archive_rb_tree {
  86. struct archive_rb_node *rbt_root;
  87. const struct archive_rb_tree_ops *rbt_ops;
  88. };
  89. void __archive_rb_tree_init(struct archive_rb_tree *,
  90. const struct archive_rb_tree_ops *);
  91. int __archive_rb_tree_insert_node(struct archive_rb_tree *,
  92. struct archive_rb_node *);
  93. struct archive_rb_node *
  94. __archive_rb_tree_find_node(struct archive_rb_tree *, const void *);
  95. struct archive_rb_node *
  96. __archive_rb_tree_find_node_geq(struct archive_rb_tree *, const void *);
  97. struct archive_rb_node *
  98. __archive_rb_tree_find_node_leq(struct archive_rb_tree *, const void *);
  99. void __archive_rb_tree_remove_node(struct archive_rb_tree *, struct archive_rb_node *);
  100. struct archive_rb_node *
  101. __archive_rb_tree_iterate(struct archive_rb_tree *,
  102. struct archive_rb_node *, const unsigned int);
  103. #endif /* ARCHIVE_RB_H_*/