File: array2.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (419 lines) | stat: -rw-r--r-- 11,366 bytes parent folder | download | duplicates (2)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
-- 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 ARRAY2[E]
   --
   -- General prurpose, resizable, two dimensional array.
   --

inherit COLLECTION2[E]

creation
   make, copy, from_collection2, from_collection, from_model

feature

   lower1, lower2, upper1, upper2: INTEGER

feature {ARRAY2}

   storage: NATIVE_ARRAY[E]
         -- To store elements line by line.

   capacity: INTEGER
         -- Number of elements in `storage'.

feature -- Creation / modification:

   make(line_min, line_max, column_min, column_max: INTEGER) is
         -- Reset all bounds `line_minimum' / `line_maximum' / `column_minimum' and
         -- `column_maximum' using arguments as new values.
         -- All elements are set to the default value of type E.
      require
         line_min <= line_max
         column_min <= column_max
      do
         lower1 := line_min
         upper1 := line_max
         lower2 := column_min
         upper2 := column_max
         if capacity >= count then
            storage.clear_all(count - 1)
         else
            capacity := count
            storage := storage.calloc(count)
         end
      ensure
         line_minimum = line_min
         line_maximum = line_max
         column_minimum = column_min
         column_maximum = column_max
      end

   from_collection2(model: COLLECTION2[like item]) is
      local
         i, j: INTEGER
      do
         make(model.line_minimum,
              model.line_maximum,
              model.column_minimum,
              model.column_maximum)
         from
            i := line_minimum
         until
            i > line_maximum
         loop
            from
               j := column_minimum
            until
               j > column_maximum
            loop
               put(model.item(i,j),i,j)
               j := j + 1
            end
            i := i + 1
         end
      ensure then
         lower1 = model.lower1
         lower2 = model.lower2
      end

   from_collection(contents: COLLECTION[E]
                   line_min, line_max, column_min, column_max: INTEGER) is
         --  Reset all bounds using `line_min', `line_max', `column_min',
         --  and `column_max' .
         --  Copy all elements of `contents', line by line into Current.
      require
         line_min <= line_max
         column_min <= column_max
         contents.count = (line_max - line_min + 1) * (column_max - column_min +1)
      local
         i: INTEGER
      do
         make(line_min,line_max,column_min,column_max)
         from
            i := 0
         until
            i >= count
         loop
            storage.put(contents.item(contents.lower + i),i)
            i := i + 1
         end
      ensure
         line_minimum = line_min
         line_maximum = line_max
         column_minimum = column_min
         column_maximum = column_max
         count = contents.count
      end

   from_model(model: COLLECTION[COLLECTION[E]]) is
         -- The `model' is used to fill line by line the COLLECTION2.
         -- Assume all sub-collections of `model' have the same indexing.
      local
         line, column: INTEGER
      do
         make(model.lower,
              model.upper,
              model.first.lower,
              model.first.upper)
         from
            line := model.lower
         until
            line > model.upper
         loop
            from
               column := model.first.lower
            until
               column > model.first.upper
            loop
               put(model.item(line).item(column),line,column)
               column := column + 1
            end
            line := line + 1
         end
      ensure then
         line_minimum = model.lower
         line_maximum = model.upper
         column_minimum = model.first.lower
         column_maximum = model.first.upper
      end

feature -- Resizing:

   resize(line_min, line_max, column_min, column_max: INTEGER) is
         -- Resize bounds of the Current array
      require
         line_max >= line_min
         column_max >= column_min
      local
         tmp: like Current
         l, c: INTEGER
      do
         !!tmp.make(line_min, line_max, column_min, column_max)
         -- It may be possible to avoid this ceation when :
         --    new `capacity' <= old `capacity'
         from
            l := lower1
         until
            l > line_maximum
         loop
            from
               c := lower2
            until
               c > column_maximum
            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
         line_minimum = line_min
         line_maximum = line_max
         column_minimum = column_min
         column_maximum = column_max
      end

