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

Contents of /FreeELKS/trunk/library/kernel/integer_interval.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91424 - (show annotations)
Tue Oct 26 18:39:32 2004 UTC (15 years, 2 months ago) by manus_eiffel
File size: 10413 byte(s)
Initial revision

1 indexing
2
3 description: "Contiguous integer intervals"
4 status: "See notice at end of class"
5 date: "$Date$"
6 revision: "$Revision$"
7
8 class
9 INTEGER_INTERVAL
10
11 inherit
12 RESIZABLE [INTEGER]
13 undefine
14 changeable_comparison_criterion
15 redefine
16 copy, is_equal
17 end
18
19 INDEXABLE [INTEGER, INTEGER]
20
21 rename
22 put as indexable_put
23 undefine
24 changeable_comparison_criterion
25 redefine
26 copy, is_equal
27 select
28 bag_put
29 end
30
31 SET [INTEGER]
32 redefine
33 copy, is_equal
34 end
35
36 create
37 make
38
39 feature {NONE} -- Initialization
40
41 make (min_index, max_index: INTEGER) is
42 -- Set up interval to have bounds `min_index' and
43 -- `max_index' (empty if `min_index' > `max_index')
44 do
45 lower_defined := True
46 upper_defined := True
47 if min_index <= max_index then
48 lower_internal := min_index
49 upper_internal := max_index
50 else
51 lower_internal := 1
52 upper_internal := 0
53 end
54 ensure
55 lower_defined: lower_defined
56 upper_defined: upper_defined
57 set_if_non_empty:
58 (min_index <= max_index) implies
59 ((lower = min_index) and
60 (upper = max_index))
61 empty_if_not_in_order:
62 (min_index > max_index) implies is_empty
63 end
64
65 feature -- Initialization
66
67 adapt (other: INTEGER_INTERVAL) is
68 -- Reset to be the same interval as `other'.
69 require
70 other_not_void: other /= Void
71 do
72 lower_internal := other.lower_internal
73 upper_internal := other.upper_internal
74 lower_defined := other.lower_defined
75 upper_defined := other.upper_defined
76 ensure
77 same_lower: lower = other.lower
78 same_upper: upper = other.upper
79 same_lower_defined: lower_defined = other.lower_defined
80 same_upper_defined: upper_defined = other.upper_defined
81 end
82
83 feature -- Access
84
85 lower_defined: BOOLEAN
86 -- Is there a lower bound?
87
88 lower: INTEGER is
89 -- Smallest value in interval
90 require
91 lower_defined: lower_defined
92 do
93 Result := lower_internal
94 end
95
96 upper_defined: BOOLEAN
97 -- Is there an upper bound?
98
99 upper: INTEGER is
100 -- Largest value in interval
101 require
102 upper_defined: upper_defined
103 do
104 Result := upper_internal
105 end
106
107 item, infix "@" (i: INTEGER): INTEGER is
108 -- Entry at index `i', if in index interval
109 do
110 Result := i
111 end
112
113 has, valid_index (v: INTEGER): BOOLEAN is
114 -- Does `v' appear in interval?
115 do
116 Result :=
117 (upper_defined implies v <= upper) and
118 (lower_defined implies v >= lower)
119 ensure then
120 iff_within_bounds: Result =
121 ((upper_defined implies v <= upper) and
122 (lower_defined implies v >= lower))
123 end
124
125 feature -- Measurement
126
127 occurrences (v: INTEGER): INTEGER is
128 -- Number of times `v' appears in structure
129 do
130 if has (v) then
131 Result := 1
132 end
133 ensure then
134 one_iff_in_bounds: Result = 1 implies has (v)
135 zero_otherwise: Result /= 1 implies Result = 0
136 end
137
138 capacity: INTEGER is
139 -- Maximum number of items in interval
140 -- (here the same thing as `count')
141 do
142 check
143 terminal: upper_defined and lower_defined
144 end
145 Result := count
146 end
147
148 count: INTEGER is
149 -- Number of items in interval
150 do
151 check
152 finite: upper_defined and lower_defined
153 end
154 if upper_defined and lower_defined then
155 Result := upper - lower + 1
156 end
157 ensure then
158 definition: Result = upper - lower + 1
159 end
160
161 index_set: INTEGER_INTERVAL is
162 -- Range of acceptable indexes
163 -- (here: the interval itself)
164 do
165 Result := Current
166 ensure then
167 index_set_is_range: equal (Result, Current)
168 end
169
170 feature -- Comparison
171
172 is_equal (other: like Current): BOOLEAN is
173 -- Is array made of the same items as `other'?
174 do
175 Result :=
176 (lower_defined implies (other.lower_defined and lower = other.lower)) and
177 (upper_defined implies (other.upper_defined and upper = other.upper))
178 ensure then
179 iff_same_bounds: Result =
180 ((lower_defined implies (other.lower_defined and lower = other.lower)) and
181 (upper_defined implies (other.upper_defined and upper = other.upper)))
182 end
183
184 feature -- Status report
185
186 all_cleared: BOOLEAN is
187 -- Are all items set to default values?
188 do
189 Result := ((lower = 0) and (upper = 0))
190 ensure then
191 iff_at_zero: Result = ((lower = 0) and (upper = 0))
192 end
193
194 extendible: BOOLEAN is
195 -- May new items be added?
196 -- Answer: yes
197 do
198 Result := True
199 end
200
201 prunable: BOOLEAN is
202 -- May individual items be removed?
203 -- Answer: no
204 do
205 Result := False
206 end
207
208 feature -- Element change
209
210 extend, put (v: INTEGER) is
211 -- Make sure that interval goes all the way
212 -- to `v' (up or down).
213 do
214 if v < lower then
215 lower_internal := v
216 elseif v > upper then
217 upper_internal := v
218 end
219 ensure then
220 extended_down: lower = (old lower).min (v)
221 extended_up: upper = (old upper).max (v)
222 end
223
224 feature -- Resizing
225
226 resize (min_index, max_index: INTEGER) is
227 -- Rearrange interval to go from at most
228 -- `min_index' to at least `max_index',
229 -- encompassing previous bounds.
230 do
231 lower_internal := min_index.min (lower)
232 upper_internal := max_index.max (upper)
233 end
234
235 resize_exactly (min_index, max_index: INTEGER) is
236 -- Rearrange interval to go from
237 -- `min_index' to `max_index'.
238 do
239 lower_internal := min_index
240 upper_internal := max_index
241 end
242
243 grow (i: INTEGER) is
244 -- Ensure that capacity is at least `i'.
245 do
246 if capacity < i then
247 resize (lower, lower + i - 1)
248 end
249 ensure then
250 no_loss_from_bottom: lower <= old lower
251 no_loss_from_top: upper >= old upper
252 end
253
254 feature -- Removal
255
256 wipe_out is
257 -- Remove all items.
258 do
259 lower_defined := True
260 upper_defined := True
261 lower_internal := 1
262 upper_internal := 0
263 end
264
265 feature -- Conversion
266
267 as_array: ARRAY [INTEGER] is
268 -- Plain array containing interval's items
269 require
270 finite: upper_defined and lower_defined
271 local
272 i: INTEGER
273 do
274 create Result.make (lower, upper)
275 from
276 i := lower
277 until
278 i > upper
279 loop
280 Result.put (i, i)
281 i := i + 1
282 end
283 ensure
284 same_lower: Result.lower = lower
285 same_upper: Result.upper = upper
286 end
287
288 to_c: ANY is
289 -- Address of actual sequence of values,
290 -- for passing to external (non-Eiffel) routines.
291 obsolete
292 "No replacement"
293 do
294 check
295 False
296 end
297 end
298
299 linear_representation: LINEAR [INTEGER] is
300 -- Representation as a linear structure
301 do
302 check
303 terminal: upper_defined and lower_defined
304 end
305 Result := as_array.linear_representation
306 end
307
308 feature -- Duplication
309
310 copy (other: like Current) is
311 -- Reset to be the same interval as `other'.
312 do
313 lower_internal := other.lower_internal
314 upper_internal := other.upper_internal
315 lower_defined := other.lower_defined
316 upper_defined := other.upper_defined
317 ensure then
318 same_lower: lower = other.lower
319 same_upper: upper = other.upper
320 same_lower_defined: lower_defined = other.lower_defined
321 same_upper_defined: upper_defined = other.upper_defined
322 end
323
324 subinterval (start_pos, end_pos: INTEGER): like Current is
325 -- Interval made of items of current array within
326 -- bounds `start_pos' and `end_pos'.
327 do
328 create Result.make (start_pos, end_pos)
329 end
330
331 feature -- Iteration
332
333 for_all (condition:
334 FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]):
335 BOOLEAN is
336 -- Do all interval's values satisfy `condition'?
337 require
338 finite: upper_defined and lower_defined
339 local
340 i: INTEGER
341 do
342 from
343 Result := True; i := lower
344 until
345 (i > upper) or else (not condition.item ([i]))
346 loop
347 i := i + 1
348 end
349 Result := (i > upper)
350 ensure
351 consistent_with_count:
352 Result = (hold_count (condition) = count)
353 end
354
355 exists (condition:
356 FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]):
357 BOOLEAN is
358 -- Does at least one of interval's values
359 -- satisfy `condition'?
360 require
361 finite: upper_defined and lower_defined
362 local
363 i: INTEGER
364 do
365 from
366 i := lower
367 until
368 i > upper or else condition.item ([i])
369 loop
370 i := i + 1
371 end
372 Result := (i <= upper)
373 ensure
374 consistent_with_count:
375 Result = (hold_count (condition) > 0)
376 end
377
378 exists1 (condition:
379 FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]):
380 BOOLEAN is
381 -- Does exactly one of interval's values
382 -- satisfy `condition'?
383 require
384 finite: upper_defined and lower_defined
385 do
386 Result := (hold_count (condition) = 1)
387 ensure
388 consistent_with_count:
389 Result = (hold_count (condition) = 1)
390 end
391
392 hold_count (condition:
393 FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]):
394 INTEGER is
395 -- Number of interval's values that
396 -- satisfy `condition'
397 require
398 finite: upper_defined and lower_defined
399 local
400 i: INTEGER
401 do
402 from
403 i := lower
404 until
405 i > upper
406 loop
407 if condition.item ([i]) then
408 Result := Result + 1
409 end
410 i := i + 1
411 end
412 ensure
413 non_negative: Result >= 0
414 end
415
416 feature {INTEGER_INTERVAL} -- Implementation
417
418 upper_internal: INTEGER
419 -- See `upper`.
420 --| `upper' has a precondition so it can't be an attribute.
421
422 lower_internal: INTEGER
423 -- See `lower`.
424 --| `lower' has a precondition so it can't be an attribute.
425
426 feature {NONE} -- Inapplicable
427
428 prune (v: INTEGER) is
429 -- Remove one occurrence of `v' if any.
430 -- Not applicable here.
431 do
432 end
433
434 indexable_put (v: INTEGER; k: INTEGER) is
435 -- Associate value `v' with key `k'.
436 -- Not applicable here.
437 do
438 end
439
440 invariant
441
442 count_definition: upper_defined and lower_defined implies count = upper - lower + 1
443
444 index_set_is_range: equal (index_set, Current)
445
446 not_infinite: upper_defined and lower_defined
447
448
449 indexing
450
451 library: "[
452 EiffelBase: Library of reusable components for Eiffel.
453 ]"
454
455 status: "[
456 Copyright 1986-2001 Interactive Software Engineering (ISE).
457 For ISE customers the original versions are an ISE product
458 covered by the ISE Eiffel license and support agreements.
459 ]"
460
461 license: "[
462 EiffelBase may now be used by anyone as FREE SOFTWARE to
463 develop any product, public-domain or commercial, without
464 payment to ISE, under the terms of the ISE Free Eiffel Library
465 License (IFELL) at http://eiffel.com/products/base/license.html.
466 ]"
467
468 source: "[
469 Interactive Software Engineering Inc.
470 ISE Building
471 360 Storke Road, Goleta, CA 93117 USA
472 Telephone 805-685-1006, Fax 805-685-6869
473 Electronic mail <info@eiffel.com>
474 Customer support http://support.eiffel.com
475 ]"
476
477 info: "[
478 For latest info see award-winning pages: http://eiffel.com
479 ]"
480
481 end -- class INTEGER_INTERVAL
482
483
484

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23