/[eiffelstudio]/trunk/eweasel/tests/incr071/chain.e
ViewVC logotype

Contents of /trunk/eweasel/tests/incr071/chain.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65297 - (show annotations)
Thu Nov 30 20:22:33 2006 UTC (13 years ago) by manus
File size: 7473 byte(s)
Moved from trunk/Src/eweasel to trunk/eweasel so that a simple checkout of the source code is not penalized by the lenghty process of checking out all the tests of eweasel.
1 indexing
2
3 description:
4 "Possibly circular sequences of items, %
5 %without commitment to a particular representation";
6
7 status: "See notice at end of class";
8 names: chain, sequence;
9 access: index, cursor, membership;
10 contents: generic;
11 date: "$Date$";
12 revision: "$Revision$"
13
14 deferred class CHAIN [G] inherit
15
16 CURSOR_STRUCTURE [G]
17 undefine
18 prune_all
19 select
20 put
21 end;
22
23 INDEXABLE [G, INTEGER]
24 rename
25 item as i_th,
26 put as put_i_th
27 undefine
28 prune_all
29 end;
30
31 SEQUENCE [G]
32 rename
33 put as sequence_put
34 export
35 {NONE} sequence_put
36 redefine
37 index_of, has, off, occurrences
38 select
39 index_of, has, occurrences
40 end;
41
42 SEQUENCE [G]
43 rename
44 put as sequence_put,
45 index_of as sequential_index_of,
46 has as sequential_has,
47 occurrences as sequential_occurrences
48 export
49 {NONE}
50 sequential_index_of, sequential_has,
51 sequence_put
52 redefine
53 off
54 end;
55
56 feature -- Access
57
58 first: like item is
59 -- Item at first position
60 require
61 not_empty: not is_empty
62 local
63 pos: CURSOR
64 do
65 pos := cursor;
66 start;
67 Result := item;
68 go_to (pos)
69 end;
70
71 last: like item is
72 -- Item at last position
73 require
74 not_empty: not is_empty
75 local
76 pos: CURSOR
77 do
78 pos := cursor;
79 finish;
80 Result := item;
81 go_to (pos)
82 end;
83
84 has (v: like item): BOOLEAN is
85 -- Does chain include `v'?
86 -- (Reference or object equality,
87 -- based on `object_comparison'.)
88
89 local
90 pos: CURSOR
91 do
92 pos := cursor;
93 Result := sequential_has (v);
94 go_to (pos)
95 end;
96
97 index_of (v: like item; i: INTEGER): INTEGER is
98 -- Index of `i'-th occurrence of item identical to `v'.
99 -- (Reference or object equality,
100 -- based on `object_comparison'.)
101 -- 0 if none.
102 local
103 pos: CURSOR
104 do
105 pos := cursor;
106 Result := sequential_index_of (v, i);
107 go_to (pos)
108 end;
109
110 i_th, infix "@" (i: INTEGER): like item is
111 -- Item at `i'-th position
112 local
113 pos: CURSOR
114 do
115 pos := cursor;
116 go_i_th (i);
117 Result := item;
118 go_to (pos)
119 end;
120
121 index: INTEGER is
122 -- Current cursor index
123 deferred
124 end;
125
126 feature -- Measurement
127
128 occurrences (v: like item): INTEGER is
129 -- Number of times `v' appears.
130 -- (Reference or object equality,
131 -- based on `object_comparison'.)
132 local
133 pos: CURSOR
134 do
135 pos := cursor;
136 Result := sequential_occurrences (v);
137 go_to (pos)
138 end;
139
140 index_set: INTEGER_INTERVAL is
141 -- Range of acceptable indexes
142 do
143 create Result.make (1, count)
144 ensure then
145 Result.count = count
146 end
147
148 feature -- Cursor movement
149
150 start is
151 -- Move cursor to first position.
152 -- (No effect if empty)
153 do
154 if not is_empty then
155 go_i_th (1)
156 end
157 ensure then
158 at_first: not is_empty implies isfirst
159 end;
160
161 finish is
162 -- Move cursor to last position.
163 -- (No effect if empty)
164 do
165 if not is_empty then
166 go_i_th (count)
167 end
168 ensure then
169 at_last: not is_empty implies islast
170 end;
171
172 move (i: INTEGER) is
173 -- Move cursor `i' positions. The cursor
174 -- may end up `off' if the absolute value of `i'
175 -- is too big.
176 local
177 counter, pos, final: INTEGER
178 do
179 if i > 0 then
180 from
181 until
182 (counter = i) or else off
183 loop
184 forth;
185 counter := counter + 1
186 end
187 elseif i < 0 then
188 final := index + i;
189 if final <= 0 then
190 start;
191 back
192 else
193 from
194 start;
195 pos := 1
196 until
197 pos = final
198 loop
199 forth;
200 pos := pos + 1
201 end
202 end
203 end
204 ensure
205 too_far_right: (old index + i > count) implies exhausted;
206 too_far_left: (old index + i < 1) implies exhausted;
207 expected_index: (not exhausted) implies (index = old index + i)
208 end;
209
210 go_i_th (i: INTEGER) is
211 -- Move cursor to `i'-th position.
212 require
213 valid_cursor_index: valid_cursor_index (i)
214 do
215 move (i - index);
216 ensure
217 position_expected: index = i
218 end;
219
220 feature -- Status report
221
222 valid_index (i: INTEGER): BOOLEAN is
223 -- Is `i' within allowable bounds?
224 do
225 Result := (i >= 1) and (i <= count)
226 ensure then
227 valid_index_definition: Result = ((i >= 1) and (i <= count))
228 end;
229
230
231 isfirst: BOOLEAN is
232 -- Is cursor at first position?
233 do
234 Result := not is_empty and (index = 1)
235 ensure
236 valid_position: Result implies not is_empty
237 end;
238
239 islast: BOOLEAN is
240 -- Is cursor at last position?
241 do
242 Result := not is_empty and (index = count)
243 ensure
244 valid_position: Result implies not is_empty
245 end;
246
247 off: BOOLEAN is
248 -- Is there no current item?
249 do
250 Result := (index = 0) or (index = count + 1)
251 end;
252
253
254 valid_cursor_index (i: INTEGER): BOOLEAN is
255 -- Is `i' correctly bounded for cursor movement?
256 do
257 Result := (i >= 0) and (i <= count + 1)
258 ensure
259 valid_cursor_index_definition: Result = ((i >= 0) and (i <= count + 1))
260 end;
261
262 feature -- Element change
263
264 put (v: like item) is
265 -- Replace current item by `v'.
266 -- (Synonym for `replace')
267 do
268 replace (v)
269 ensure then
270 same_count: count = old count;
271 end;
272
273 put_i_th (v: like item; i: INTEGER) is
274 -- Put `v' at `i'-th position.
275 local
276 pos: CURSOR
277 do
278 pos := cursor;
279 go_i_th (i);
280 replace (v);
281 go_to (pos)
282 end;
283
284 feature -- Transformation
285
286 swap (i: INTEGER) is
287 -- Exchange item at `i'-th position with item
288 -- at cursor position.
289 require
290 not_off: not off;
291 valid_index: valid_index (i)
292 local
293 old_item, new_item: like item;
294 pos: CURSOR
295 do
296 pos := cursor;
297 old_item := item;
298 go_i_th (i);
299 new_item := item;
300 replace (old_item);
301 go_to (pos);
302 replace (new_item)
303 ensure
304 swapped_to_item: item = old i_th (i);
305 swapped_from_item: i_th (i) = old item
306 end;
307
308 feature -- Duplication
309
310 duplicate (n: INTEGER): like Current is
311 -- Copy of sub-chain beginning at current position
312 -- and having min (`n', `from_here') items,
313 -- where `from_here' is the number of items
314 -- at or to the right of current position.
315 require
316 not_off_unless_after: off implies after;
317 valid_subchain: n >= 0
318 deferred
319 end;
320
321 feature {NONE} -- Inapplicable
322
323 remove is
324 -- Remove current item.
325 do
326 end;
327
328 invariant
329
330 non_negative_index: index >= 0;
331 index_small_enough: index <= count + 1;
332 off_definition: off = ((index = 0) or (index = count + 1));
333 isfirst_definition: isfirst = ((not is_empty) and (index = 1));
334 islast_definition: islast = ((not is_empty) and (index = count));
335 item_corresponds_to_index: (not off) implies (item = i_th (index))
336 index_set_has_same_count: index_set.count = count
337
338 end -- class CHAIN
339
340
341 --|----------------------------------------------------------------
342 --| EiffelBase: Library of reusable components for Eiffel.
343 --| Copyright (c) 1993-2006 University of Southern California and contributors.
344 --| For ISE customers the original versions are an ISE product
345 --| covered by the ISE Eiffel license and support agreements.
346 --| EiffelBase may now be used by anyone as FREE SOFTWARE to
347 --| develop any product, public-domain or commercial, without
348 --| payment to ISE, under the terms of the ISE Free Eiffel Library
349 --| License (IFELL) at http://eiffel.com/products/base/license.html.
350 --|
351 --| Interactive Software Engineering Inc.
352 --| ISE Building, 2nd floor
353 --| 270 Storke Road, Goleta, CA 93117 USA
354 --| Telephone 805-685-1006, Fax 805-685-6869
355 --| Electronic mail <info@eiffel.com>
356 --| Customer support e-mail <support@eiffel.com>
357 --| For latest info see award-winning pages: http://eiffel.com
358 --|----------------------------------------------------------------
359

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23