CAvl_header.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file CAvl_header.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. // Preprocessor inputs:
  30. // CAVL_PARAM_NAME - name of this data structure
  31. // CAVL_PARAM_FEATURE_COUNTS - whether to keep count information (0 or 1)
  32. // CAVL_PARAM_FEATURE_KEYS_ARE_INDICES - (0 or 1) whether to assume the keys are entry indices
  33. // (number of entries lesser than given entry). If yes, CAVL_PARAM_TYPE_KEY is unused.
  34. // Requires CAVL_PARAM_FEATURE_COUNTS.
  35. // CAVL_PARAM_FEATURE_NOKEYS - define to 1 if there is no need for a lookup operation
  36. // CAVL_PARAM_FEATURE_ASSOC - define to 1 for computation of an associative operation on subtrees.
  37. // If enabled, the following macros must be defined: CAVL_PARAM_TYPE_ASSOC,
  38. // CAVL_PARAM_VALUE_ASSOC_ZERO, CAVL_PARAM_FUN_ASSOC_VALUE,
  39. // CAVL_PARAM_FUN_ASSOC_OPER, CAVL_PARAM_MEMBER_ASSOC.
  40. // CAVL_PARAM_TYPE_ENTRY - type of entry
  41. // CAVL_PARAM_TYPE_LINK - type of entry link (usually pointer or index)
  42. // CAVL_PARAM_TYPE_KEY - type of key (only if not CAVL_PARAM_FEATURE_KEYS_ARE_INDICES and
  43. // not CAVL_PARAM_FEATURE_NOKEYS)
  44. // CAVL_PARAM_TYPE_ARG - type of argument pass through to callbacks
  45. // CAVL_PARAM_TYPE_COUNT - type of count (only if CAVL_PARAM_FEATURE_COUNTS)
  46. // CAVL_PARAM_TYPE_ASSOC - type of associative operation result
  47. // CAVL_PARAM_VALUE_COUNT_MAX - maximum value of count (type is CAVL_PARAM_TYPE_COUNT)
  48. // CAVL_PARAM_VALUE_NULL - value of invalid link (type is CAVL_PARAM_TYPE_LINK)
  49. // CAVL_PARAM_VALUE_ASSOC_ZERO - zero value for associative operation (type is CAVL_PARAM_TYPE_ASSOC).
  50. // This must be both a left- and right-identity for the associative operation.
  51. // CAVL_PARAM_FUN_DEREF(arg, link) - dereference a non-null link; returns pointer to CAVL_PARAM_TYPE_LINK
  52. // CAVL_PARAM_FUN_COMPARE_ENTRIES(arg, entry1, entry2) - compare to entries; returns -1/0/1
  53. // CAVL_PARAM_FUN_COMPARE_KEY_ENTRY(arg, key1, entry2) - compare key and entry; returns -1/0/1
  54. // CAVL_PARAM_FUN_ASSOC_VALUE(arg, entry) - get value of a node for associative operation.
  55. // The result will be cast to CAVL_PARAM_TYPE_ASSOC.
  56. // CAVL_PARAM_FUN_ASSOC_OPER(arg, value1, value2) - compute the associative operation on two values.
  57. // The type of the two values is CAVL_PARAM_TYPE_ASSOC, and the result will be cast to
  58. // CAVL_PARAM_TYPE_ASSOC.
  59. // CAVL_PARAM_MEMBER_CHILD - name of the child member in entry (type is CAVL_PARAM_TYPE_LINK[2])
  60. // CAVL_PARAM_MEMBER_BALANCE - name of the balance member in entry (type is any signed integer)
  61. // CAVL_PARAM_MEMBER_PARENT - name of the parent member in entry (type is CAVL_PARAM_TYPE_LINK)
  62. // CAVL_PARAM_MEMBER_COUNT - name of the count member in entry (type is CAVL_PARAM_TYPE_COUNT)
  63. // (only if CAVL_PARAM_FEATURE_COUNTS)
  64. // CAVL_PARAM_MEMBER_ASSOC - name of assoc member in entry (type is CAVL_PARAM_TYPE_ASSOC)
  65. #ifndef BADVPN_CAVL_H
  66. #error CAvl.h has not been included
  67. #endif
  68. #if CAVL_PARAM_FEATURE_KEYS_ARE_INDICES && !CAVL_PARAM_FEATURE_COUNTS
  69. #error CAVL_PARAM_FEATURE_KEYS_ARE_INDICES requires CAVL_PARAM_FEATURE_COUNTS
  70. #endif
  71. #if CAVL_PARAM_FEATURE_KEYS_ARE_INDICES && CAVL_PARAM_FEATURE_NOKEYS
  72. #error CAVL_PARAM_FEATURE_KEYS_ARE_INDICES and CAVL_PARAM_FEATURE_NOKEYS cannot be used together
  73. #endif
  74. // types
  75. #define CAvl CAVL_PARAM_NAME
  76. #define CAvlEntry CAVL_PARAM_TYPE_ENTRY
  77. #define CAvlLink CAVL_PARAM_TYPE_LINK
  78. #define CAvlRef MERGE(CAVL_PARAM_NAME, Ref)
  79. #define CAvlArg CAVL_PARAM_TYPE_ARG
  80. #define CAvlKey CAVL_PARAM_TYPE_KEY
  81. #define CAvlCount CAVL_PARAM_TYPE_COUNT
  82. #define CAvlAssoc CAVL_PARAM_TYPE_ASSOC
  83. // non-object public functions
  84. #define CAvlIsNullRef MERGE(CAvl, IsNullRef)
  85. #define CAvlIsValidRef MERGE(CAvl, IsValidRef)
  86. #define CAvlDeref MERGE(CAvl, Deref)
  87. // public functions
  88. #define CAvl_Init MERGE(CAvl, _Init)
  89. #define CAvl_Insert MERGE(CAvl, _Insert)
  90. #define CAvl_InsertAt MERGE(CAvl, _InsertAt)
  91. #define CAvl_Remove MERGE(CAvl, _Remove)
  92. #define CAvl_Lookup MERGE(CAvl, _Lookup)
  93. #define CAvl_LookupExact MERGE(CAvl, _LookupExact)
  94. #define CAvl_GetFirstGreater MERGE(CAvl, _GetFirstGreater)
  95. #define CAvl_GetLastLesser MERGE(CAvl, _GetLastLesser)
  96. #define CAvl_GetFirstGreaterEqual MERGE(CAvl, _GetFirstGreaterEqual)
  97. #define CAvl_GetLastLesserEqual MERGE(CAvl, _GetLastLesserEqual)
  98. #define CAvl_GetFirst MERGE(CAvl, _GetFirst)
  99. #define CAvl_GetLast MERGE(CAvl, _GetLast)
  100. #define CAvl_GetNext MERGE(CAvl, _GetNext)
  101. #define CAvl_GetPrev MERGE(CAvl, _GetPrev)
  102. #define CAvl_IsEmpty MERGE(CAvl, _IsEmpty)
  103. #define CAvl_Verify MERGE(CAvl, _Verify)
  104. #define CAvl_Count MERGE(CAvl, _Count)
  105. #define CAvl_IndexOf MERGE(CAvl, _IndexOf)
  106. #define CAvl_GetAt MERGE(CAvl, _GetAt)
  107. #define CAvl_AssocSum MERGE(CAvl, _AssocSum)
  108. #define CAvl_ExclusiveAssocPrefixSum MERGE(CAvl, _ExclusiveAssocPrefixSum)
  109. #define CAvl_FindLastExclusiveAssocPrefixSumLesserEqual MERGE(CAvl, _FindLastExclusiveAssocPrefixSumLesserEqual)
  110. // private stuff
  111. #define CAvl_link(entry) ((entry).ptr->CAVL_PARAM_MEMBER_CHILD)
  112. #define CAvl_balance(entry) ((entry).ptr->CAVL_PARAM_MEMBER_BALANCE)
  113. #define CAvl_parent(entry) ((entry).ptr->CAVL_PARAM_MEMBER_PARENT)
  114. #define CAvl_count(entry) ((entry).ptr->CAVL_PARAM_MEMBER_COUNT)
  115. #define CAvl_assoc(entry) ((entry).ptr->CAVL_PARAM_MEMBER_ASSOC)
  116. #define CAvl_nulllink MERGE(CAvl, __nulllink)
  117. #define CAvl_nullref MERGE(CAvl, __nullref)
  118. #define CAvl_compare_entries MERGE(CAVL_PARAM_NAME, _compare_entries)
  119. #define CAvl_compare_key_entry MERGE(CAVL_PARAM_NAME, _compare_key_entry)
  120. #define CAvl_compute_node_assoc MERGE(CAVL_PARAM_NAME, _compute_node_assoc)
  121. #define CAvl_check_parent MERGE(CAVL_PARAM_NAME, _check_parent)
  122. #define CAvl_verify_recurser MERGE(CAVL_PARAM_NAME, _verify_recurser)
  123. #define CAvl_assert_tree MERGE(CAVL_PARAM_NAME, _assert_tree)
  124. #define CAvl_update_count_from_children MERGE(CAVL_PARAM_NAME, _update_count_from_children)
  125. #define CAvl_rotate MERGE(CAVL_PARAM_NAME, _rotate)
  126. #define CAvl_subtree_min MERGE(CAVL_PARAM_NAME, _subtree_min)
  127. #define CAvl_subtree_max MERGE(CAVL_PARAM_NAME, _subtree_max)
  128. #define CAvl_replace_subtree_fix_assoc MERGE(CAVL_PARAM_NAME, _replace_subtree_fix_counts)
  129. #define CAvl_swap_for_remove MERGE(CAVL_PARAM_NAME, _swap_entries)
  130. #define CAvl_rebalance MERGE(CAVL_PARAM_NAME, _rebalance)
  131. #define CAvl_child_count MERGE(CAvl, __child_count)
  132. #define CAvl_MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
  133. #define CAvl_OPTNEG(_a, _neg) ((_neg) ? -(_a) : (_a))