/src/lib/storage/dictionary/abstract_avl_dictionary.e

http://github.com/tybor/Liberty · Specman e · 223 lines · 165 code · 29 blank · 29 comment · 10 complexity · c93ad5db31406f444ec0656558b16c6f 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 ABSTRACT_AVL_DICTIONARY[V_, K_]
  5. --
  6. -- Associative memory. Values of type `V_' are stored using Keys of type `K_'.
  7. --
  8. -- Efficient implementation of DICTIONARY using an AVL balanced tree. AVL stands for the names of G. M. Adel'son-Velskii
  9. -- and E. M. Landis, two Russian mathematicians who first came up with this method of keeping the tree balanced.
  10. --
  11. inherit
  12. SIMPLE_DICTIONARY[V_, K_]
  13. redefine
  14. occurrences, fast_occurrences, key_at, fast_key_at, copy,
  15. new_iterator_on_items,
  16. default_create
  17. end
  18. insert
  19. AVL_TREE[K_]
  20. rename
  21. item_memory as key_memory
  22. export
  23. {ITERATOR_ON_AVL_DICTIONARY_ITEMS, ITERATOR_ON_AVL_DICTIONARY_KEYS, ITERATOR_ON_AVL_DICTIONARY} root
  24. redefine
  25. default_create
  26. end
  27. feature {ANY}
  28. capacity: INTEGER 0
  29. at (k: K_): V_
  30. do
  31. Result := root.at(k).value
  32. end
  33. fast_at (k: K_): V_
  34. do
  35. Result := root.fast_at(k).value
  36. end
  37. reference_at (k: K_): V_
  38. local
  39. n: ABSTRACT_AVL_DICTIONARY_NODE[V_, K_]
  40. do
  41. if root /= Void then
  42. n := root.at(k)
  43. if n /= Void then
  44. Result := n.value
  45. end
  46. end
  47. end
  48. fast_reference_at (k: K_): V_
  49. local
  50. n: ABSTRACT_AVL_DICTIONARY_NODE[V_, K_]
  51. do
  52. if root /= Void then
  53. n := root.fast_at(k)
  54. if n /= Void then
  55. Result := n.value
  56. end
  57. end
  58. end
  59. put, add (v: V_; k: K_)
  60. do
  61. value_memory := v
  62. key_memory := k
  63. root := do_insert(root)
  64. next_generation
  65. end
  66. fast_put (v: V_; k: K_)
  67. do
  68. value_memory := v
  69. key_memory := k
  70. root := fast_do_insert(root)
  71. next_generation
  72. end
  73. occurrences (v: V_): INTEGER
  74. do
  75. if root /= Void then
  76. Result := root.occurrences(v)
  77. end
  78. end
  79. fast_occurrences (v: V_): INTEGER
  80. do
  81. if root /= Void then
  82. Result := root.fast_occurrences(v)
  83. end
  84. end
  85. key_at (v: V_): K_
  86. do
  87. Result := root.key_at(v)
  88. end
  89. fast_key_at (v: V_): K_
  90. do
  91. Result := root.fast_key_at(v)
  92. end
  93. clear_count, clear_count_and_capacity
  94. do
  95. if not is_empty then
  96. clear_nodes(root)
  97. root := Void
  98. count := 0
  99. map_dirty := True
  100. end
  101. next_generation
  102. end
  103. set_item (v: V_; index: INTEGER)
  104. do
  105. if map_dirty then
  106. build_map
  107. end
  108. map.item(index - 1).set_value(v)
  109. next_generation
  110. end
  111. item (index: INTEGER): V_
  112. do
  113. if map_dirty then
  114. build_map
  115. end
  116. Result := map.item(index - 1).value
  117. end
  118. key (index: INTEGER): K_
  119. do
  120. if map_dirty then
  121. build_map
  122. end
  123. Result := map.item(index - 1).key
  124. end
  125. new_iterator_on_keys: ITERATOR[K_]
  126. do
  127. create {ITERATOR_ON_AVL_DICTIONARY_KEYS[V_, K_]} Result.make(Current)
  128. end
  129. new_iterator_on_items: ITERATOR[V_]
  130. do
  131. create {ITERATOR_ON_AVL_DICTIONARY_ITEMS[V_, K_]} Result.make(Current)
  132. end
  133. new_iterator: ITERATOR[TUPLE[V_, K_]]
  134. do
  135. create {ITERATOR_ON_AVL_DICTIONARY[V_, K_]} Result.make(Current)
  136. end
  137. internal_key (k: K_): K_
  138. do
  139. Result := root.at(k).key
  140. end
  141. copy (other: like Current)
  142. do
  143. make
  144. Precursor(other)
  145. next_generation
  146. end
  147. feature {}
  148. value_memory: V_
  149. set_value_and_key (n: like a_new_node)
  150. do
  151. n.set(value_memory, key_memory)
  152. end
  153. set_value (n: like a_new_node)
  154. do
  155. n.set_value(value_memory)
  156. end
  157. a_new_node: ABSTRACT_AVL_DICTIONARY_NODE[V_, K_]
  158. deferred
  159. end
  160. exchange_and_discard (n1, n2: like root)
  161. do
  162. map_dirty := True
  163. n1.set_key(n2.key)
  164. n1.set_value(n2.value)
  165. rebalance := True
  166. count := count - 1
  167. discard_node(n2)
  168. end
  169. make, default_create
  170. do
  171. create map.make(0)
  172. next_generation
  173. end
  174. end -- class ABSTRACT_AVL_DICTIONARY
  175. --
  176. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  177. --
  178. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  179. -- of this software and associated documentation files (the "Software"), to deal
  180. -- in the Software without restriction, including without limitation the rights
  181. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  182. -- copies of the Software, and to permit persons to whom the Software is
  183. -- furnished to do so, subject to the following conditions:
  184. --
  185. -- The above copyright notice and this permission notice shall be included in
  186. -- all copies or substantial portions of the Software.
  187. --
  188. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  189. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  190. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  191. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  192. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  193. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  194. -- THE SOFTWARE.