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