1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
-- This file is free software, which comes along with SmartEiffel. This
-- software is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-- FITNESS FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
-- this header is kept unaltered, and a notification of the changes is added.
-- You are allowed to redistribute it and sell it, alone or as a part of
-- another product.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
-- - University of Nancy 1 - FRANCE
-- Copyright(C) 2003: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
-- - University of Nancy 2 - FRANCE
--
-- Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
-- Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
class FIXED_ARRAY2[E]
--
-- Resizable two dimensional array.
-- Unlike ARRAY2, the `lower1' bound and the `lower2' bound are
-- frozen to 0. Thus, one can expect better performances.
--
inherit COLLECTION2[E]
creation
make, copy, from_collection2, from_collection, from_model
feature
upper1, count1, upper2, count2, count: INTEGER
feature {FIXED_ARRAY2}
storage: NATIVE_ARRAY[E]
capacity : INTEGER; -- of `storage'.
feature
lower1: INTEGER is 0
lower2: INTEGER is 0
feature
make(new_count1 , new_count2 : INTEGER) is
-- Create or reset Current with new dimensions.
-- All elements are set to the default value of type E.
require
new_count1 > 0
new_count2 > 0
do
count1 := new_count1
upper1 := new_count1 - 1
count2 := new_count2
upper2 := new_count2 - 1
count := count1 * count2
if capacity < count then
capacity := count
storage := storage.calloc(capacity)
else
storage.clear_all(capacity - 1)
end
ensure
count1 = new_count1
count2 = new_count2
all_default
end
from_collection(model: COLLECTION2[like item]) is
-- Uses the `model' to update Current.
local
i, j: INTEGER
do
make(model.upper1+1, model.upper2+1)
from
i := 0
until
i > upper1
loop
from
j := 0
until
j > upper2
loop
put(model.item(i,j),i,j)
j := j + 1
end
i := i + 1
end
end
feature -- Implementation of others feature from COLLECTION2:
item(line, column: INTEGER): E is
do
Result := storage.item(line * count2 + column)
end
put(x: like item; line, column: INTEGER) is
do
storage.put(x,line * count2 + column)
end
force(element: like item; line , column : INTEGER) is
do
if not valid_index(line, column) then
resize(line.max(upper1),column.max(upper2))
end
put(element,line,column)
end
copy(other: like Current) is
do
count1 := other.count1
upper1 := count1 - 1
count2 := other.count2
upper2 := count2 - 1
count := count1 * count2
if capacity < count then
capacity := count
storage := storage.calloc(capacity)
end
storage.copy_from(other.storage, count - 1)
end
is_equal(other: like Current): BOOLEAN is
do
if other = Current then
Result := true
elseif upper1 /= other.upper1 then
elseif upper2 /= other.upper2 then
else
Result := storage.memcmp(other.storage,count)
end
end
feature -- Writing:
set_all_with(x: E) is
-- All element are set with the value x.
do
storage.set_all_with(x,count - 1)
end
all_default: BOOLEAN is
do
result := storage.all_default(count - 1)
end
slice(l1,up1,l2,up2: INTEGER): like Current is
-- Create a new collection initialized with elements of
-- range `low'..`up'. Result has the same dynamic type
-- as Current collection.
local
ligne,colonne : INTEGER
do
from
!!Result.make((up1 - l1) + 1,(up2 - l2) + 1)
ligne := l1
until
ligne > up1
loop
from
colonne := l2
until
colonne > up2
loop
Result.put(item(ligne,colonne),ligne - l1,colonne - l2)
colonne := colonne + 1
end
ligne := ligne + 1
end
end; -- slice
set_slice(element: like item; l1,up1,l2,up2: INTEGER) is
-- Set all the elements in the
-- range [(l1,up1),(l2,up2)] of
-- Current with the element 'element'.
local
i,j : INTEGER
do
from
i := l1 * count2
until
i > up1 * count2
loop
from
j := l2
until
j > up2
loop
storage.put(element,i + j)
j := j + 1
end
i := i + count2
end
end
swap(line1, column1, line2, column2 : INTEGER) is
local
tmp: like item
c2, index1, index2 : INTEGER
do
c2 := count2
index1 := line1 * c2 + column1
index2 := line2 * c2 + column2
tmp := storage.item( index1)
storage.put(storage.item(index2) ,index1)
storage.put(tmp,index2)
end
feature -- Looking and comparison:
occurrences(elt: E): INTEGER is
do
Result := storage.occurrences(elt,count - 1)
end
fast_occurrences(elt: E): INTEGER is
do
Result := storage.fast_occurrences(elt,count - 1)
end
feature -- Resizing:
resize(new_count1 , new_count2: INTEGER) is
require
new_count1 > 0
new_count2 > 0
local
tmp: like Current
l, c: INTEGER
do
!!tmp.make(new_count1, new_count2)
-- It may be possible to avoid this ceation when :
-- new `capacity' <= old `capacity'
from
l := line_maximum
until
l < 0
loop
from
c := column_maximum
until
c < 0
loop
if tmp.valid_index(l,c) then
tmp.put(item(l,c),l,c)
end
c := c - 1
end
l := l - 1
end
standard_copy(tmp)
ensure
upper1 = new_count1 - 1
count1 = new_count1
upper2 = new_count2 - 1
count2 = new_count2
count = new_count1 * new_count2
end
feature -- Looking and Searching:
has(x: like item): BOOLEAN is
-- Look for `x' using `equal' for comparison.
do
Result := storage.index_of(x,count-1) <= (count-1)
end; -- has
fast_has(x: like item): BOOLEAN is
-- Same as `has' but use `=' for comparison
do
Result := storage.fast_index_of(x,count - 1) <= (count - 1)
end; -- fast_has
feature -- Other features:
replace_all(old_value, new_value: like item) is
do
storage.replace_all(old_value,new_value,count - 1)
end
fast_replace_all(old_value, new_value: like item) is
do
storage.fast_replace_all(old_value,new_value,count - 1)
end
transpose is
-- Transpose the Current array
local
i,j : INTEGER
oldu1 , oldu2 : INTEGER
do
oldu1 := upper1
oldu2 := upper2
resize(upper2.max(upper1),upper2.max(upper1))
from
i := 0
until
i > upper1 - 1
loop
from
j := i + 1
until
j > upper2
loop
swap(i,j,j,i)
j := j + 1
end
i := i + 1
end
resize(oldu2,oldu1)
end
to_external: POINTER is
-- Gives C access to the internal `storage' (may be dangerous).
do
Result := storage.to_external
end
feature {COLLECTION2}
same_as(other: COLLECTION2[E]): BOOLEAN is
do
Result := other.same_as_fixed_array2(Current)
end
same_as_array2(other: ARRAY2[E]): BOOLEAN is
do
Result := standard_same_as(other)
end
same_as_fixed_array2(other: FIXED_ARRAY2[E]): BOOLEAN is
do
Result := standard_same_as(other)
end
invariant
count1 = upper1 + 1
count2 = upper2 + 1
count = count1 * count2
capacity >= count
end -- FIXED_ARRAY2[E]
|