/[eiffelstudio]/FreeELKS/trunk/library/kernel/special.e
ViewVC logotype

Annotation of /FreeELKS/trunk/library/kernel/special.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91477 - (hide annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years, 1 month ago) by ericb
File size: 9861 byte(s)
Synchronized with ISE 6.0.65740
1 manus_eiffel 91424 indexing
2     description: "[
3 ericb 91477 Special objects: homogeneous sequences of values,
4 manus_eiffel 91424 used to represent arrays and strings
5     ]"
6 manus_eiffel 91434 library: "Free implementation of ELKS library"
7 ericb 91441 copyright: "Copyright (c) 1986-2005, Eiffel Software and others"
8 manus_eiffel 91434 license: "Eiffel Forum License v2 (see forum.txt)"
9 manus_eiffel 91424 date: "$Date$"
10     revision: "$Revision$"
11    
12     frozen class
13     SPECIAL [T]
14    
15 ericb 91441 inherit
16     ABSTRACT_SPECIAL
17    
18 manus_eiffel 91424 create
19 ericb 91441 make,
20     make_from_native_array
21 manus_eiffel 91424
22     feature {NONE} -- Initialization
23    
24     frozen make (n: INTEGER) is
25     -- Creates a special object for `n' entries.
26     require
27     non_negative_argument: n >= 0
28     external
29     "built_in"
30     ensure
31     area_allocated: count = n
32     end
33 ericb 91475
34 ericb 91441 frozen make_from_native_array (an_array: like native_array) is
35     -- Creates a special object from `an_array'.
36     require
37     is_dotnet: {PLATFORM}.is_dotnet
38     an_array_not_void: an_array /= Void
39     do
40     end
41 manus_eiffel 91424
42     feature -- Access
43    
44 ericb 91441 frozen item alias "[]" (i: INTEGER): T assign put is
45 manus_eiffel 91424 -- Item at `i'-th position
46     -- (indices begin at 0)
47     require
48     index_big_enough: i >= 0
49     index_small_enough: i < count
50     external
51     "built_in"
52     end
53    
54 ericb 91441 frozen infix "@" (i: INTEGER): T is
55     -- Item at `i'-th position
56     -- (indices begin at 0)
57     require
58     index_big_enough: i >= 0
59     index_small_enough: i < count
60     do
61     Result := item (i)
62     end
63    
64 manus_eiffel 91424 frozen index_of (v: T; start_position: INTEGER): INTEGER is
65     -- Index of first occurrence of item identical to `v'.
66     -- -1 if none.
67     require
68     valid_start_position: start_position >= 0
69     local
70     nb: INTEGER
71     do
72     from
73     Result := start_position
74     nb := count
75     until
76     Result >= nb or else equal (item (Result), v)
77     loop
78     Result := Result + 1
79     end
80     if Result >= nb then
81     Result := -1
82     end
83     ensure
84     found_or_not_found: Result = -1 or else (Result >= 0 and then Result < count)
85     end
86 ericb 91475
87 manus_eiffel 91424 frozen item_address (i: INTEGER): POINTER is
88     -- Address of element at position `i'.
89     require
90 ericb 91441 not_dotnet: not {PLATFORM}.is_dotnet
91 manus_eiffel 91424 index_big_enough: i >= 0
92     index_small_enough: i < count
93     do
94     Result := $Current
95     Result := Result + i * element_size
96     ensure
97     element_address_not_null: Result /= default_pointer
98     end
99    
100     frozen base_address: POINTER is
101     -- Address of element at position `0'.
102 ericb 91441 require
103     not_dotnet: not {PLATFORM}.is_dotnet
104 manus_eiffel 91424 do
105     Result := $Current
106     ensure
107     base_address_not_null: Result /= default_pointer
108     end
109 ericb 91475
110 ericb 91441 frozen native_array: NATIVE_ARRAY [T] is
111     -- Only for compatibility with .NET
112     require
113     is_dotnet: {PLATFORM}.is_dotnet
114     do
115     end
116 ericb 91477
117 manus_eiffel 91424 feature -- Measurement
118    
119     lower: INTEGER is 0
120     -- Minimum index of Current
121 ericb 91475
122     frozen upper: INTEGER is
123 manus_eiffel 91424 -- Maximum index of Current
124     do
125 ericb 91441 Result := count - 1
126 manus_eiffel 91424 end
127    
128 ericb 91477 frozen count: INTEGER is
129 manus_eiffel 91424 -- Count of the special area
130 ericb 91441 external
131     "built_in"
132 manus_eiffel 91424 end
133    
134     frozen capacity: INTEGER is
135     -- Count of the special area
136 ericb 91441 do
137     Result := count
138 manus_eiffel 91424 end
139    
140     feature -- Status report
141    
142     frozen all_default (upper_bound: INTEGER): BOOLEAN is
143     -- Are all items between index `0' and `upper_bound'
144     -- set to default values?
145     require
146     min_upper_bound: upper_bound >= -1
147     max_upper_bound: upper_bound < count
148     local
149     i: INTEGER
150     t: T
151     do
152     from
153     Result := True
154     until
155     i > upper_bound or else not Result
156     loop
157     Result := item (i) = t
158     i := i + 1
159     end
160     ensure
161     valid_on_empty_area: upper_bound = -1 implies Result
162     end
163    
164     frozen same_items (other: like Current; upper_bound: INTEGER): BOOLEAN is
165     -- Do all items between index `0' and `upper_bound' have
166     -- same value?
167     require
168     min_upper_bound: upper_bound >= -1
169     max_upper_bound: upper_bound < count
170     other_not_void: other /= Void
171     other_has_enough_items: upper_bound < other.count
172     local
173     i: INTEGER
174     do
175     from
176     Result := True
177     until
178     i > upper_bound or else not Result
179     loop
180     Result := item (i) = other.item (i)
181     i := i + 1
182     end
183     ensure
184     valid_on_empty_area: upper_bound = -1 implies Result
185     end
186 ericb 91475
187 manus_eiffel 91424 frozen valid_index (i: INTEGER): BOOLEAN is
188     -- Is `i' within the bounds of Current?
189     do
190     Result := (0 <= i) and then (i < count)
191     end
192 ericb 91475
193 manus_eiffel 91424 feature -- Element change
194    
195     frozen put (v: T; i: INTEGER) is
196     -- Replace `i'-th item by `v'.
197     -- (Indices begin at 0.)
198     require
199     index_big_enough: i >= 0
200     index_small_enough: i < count
201     external
202     "built_in"
203     end
204 ericb 91475
205 ericb 91441 frozen fill_with (v: T; start_index, end_index: INTEGER) is
206     -- Set items between `start_index' and `end_index' with `v'.
207     require
208     start_index_non_negative: start_index >= 0
209     start_index_not_too_big: start_index <= end_index
210     end_index_valid: end_index < count
211     local
212     i, nb: INTEGER
213     do
214     from
215     i := start_index
216     nb := end_index + 1
217     until
218     i = nb
219     loop
220     put (v, i)
221     i := i + 1
222     end
223     end
224 manus_eiffel 91424
225 ericb 91477 frozen copy_data (other: SPECIAL [T]; source_index, destination_index, n: INTEGER) is
226 ericb 91475 -- Copy `n' elements of `other' from `source_index' position to Current at
227 ericb 91441 -- `destination_index'. Other elements of Current remain unchanged.
228     require
229     other_not_void: other /= Void
230     source_index_non_negative: source_index >= 0
231     destination_index_non_negative: destination_index >= 0
232     n_non_negative: n >= 0
233     n_is_small_enough_for_source: source_index + n <= other.count
234     n_is_small_enough_for_destination: destination_index + n <= count
235 ericb 91477 same_type: same_type (other)
236 ericb 91441 local
237     i, j, nb: INTEGER
238     do
239     if other = Current then
240     move_data (source_index, destination_index, n)
241     else
242     from
243     i := source_index
244     j := destination_index
245     nb := source_index + n
246     until
247     i = nb
248     loop
249     put (other.item (i), j)
250     i := i + 1
251     j := j + 1
252     end
253     end
254 manus_eiffel 91424 end
255    
256 ericb 91441 frozen move_data (source_index, destination_index, n: INTEGER) is
257     -- Move `n' elements of Current from `source_start' position to `destination_index'.
258     -- Other elements remain unchanged.
259     require
260     source_index_non_negative: source_index >= 0
261     destination_index_non_negative: destination_index >= 0
262     n_non_negative: n >= 0
263     n_is_small_enough_for_source: source_index + n <= count
264     n_is_small_enough_for_destination: destination_index + n <= count
265     do
266     if source_index = destination_index then
267     elseif source_index > destination_index then
268     if destination_index + n < source_index then
269     non_overlapping_move (source_index, destination_index, n)
270     else
271     overlapping_move (source_index, destination_index, n)
272     end
273     else
274     if source_index + n < destination_index then
275     non_overlapping_move (source_index, destination_index, n)
276     else
277     overlapping_move (source_index, destination_index, n)
278     end
279     end
280     end
281    
282     frozen overlapping_move (source_index, destination_index, n: INTEGER) is
283     -- Move `n' elements of Current from `source_start' position to `destination_index'.
284     -- Other elements remain unchanged.
285     require
286     source_index_non_negative: source_index >= 0
287     destination_index_non_negative: destination_index >= 0
288     n_non_negative: n >= 0
289     different_source_and_target: source_index /= destination_index
290     n_is_small_enough_for_source: source_index + n <= count
291     n_is_small_enough_for_destination: destination_index + n <= count
292     local
293     i, nb: INTEGER
294     l_offset: INTEGER
295     do
296     if source_index < destination_index then
297     -- We shift from left to right starting from the end
298     -- due to possible overlapping.
299     from
300     i := source_index + n - 1
301     nb := source_index - 1
302     l_offset := destination_index - source_index
303     check
304     l_offset_positive: l_offset > 0
305     end
306     until
307     i = nb
308     loop
309     put (item (i), i + l_offset)
310     i := i - 1
311     end
312     else
313     -- We shift from right to left.
314     from
315     i := source_index
316     nb := source_index + n
317     l_offset := source_index - destination_index
318     check
319     l_offset_positive: l_offset > 0
320     end
321     until
322     i = nb
323     loop
324     put (item (i), i - l_offset)
325     i := i + 1
326     end
327     end
328     end
329    
330     frozen non_overlapping_move (source_index, destination_index, n: INTEGER) is
331     -- Move `n' elements of Current from `source_start' position to `destination_index'.
332     -- Other elements remain unchanged.
333     require
334     source_index_non_negative: source_index >= 0
335     destination_index_non_negative: destination_index >= 0
336     n_non_negative: n >= 0
337     different_source_and_target: source_index /= destination_index
338 ericb 91475 non_overlapping:
339 ericb 91441 (source_index < destination_index implies source_index + n < destination_index) or
340     (source_index > destination_index implies destination_index + n < source_index)
341     n_is_small_enough_for_source: source_index + n <= count
342     n_is_small_enough_for_destination: destination_index + n <= count
343     local
344     i, nb: INTEGER
345     l_offset: INTEGER
346     do
347     from
348     i := source_index
349     nb := source_index + n
350     l_offset := destination_index - source_index
351     until
352     i = nb
353     loop
354     put (item (i), i + l_offset)
355     i := i + 1
356     end
357     end
358 ericb 91475
359 manus_eiffel 91424 feature -- Resizing
360    
361     frozen resized_area (n: INTEGER): like Current is
362     -- Create a copy of Current with a count of `n'.
363     require
364     valid_new_count: n > count
365     do
366     create Result.make (n)
367 ericb 91477 Result.copy_data (Current, 0, 0, count)
368 manus_eiffel 91424 ensure
369     Result_not_void: Result /= Void
370     Result_different_from_current: Result /= Current
371     new_count: Result.count = n
372     end
373 ericb 91475
374 manus_eiffel 91424 frozen aliased_resized_area (n: INTEGER): like Current is
375     -- Try to resize `Current' with a count of `n', if not
376     -- possible a new copy.
377     require
378     valid_new_count: n > count
379 manus_eiffel 91434 external
380     "built_in"
381 manus_eiffel 91424 ensure
382     Result_not_void: Result /= Void
383     new_count: Result.count = n
384     end
385 ericb 91441
386 manus_eiffel 91424 feature -- Removal
387    
388     frozen clear_all is
389     -- Reset all items to default values.
390 ericb 91441 local
391     i: INTEGER
392     v: T
393     do
394     from
395     i := count - 1
396     until
397     i < 0
398     loop
399     put (v, i)
400     i := i - 1
401     end
402 manus_eiffel 91424 end
403    
404     feature {NONE} -- Implementation
405    
406     frozen element_size: INTEGER is
407     -- Size of elements.
408     external
409     "built_in"
410     end
411    
412 manus_eiffel 91438 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23