PageRenderTime 13ms CodeModel.GetById 5ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/storage/internal/arrayed_collection.e

http://github.com/tybor/Liberty
Specman e | 152 lines | 97 code | 18 blank | 37 comment | 1 complexity | b8bd0bad7bec9931bcd05edff363f5c3 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4deferred class ARRAYED_COLLECTION[E_]
  5   --
  6   -- Common root for ARRAY, FAST_ARRAY and RING_ARRAY.
  7   --
  8
  9inherit
 10   COLLECTION[E_]
 11
 12feature {ARRAYED_COLLECTION, ARRAYED_COLLECTION_HANDLER}
 13   storage: NATIVE_ARRAY[E_]
 14         -- Internal access to storage location.
 15
 16   from_external (a_storage: POINTER; a_capacity: like capacity)
 17      require
 18         a_capacity > 0 implies a_storage.is_not_null
 19      do
 20         storage := storage.from_pointer(a_storage)
 21         capacity := a_capacity
 22      ensure
 23         storage.to_external = a_storage
 24         capacity = a_capacity
 25      end
 26
 27feature {ANY}
 28   capacity: INTEGER
 29         -- Internal storage capacity in number of item.
 30
 31   upper: INTEGER
 32         -- Upper index bound.
 33
 34   subarray (min, max: INTEGER): like Current
 35         -- New collection consisting of items at indexes in [`min' .. `max'].
 36         -- Result has the same dynamic type as `Current'.
 37         -- See also `slice'.
 38      require
 39         lower <= min
 40         max <= upper
 41         min <= max + 1
 42      deferred
 43      ensure
 44         same_dynamic_type(Result)
 45         Result.count = max - min + 1
 46         Result.lower = min or Result.lower = 0
 47      end
 48
 49feature {ANY} -- Implementation of deferred:
 50   first: like item
 51      do
 52         Result := storage.item(0)
 53      end
 54
 55   last: like item
 56      do
 57         Result := item(upper)
 58      end
 59
 60   add (element: like item; index: INTEGER)
 61      do
 62         if index = upper + 1 then
 63            add_last(element)
 64         else
 65            add_last(element)
 66            move(index, upper - 1, 1)
 67            put(element, index)
 68         end
 69      end
 70
 71   remove_last
 72      do
 73         upper := upper - 1
 74      end
 75
 76   remove_tail (n: INTEGER)
 77      do
 78         upper := upper - n
 79      end
 80
 81   replace_all (old_value, new_value: like item)
 82      do
 83         storage.replace_all(old_value, new_value, count - 1)
 84      end
 85
 86   fast_replace_all (old_value, new_value: like item)
 87      do
 88         storage.fast_replace_all(old_value, new_value, count - 1)
 89      end
 90
 91   reverse
 92      local
 93         i, j: INTEGER
 94      do
 95         from
 96            i := lower
 97            j := upper
 98         until
 99            i >= j
100         loop
101            swap(i, j)
102            i := i + 1
103            j := j - 1
104         end
105      end
106
107feature {ANY} -- Interfacing with C:
108   to_external: POINTER
109         -- Gives C access into the internal `storage' of the ARRAY.
110         -- Result is pointing the element at index `lower'.
111         --
112         -- NOTE: do not free/realloc the Result. Resizing of the array
113         --       can makes this pointer invalid.
114      require
115         not is_empty
116      do
117         Result := storage.to_pointer
118      ensure
119         Result.is_not_null
120      end
121
122feature {ARRAYED_COLLECTION}
123   set_upper (new_upper: like upper)
124      do
125         upper := new_upper
126      end
127
128invariant
129   capacity >= upper - lower + 1
130   capacity > 0 implies storage.is_not_null
131
132end -- class ARRAYED_COLLECTION
133--
134-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
135--
136-- Permission is hereby granted, free of charge, to any person obtaining a copy
137-- of this software and associated documentation files (the "Software"), to deal
138-- in the Software without restriction, including without limitation the rights
139-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
140-- copies of the Software, and to permit persons to whom the Software is
141-- furnished to do so, subject to the following conditions:
142--
143-- The above copyright notice and this permission notice shall be included in
144-- all copies or substantial portions of the Software.
145--
146-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
147-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
148-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
149-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
150-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
151-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
152-- THE SOFTWARE.