PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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