feature -- Implementation of others feature from COLLECTION2:

   item(line, column: INTEGER): E is
      do
         Result := storage.item((line - lower1) * count2 + column - lower2)
      end

   put(element: like item; line, column: INTEGER) is
      do
         storage.put(element,(line - lower1) * count2 + column - lower2)
      end

   count1: INTEGER is
      do
         Result := upper1 - lower1 + 1
      end

   count2: INTEGER is
      do
         Result := upper2 - lower2 + 1
      end

   count: INTEGER is
      do
         Result := count1 * count2
      end

   force(x: like item; line, column: INTEGER) is
      require else
         true
      do
         if not valid_index(line,column) then
            resize(line.min(lower1),
                   line.max(upper1),
                   column.min(lower2),
                   column.max(upper2))
         end
         put(x,line,column)
      end

   set_all_with(element: E) is
      do
         storage.set_all_with(element,count - 1)
      end

   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

   sub_collection2(line_min, line_max, column_min, column_max: INTEGER): like Current is
      local
         i, j, k: INTEGER
      do
         !!Result.make(line_min,line_max,column_min,column_max)
         from
            i := line_min
            k := 0
         until
            i > line_max
         loop
            from
               j := column_min
            until
               j > column_max
            loop
               Result.storage.put(item(i,j),k)
               j := j + 1
               k := k + 1
            end
            i := i + 1
         end
      ensure then
         Result.lower1 = line_min
         Result.lower2 = column_min
         Result.upper1 = line_max
         Result.upper2 = column_max
      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

   has(x: like item): BOOLEAN is
         -- Search if a element x is in the array using `equal'.
         -- See also `fast_has' to chose the apropriate one.
      do
         Result := storage.index_of(x,count - 1) <= count - 1
      end

   fast_has(x: like item): BOOLEAN is
         --  Search if a element x is in the array using `='.
      do
         Result := storage.fast_index_of(x,count - 1) <= count -1
      end

   all_default: BOOLEAN is
      do
         result := storage.all_default(count - 1)
      end

   swap(line1, column1, line2, column2 : INTEGER) is
      local
         tmp: like item
         c2, index1, index2: INTEGER
      do
         c2 := count2
         index1 := (line1 - lower1) * c2 + column1 - lower2
         index2 := (line2 - lower1) * c2 + column2 - lower2
         tmp := storage.item(index1)
         storage.put(storage.item(index2),index1)
         storage.put(tmp,index2)
      end

   copy(other: like Current) is
      do
         lower1 := other.lower1
         upper1 := other.upper1
         lower2 := other.lower2
         upper2 := other.upper2
         if capacity < count then
            capacity := count
            storage := storage.calloc(count)
         end
         storage.copy_from(other.storage, count - 1)
      end

   is_equal(other: like Current): BOOLEAN is
      do
         if other = Current then
            Result := true
         elseif lower1 /= other.lower1 then
         elseif lower2 /= other.lower2 then
         elseif upper1 /= other.upper1 then
         elseif upper2 /= other.upper2 then
         else
            Result := storage.memcmp(other.storage,count)
         end
      end

feature -- Only for ARRAY2:

   transpose is
         -- Transpose the Current array
      local
         i,j : INTEGER
         oldu1,oldu2 : INTEGER
         oldl1,oldl2 : INTEGER
      do
         oldu1 := line_maximum
         oldu2 := column_maximum
         oldl1 := lower1
         oldl2 := lower2
         resize(lower1.min(lower2),
                line_maximum.max(column_maximum),
                lower1.min(lower2),
                line_maximum.max(column_maximum))
         from
            i := lower1
         until
            i > line_maximum
         loop
            from
               j := i + 1
            until
               j > column_maximum
            loop
               swap(i,j,j,i)
               j := j + 1
            end
            i := i + 1
         end
         resize(oldl2,oldu2,oldl1,oldu1)
      ensure
         line_minimum = old column_minimum
         column_minimum = old line_minimum
         line_maximum = old column_maximum
         column_maximum = old line_maximum
         count = old count
      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_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

   count2 = upper2 - lower2 + 1

   capacity >= count

end -- ARRAY2[E]