PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/storage/dictionary/array_dictionary.e

http://github.com/tybor/Liberty
Specman e | 232 lines | 173 code | 31 blank | 28 comment | 7 complexity | db2a7612786ac5777c789bcc0c1f4346 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class ARRAY_DICTIONARY[V_, K_]
  5   --
  6   -- This dictionary is sorted in the order in which the keys are added.
  7   -- Access time is not good at all: O(n)
  8   --
  9
 10inherit
 11   DICTIONARY[V_, K_]
 12      redefine
 13         new_iterator_on_items, copy
 14      end
 15
 16create {ANY}
 17   make, with_capacity
 18
 19feature {ANY} -- Basic access:
 20   has (k: K_): BOOLEAN
 21      do
 22         Result := keys_storage.has(k)
 23      end
 24
 25   at (k: K_): V_
 26      do
 27         Result := items_storage.item(keys_storage.first_index_of(k))
 28      end
 29
 30   reference_at (k: K_): V_
 31      local
 32         i: INTEGER
 33      do
 34         i := keys_storage.first_index_of(k)
 35         if keys_storage.valid_index(i) then
 36            Result := items_storage.item(i)
 37         end
 38      end
 39
 40   fast_has (k: K_): BOOLEAN
 41      do
 42         Result := keys_storage.fast_has(k)
 43      end
 44
 45   fast_at (k: K_): V_
 46      do
 47         Result := items_storage.item(keys_storage.fast_first_index_of(k))
 48      end
 49
 50   fast_reference_at (k: K_): V_
 51      local
 52         i: INTEGER
 53      do
 54         i := keys_storage.fast_first_index_of(k)
 55         if keys_storage.valid_index(i) then
 56            Result := items_storage.item(i)
 57         end
 58      end
 59
 60feature {ANY}
 61   put (v: V_; k: K_)
 62      local
 63         i: INTEGER
 64      do
 65         i := keys_storage.first_index_of(k)
 66         if keys_storage.valid_index(i) then
 67            items_storage.put(v, i)
 68         else
 69            add(v, k)
 70         end
 71      end
 72
 73   fast_put (v: V_; k: K_)
 74      local
 75         i: INTEGER
 76      do
 77         i := keys_storage.fast_first_index_of(k)
 78         if keys_storage.valid_index(i) then
 79            items_storage.put(v, i)
 80         else
 81            add(v, k)
 82         end
 83      end
 84
 85   add (v: V_; k: K_)
 86      do
 87         keys_storage.add_last(k)
 88         items_storage.add_last(v)
 89      end
 90
 91feature {ANY} -- Removing:
 92   remove (k: K_)
 93      local
 94         i: INTEGER
 95      do
 96         i := keys_storage.first_index_of(k)
 97         if keys_storage.valid_index(i) then
 98            keys_storage.remove(i)
 99            items_storage.remove(i)
100         end
101      end
102
103   fast_remove (k: K_)
104      local
105         i: INTEGER
106      do
107         i := keys_storage.fast_first_index_of(k)
108         if keys_storage.valid_index(i) then
109            keys_storage.remove(i)
110            items_storage.remove(i)
111         end
112      end
113
114   clear_count
115      do
116         keys_storage.clear_count
117         items_storage.clear_count
118      end
119
120   clear_count_and_capacity
121      do
122         keys_storage.clear_count_and_capacity
123         items_storage.clear_count_and_capacity
124      end
125
126   capacity: INTEGER
127      do
128         Result := keys_storage.capacity -- see also invariant
129      end
130
131   count: INTEGER
132      do
133         Result := keys_storage.count -- see also invariant
134      end
135
136feature {ANY} -- To provide iterating facilities:
137   set_item (v: V_; index: INTEGER)
138      do
139         items_storage.put(v, index - 1)
140      end
141
142   item (index: INTEGER): V_
143      do
144         Result := items_storage.item(index - 1)
145      end
146
147   key (index: INTEGER): K_
148      do
149         Result := keys_storage.item(index - 1)
150      end
151
152   new_iterator_on_items: ITERATOR[V_]
153      do
154         Result := items_storage.new_iterator
155      end
156
157   new_iterator_on_keys: ITERATOR[K_]
158      do
159         Result := keys_storage.new_iterator
160      end
161
162   new_iterator: ITERATOR[TUPLE[V_, K_]]
163      do
164         not_yet_implemented
165      end
166
167feature {ANY} -- Other features:
168   internal_key (k: K_): K_
169      do
170         Result := keys_storage.item(keys_storage.first_index_of(k))
171      end
172
173   copy (other: like Current)
174         -- Reinitialize by copying all associations of `other'.
175      do
176         if keys_storage /= Void then
177            keys_storage.copy(other.keys_storage)
178            items_storage.copy(other.items_storage)
179         else
180            keys_storage := other.keys_storage.twin
181            items_storage := other.items_storage.twin
182         end
183      end
184
185
186feature {}
187   make
188      do
189         create keys_storage.make(0)
190         create items_storage.make(0)
191      end
192
193   with_capacity (needed_capacity: like capacity)
194      require
195         needed_capacity >= 0
196      do
197         create keys_storage.with_capacity(needed_capacity)
198         create items_storage.with_capacity(needed_capacity)
199      ensure
200         capacity >= needed_capacity
201         is_empty
202      end
203
204feature {ARRAY_DICTIONARY}
205   keys_storage: FAST_ARRAY[K_]
206   items_storage: FAST_ARRAY[V_]
207
208invariant
209   keys_storage.count = items_storage.count
210   keys_storage.capacity = items_storage.capacity
211
212end -- class ARRAY_DICTIONARY
213--
214-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
215--
216-- Permission is hereby granted, free of charge, to any person obtaining a copy
217-- of this software and associated documentation files (the "Software"), to deal
218-- in the Software without restriction, including without limitation the rights
219-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
220-- copies of the Software, and to permit persons to whom the Software is
221-- furnished to do so, subject to the following conditions:
222--
223-- The above copyright notice and this permission notice shall be included in
224-- all copies or substantial portions of the Software.
225--
226-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
227-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
228-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
229-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
230-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
231-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
232-- THE SOFTWARE.