PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

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