PageRenderTime 28ms CodeModel.GetById 22ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/storage/dictionary/zip.e

http://github.com/tybor/Liberty
Specman e | 178 lines | 129 code | 23 blank | 26 comment | 4 complexity | 2b8978279d4a185cfdaad69ac2298c79 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class ZIP[V_, K_]
  5   --
  6   -- Makes a dictionary of two independent hoards.
  7   --
  8
  9inherit
 10   MAP[V_, K_]
 11      redefine
 12         lower, upper, keys, items
 13      end
 14
 15insert
 16   SAFE_EQUAL[K_]
 17      undefine
 18         is_equal, out_in_tagged_out_memory
 19      redefine
 20         copy
 21      end
 22
 23create {ANY}
 24   make
 25
 26feature {ANY}
 27   lower: INTEGER 0
 28
 29   upper: INTEGER
 30      do
 31         Result := count - 1
 32      end
 33
 34   count: INTEGER
 35      do
 36         Result := keys.count
 37      end
 38
 39   copy (other: like Current)
 40      do
 41         make(other.items, other.keys)
 42      end
 43
 44   has (k: K_): BOOLEAN
 45      do
 46         Result := keys.valid_index(index_of(k))
 47      end
 48
 49   at, reference_at (k: K_): V_
 50      local
 51         index: INTEGER
 52      do
 53         index := index_of(k)
 54         Result := items.item(index - keys.lower + items.lower)
 55      end
 56
 57   fast_has (k: K_): BOOLEAN
 58      do
 59         Result := keys.valid_index(fast_index_of(k))
 60      end
 61
 62   fast_at, fast_reference_at (k: K_): V_
 63      local
 64         index: INTEGER
 65      do
 66         index := fast_index_of(k)
 67         Result := items.item(index - keys.lower + items.lower)
 68      end
 69
 70   item (index: INTEGER): V_
 71      do
 72         Result := items.item(index + items.lower)
 73      end
 74
 75   key (index: INTEGER): K_
 76      do
 77         Result := keys.item(index + keys.lower)
 78      end
 79
 80   new_iterator_on_items: ITERATOR[V_]
 81      do
 82         Result := items.new_iterator
 83      end
 84
 85   new_iterator_on_keys: ITERATOR[K_]
 86      do
 87         Result := keys.new_iterator
 88      end
 89
 90   new_iterator: ITERATOR[TUPLE[V_, K_]]
 91      do
 92         create {ITERATOR_ON_ZIP[V_, K_]} Result.make(new_iterator_on_items, new_iterator_on_keys)
 93      end
 94
 95feature {ANY} -- Other features:
 96   internal_key (k: K_): K_
 97      local
 98         index: INTEGER
 99      do
100         index := index_of(k)
101         Result := keys.item(index)
102      end
103
104feature {ANY}
105   make (a_items: like items; a_keys: like keys)
106      require
107         a_items.count = a_keys.count
108      do
109         items := a_items
110         keys := a_keys
111      ensure
112         items = a_items
113         keys = a_keys
114      end
115
116feature {ANY}
117   items: TRAVERSABLE[V_]
118   keys: TRAVERSABLE[K_]
119
120feature {}
121   index_of (k: K_): INTEGER
122      local
123         i: INTEGER
124      do
125         from
126            Result := keys.lower - 1
127            i := keys.lower
128         until
129            keys.valid_index(Result) or else i > keys.upper
130         loop
131            if safe_equal(k, keys.item(i)) then
132               Result := i
133            end
134            i := i + 1
135         end
136      end
137
138   fast_index_of (k: K_): INTEGER
139      local
140         i: INTEGER
141      do
142         from
143            Result := keys.lower - 1
144            i := keys.lower
145         until
146            keys.valid_index(Result) or else i > keys.upper
147         loop
148            if k = keys.item(i) then
149               Result := i
150            end
151            i := i + 1
152         end
153      end
154
155invariant
156   items.count = keys.count
157
158end -- class ZIP
159--
160-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
161--
162-- Permission is hereby granted, free of charge, to any person obtaining a copy
163-- of this software and associated documentation files (the "Software"), to deal
164-- in the Software without restriction, including without limitation the rights
165-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
166-- copies of the Software, and to permit persons to whom the Software is
167-- furnished to do so, subject to the following conditions:
168--
169-- The above copyright notice and this permission notice shall be included in
170-- all copies or substantial portions of the Software.
171--
172-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
175-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
177-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
178-- THE SOFTWARE.