archive_rb.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <matt@3am-software.com>.
  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_
  32. #define ARCHIVE_RB_H_
  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_FOREACH(N, T) \
  49. for ((N) = ARCHIVE_RB_TREE_MIN(T); (N); \
  50. (N) = __archive_rb_tree_iterate((T), (N), ARCHIVE_RB_DIR_RIGHT))
  51. #define ARCHIVE_RB_TREE_FOREACH_REVERSE(N, T) \
  52. for ((N) = ARCHIVE_RB_TREE_MAX(T); (N); \
  53. (N) = __archive_rb_tree_iterate((T), (N), ARCHIVE_RB_DIR_LEFT))
  54. /*
  55. * archive_rbto_compare_nodes_fn:
  56. * return a positive value if the first node < the second node.
  57. * return a negative value if the first node > the second node.
  58. * return 0 if they are considered same.
  59. *
  60. * archive_rbto_compare_key_fn:
  61. * return a positive value if the node < the key.
  62. * return a negative value if the node > the key.
  63. * return 0 if they are considered same.
  64. */
  65. typedef signed int (*const archive_rbto_compare_nodes_fn)(const struct archive_rb_node *,
  66. const struct archive_rb_node *);
  67. typedef signed int (*const archive_rbto_compare_key_fn)(const struct archive_rb_node *,
  68. const void *);
  69. struct archive_rb_tree_ops {
  70. archive_rbto_compare_nodes_fn rbto_compare_nodes;
  71. archive_rbto_compare_key_fn rbto_compare_key;
  72. };
  73. struct archive_rb_tree {
  74. struct archive_rb_node *rbt_root;
  75. const struct archive_rb_tree_ops *rbt_ops;
  76. };
  77. void __archive_rb_tree_init(struct archive_rb_tree *,
  78. const struct archive_rb_tree_ops *);
  79. int __archive_rb_tree_insert_node(struct archive_rb_tree *,
  80. struct archive_rb_node *);
  81. struct archive_rb_node *
  82. __archive_rb_tree_find_node(struct archive_rb_tree *, const void *);
  83. struct archive_rb_node *
  84. __archive_rb_tree_find_node_geq(struct archive_rb_tree *, const void *);
  85. struct archive_rb_node *
  86. __archive_rb_tree_find_node_leq(struct archive_rb_tree *, const void *);
  87. void __archive_rb_tree_remove_node(struct archive_rb_tree *, struct archive_rb_node *);
  88. struct archive_rb_node *
  89. __archive_rb_tree_iterate(struct archive_rb_tree *,
  90. struct archive_rb_node *, const unsigned int);
  91. #endif /* ARCHIVE_RB_H_*/