/test/language/unclassified/aux_ph4_splay_dictionary.e

http://github.com/tybor/Liberty · Specman e · 254 lines · 212 code · 16 blank · 26 comment · 23 complexity · 3ec4b8f075abd01de25ae54d2799b48a MD5 · raw file

  1. -- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries.
  2. -- See the Copyright notice at the end of this file.
  3. --
  4. class AUX_PH4_SPLAY_DICTIONARY[E, I -> COMPARABLE]
  5. create {ANY}
  6. make
  7. feature {ANY}
  8. make
  9. do
  10. count := 0
  11. root := Void
  12. end
  13. count: INTEGER
  14. is_empty: BOOLEAN
  15. do
  16. Result := root = Void
  17. end
  18. has (index: I): BOOLEAN
  19. local
  20. tmp_node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  21. do
  22. if not is_empty then
  23. from
  24. tmp_node := root
  25. until
  26. tmp_node = Void or else Result = True
  27. loop
  28. if index = tmp_node.index then
  29. splay(tmp_node)
  30. Result := True
  31. elseif index < tmp_node.index then
  32. if tmp_node.left /= Void then
  33. tmp_node := tmp_node.left
  34. else
  35. tmp_node := Void
  36. end
  37. else
  38. if tmp_node.right /= Void then
  39. tmp_node := tmp_node.right
  40. else
  41. tmp_node := Void
  42. end
  43. end
  44. end
  45. end
  46. end
  47. item (index: I): E
  48. local
  49. chk: BOOLEAN
  50. do
  51. chk := has(index)
  52. Result := root.item
  53. end
  54. new_iterator: AUX_PH4_SPLAY_DICTIONARY_ITERATOR[I]
  55. do
  56. create Result.make(Void)
  57. end
  58. put (value: E; index: I)
  59. local
  60. elem, tmp: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  61. do
  62. create elem.make(value, index, Void, Void, Void)
  63. if is_empty then
  64. root := elem
  65. count := count + 1
  66. else
  67. from
  68. tmp := root
  69. until
  70. tmp = Void and then root /= Void
  71. loop
  72. check
  73. elem.index /= tmp.index
  74. end
  75. if elem.index < tmp.index then
  76. if tmp.left = Void then
  77. tmp.set_left(elem)
  78. elem.set_parent(tmp)
  79. splay(elem)
  80. count := count + 1
  81. tmp := Void
  82. else
  83. tmp := tmp.left
  84. end
  85. else
  86. if tmp.right = Void then
  87. tmp.set_right(elem)
  88. elem.set_parent(tmp)
  89. splay(elem)
  90. count := count + 1
  91. tmp := Void
  92. else
  93. tmp := tmp.right
  94. end
  95. end
  96. end
  97. end
  98. end
  99. lowest_node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  100. do
  101. Result := lowest_node_in_subtree(root)
  102. splay(Result)
  103. end
  104. next_highest (node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]): AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  105. do
  106. splay(node)
  107. if root.right /= Void then
  108. Result := lowest_node_in_subtree(root.right)
  109. splay(Result)
  110. end
  111. end
  112. lowest_node_in_subtree (node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]): AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  113. local
  114. elem: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  115. do
  116. from
  117. elem := node
  118. until
  119. elem = Void
  120. loop
  121. if elem.left = Void then
  122. Result := elem
  123. elem := Void
  124. else
  125. elem := elem.left
  126. end
  127. end
  128. end
  129. splay (node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I])
  130. local
  131. parent: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  132. do
  133. from
  134. until
  135. node = root
  136. loop
  137. if node.parent = root then
  138. -- In this case we only need to zig.
  139. parent := node.parent
  140. if node = parent.left then
  141. rotate_right(node)
  142. else
  143. rotate_left(node)
  144. end
  145. root := node -- Make sure root is update!
  146. elseif node = node.parent.left then
  147. if node.parent = node.parent.parent.right then
  148. rotate_right(node)
  149. rotate_left(node)
  150. else
  151. rotate_right(node.parent)
  152. rotate_right(node)
  153. end
  154. if node.parent = Void then
  155. -- Make sure to update the root if
  156. -- necessary.
  157. root := node
  158. end
  159. elseif node = node.parent.right then
  160. if node.parent = node.parent.parent.left then
  161. rotate_left(node)
  162. rotate_right(node)
  163. elseif node.parent = node.parent.parent.right then
  164. rotate_left(node.parent)
  165. rotate_left(node)
  166. end
  167. if node.parent = Void then
  168. -- Make sure to update the root if
  169. -- necessary.
  170. root := node
  171. end
  172. end
  173. end
  174. end
  175. rotate_right (node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I])
  176. local
  177. parent: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  178. do
  179. parent := node.parent
  180. parent.set_left(node.right)
  181. if node.right /= Void then
  182. node.right.set_parent(parent)
  183. end
  184. if parent.parent = Void then
  185. node.set_parent(Void)
  186. else
  187. node.set_parent(parent.parent)
  188. if parent = parent.parent.left then
  189. parent.parent.set_left(node)
  190. else
  191. parent.parent.set_right(node)
  192. end
  193. end
  194. node.set_right(parent)
  195. parent.set_parent(node)
  196. end
  197. rotate_left (node: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I])
  198. local
  199. parent: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  200. do
  201. parent := node.parent
  202. parent.set_right(node.left)
  203. if node.left /= Void then
  204. node.left.set_parent(parent)
  205. end
  206. if parent.parent = Void then
  207. node.set_parent(Void)
  208. else
  209. node.set_parent(parent.parent)
  210. if parent = parent.parent.left then
  211. parent.parent.set_left(node)
  212. else
  213. parent.parent.set_right(node)
  214. end
  215. end
  216. node.set_left(parent)
  217. parent.set_parent(node)
  218. end
  219. root: AUX_PH4_SPLAY_DICTIONARY_ITEM[E, I]
  220. end -- class AUX_PH4_SPLAY_DICTIONARY
  221. --
  222. -- ------------------------------------------------------------------------------------------------------------------------------
  223. -- Copyright notice below. Please read.
  224. --
  225. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  226. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  227. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  228. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  229. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  230. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  231. --
  232. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  233. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  234. --
  235. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  236. --
  237. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  238. -- ------------------------------------------------------------------------------------------------------------------------------