/src/lib/storage/internal/avl_tree_node.e

http://github.com/tybor/Liberty · Specman e · 233 lines · 171 code · 22 blank · 40 comment · 17 complexity · b2b6df87db9488edb0c78b720e4a2ecb MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class AVL_TREE_NODE[E_]
  5. --
  6. -- Auxiliary class to implement AVL_SET.
  7. --
  8. -- This a classic implementation of an AVL tree (balanced tree first designed by Adelson-Velskii and
  9. -- Landis (hence A.V.L.), 1960)
  10. --
  11. inherit
  12. AVL_TREE_NODE_ANY
  13. insert
  14. AVL_CONSTANTS
  15. feature {ANY}
  16. out_in_tagged_out_memory
  17. do
  18. item.out_in_tagged_out_memory
  19. tagged_out_memory.extend('(')
  20. if left = Void then
  21. tagged_out_memory.append(once "Void")
  22. else
  23. left.out_in_tagged_out_memory
  24. end
  25. tagged_out_memory.extend(',')
  26. if right = Void then
  27. tagged_out_memory.append(once "Void")
  28. else
  29. right.out_in_tagged_out_memory
  30. end
  31. tagged_out_memory.extend(')')
  32. end
  33. safe_equal: SAFE_EQUAL[E_]
  34. feature {AVL_TREE_NODE, AVL_TREE, AVL_TREE_ITERATOR}
  35. left, right: like Current
  36. item: E_
  37. balance: INTEGER
  38. -- Balance factor; either `balanced' (the tree is balanced),
  39. -- `imbalanced_left' (the left branch is the longer) or
  40. -- `imbalanced_right' (the right branch is the longer)
  41. count: INTEGER
  42. do
  43. Result := 1
  44. if left /= Void then
  45. Result := Result + left.count
  46. end
  47. if right /= Void then
  48. Result := Result + right.count
  49. end
  50. end
  51. height: INTEGER
  52. do
  53. if left /= Void then
  54. Result := left.height
  55. end
  56. if right /= Void then
  57. Result := Result.max(right.height)
  58. end
  59. Result := Result + 1
  60. end
  61. map_in (map: COLLECTION[like Current])
  62. require
  63. map /= Void
  64. do
  65. if left /= Void then
  66. left.map_in(map)
  67. end
  68. map.add_last(Current)
  69. if right /= Void then
  70. right.map_in(map)
  71. end
  72. ensure
  73. map.count = old map.count + count
  74. end
  75. has (e: like item): BOOLEAN
  76. -- Is element `e' in the tree?
  77. do
  78. Result := safe_equal.test(item, e)
  79. if not Result then
  80. if ordered(e, item) then
  81. Result := left /= Void and then left.has(e)
  82. else
  83. Result := right /= Void and then right.has(e)
  84. end
  85. end
  86. end
  87. fast_has (e: like item): BOOLEAN
  88. -- Is element `e' in the tree?
  89. do
  90. Result := item = e
  91. if not Result and then not safe_equal.test(item, e) then
  92. if ordered(e, item) then
  93. Result := left /= Void and then left.fast_has(e)
  94. else
  95. Result := right /= Void and then right.fast_has(e)
  96. end
  97. end
  98. ensure
  99. Result implies count > 0
  100. end
  101. at (e: like item): like Current
  102. -- Is element `e' in the tree?
  103. do
  104. if safe_equal.test(item, e) then
  105. Result := Current
  106. elseif ordered(e, item) then
  107. if left /= Void then
  108. Result := left.at(e)
  109. end
  110. else
  111. if right /= Void then
  112. Result := right.at(e)
  113. end
  114. end
  115. end
  116. set_item (i: like item)
  117. require
  118. -- Equality admitted for the free list
  119. left /= Void implies safe_equal.test(left.item, i) or else ordered(left.item, i)
  120. right /= Void implies ordered(i, right.item)
  121. do
  122. item := i
  123. ensure
  124. item = i
  125. end
  126. set_left (l: like left)
  127. require
  128. -- Equality admitted for the free list
  129. l /= Void implies safe_equal.test(l.item, item) or else ordered(l.item, item)
  130. do
  131. left := l
  132. ensure
  133. left = l
  134. end
  135. set_right (r: like right)
  136. require
  137. r /= Void implies ordered(item, r.item)
  138. do
  139. right := r
  140. ensure
  141. right = r
  142. end
  143. set_balance (b: like balance)
  144. do
  145. balance := b
  146. ensure
  147. balance = b
  148. end
  149. feature {AVL_TREE} -- Rotations:
  150. rotate_right: like Current
  151. -- Proceeds to some reorganisation and returns the upper node.
  152. local
  153. left_right: like Current
  154. do
  155. Result := left
  156. left_right := left.right
  157. left.set_right(Current)
  158. set_left(left_right)
  159. ensure
  160. Result /= Void
  161. end
  162. rotate_left: like Current
  163. -- Proceeds to some reorganisation and returns the upper node.
  164. local
  165. right_left: like Current
  166. do
  167. Result := right
  168. right_left := right.left
  169. right.set_left(Current)
  170. set_right(right_left)
  171. ensure
  172. Result /= Void
  173. end
  174. feature {}
  175. ordered (e1, e2: E_): BOOLEAN
  176. -- True if [e1, e2] is a correctly ordered sequence; usually, e1 < e2
  177. require
  178. e1 /= Void
  179. e2 /= Void
  180. deferred
  181. end
  182. feature {RECYCLING_POOL}
  183. recycle
  184. local
  185. e: E_
  186. do
  187. left := Void
  188. right := Void
  189. item := e
  190. end
  191. end -- class AVL_TREE_NODE
  192. --
  193. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  194. --
  195. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  196. -- of this software and associated documentation files (the "Software"), to deal
  197. -- in the Software without restriction, including without limitation the rights
  198. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  199. -- copies of the Software, and to permit persons to whom the Software is
  200. -- furnished to do so, subject to the following conditions:
  201. --
  202. -- The above copyright notice and this permission notice shall be included in
  203. -- all copies or substantial portions of the Software.
  204. --
  205. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  206. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  207. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  208. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  209. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  210. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  211. -- THE SOFTWARE.