/src/lib/storage/internal/abstract_avl_dictionary_node.e
Specman e | 158 lines | 115 code | 14 blank | 29 comment | 11 complexity | c8e06423ecc006fa53ff75fb75997049 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_NODE[V_, K_] 5 -- 6 -- Auxiliary class to implement AVL_DICTIONARY. 7 -- 8 9inherit 10 AVL_TREE_NODE[K_] 11 rename 12 item as key, 13 set_item as set_key, 14 safe_equal as safe_equal_key 15 redefine 16 recycle 17 end 18 ANY_AVL_DICTIONARY_NODE 19 20feature {ABSTRACT_AVL_DICTIONARY, ABSTRACT_AVL_DICTIONARY_NODE, ITERATOR_ON_AVL_DICTIONARY_ITEMS, ITERATOR_ON_AVL_DICTIONARY} 21 safe_equal_value: SAFE_EQUAL[V_] 22 23 value: V_ 24 25 set_value (v: like value) 26 do 27 value := v 28 ensure 29 value = v 30 end 31 32 fast_at (k: like key): like Current 33 -- Is element `e' in the tree? 34 do 35 if key = k then 36 Result := Current 37 elseif safe_equal_key.test(key, k) then 38 -- because otherwise there would be an infinite recursion 39 --Result := Void 40 elseif ordered(k, key) then 41 if left /= Void then 42 Result := left.fast_at(k) 43 end 44 else 45 if right /= Void then 46 Result := right.fast_at(k) 47 end 48 end 49 end 50 51 occurrences (v: V_): INTEGER 52 do 53 Result := occurrences_(v, 0) 54 end 55 56 fast_occurrences (v: V_): INTEGER 57 do 58 Result := fast_occurrences_(v, 0) 59 end 60 61 key_at (v: V_): K_ 62 do 63 if safe_equal_value.test(v, value) then 64 Result := key 65 elseif left /= Void then 66 Result := left.key_at(v) 67 elseif right /= Void then 68 Result := right.key_at(v) 69 end 70 end 71 72 fast_key_at (v: V_): K_ 73 do 74 if v = value then 75 Result := key 76 elseif left /= Void then 77 Result := left.fast_key_at(v) 78 elseif right /= Void then 79 Result := right.fast_key_at(v) 80 end 81 end 82 83feature {ABSTRACT_AVL_DICTIONARY_NODE} 84 occurrences_ (v: V_; cnt: INTEGER): INTEGER 85 do 86 Result := cnt 87 if safe_equal_value.test(v, value) then 88 Result := Result + 1 89 end 90 if left /= Void then 91 Result := left.occurrences_(v, Result) 92 end 93 if right /= Void then 94 Result := right.occurrences_(v, Result) 95 end 96 ensure 97 Result >= cnt 98 end 99 100 fast_occurrences_ (v: V_; cnt: INTEGER): INTEGER 101 do 102 Result := cnt 103 if v = value then 104 Result := Result + 1 105 end 106 if left /= Void then 107 Result := left.fast_occurrences_(v, Result) 108 end 109 if right /= Void then 110 Result := right.fast_occurrences_(v, Result) 111 end 112 ensure 113 Result >= cnt 114 end 115 116feature {ABSTRACT_AVL_DICTIONARY} 117 set (v: like value; k: like key) 118 do 119 set_balance(balanced) 120 left := Void 121 right := Void 122 set_value(v) 123 set_key(k) 124 ensure 125 value = v 126 key = k 127 end 128 129feature {RECYCLING_POOL} 130 recycle 131 local 132 v: V_ 133 do 134 Precursor 135 value := v 136 end 137 138end -- class ABSTRACT_AVL_DICTIONARY_NODE 139-- 140-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 141-- 142-- Permission is hereby granted, free of charge, to any person obtaining a copy 143-- of this software and associated documentation files (the "Software"), to deal 144-- in the Software without restriction, including without limitation the rights 145-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 146-- copies of the Software, and to permit persons to whom the Software is 147-- furnished to do so, subject to the following conditions: 148-- 149-- The above copyright notice and this permission notice shall be included in 150-- all copies or substantial portions of the Software. 151-- 152-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 153-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 154-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 155-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 156-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 157-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 158-- THE SOFTWARE.