author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 17:01:19 +0200 | |
changeset 3919 | 57fbcf872e8d |
parent 3861 | 694e93011ca8 |
child 4145 | dac736be1523 |
permissions | -rw-r--r-- |
1924 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1753
f9be6a841a22
ReindexedCollectionCVS: ----------------------------------------------------------------------
Claus Gittinger <cg@exept.de>
parents:
1618
diff
changeset
|
12 |
"{ Package: 'stx:libbasic2' }" |
1190 | 13 |
|
3687 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1190 | 16 |
SequenceableCollection subclass:#ReindexedCollection |
17 |
instanceVariableNames:'sequence interval' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Sequenceable' |
|
21 |
! |
|
22 |
||
23 |
!ReindexedCollection class methodsFor:'documentation'! |
|
24 |
||
1924 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
1190 | 39 |
documentation |
40 |
" |
|
41 |
ReindexedCollection is a wrapper around a sequenceable collection that remaps the indices |
|
42 |
with in linear algorithm. |
|
43 |
The elements in the ReindexedCollection are elements in the sequenceable collection at |
|
44 |
some start to some stop at some step. |
|
1453 | 45 |
|
46 |
ReindexedCollection allows for efficient use of first/rest-like algorithms (i.e. aka Lisp) |
|
47 |
applied to Sequenceable collections, as they avoid element-copying. |
|
48 |
||
1190 | 49 |
See class side examples. |
50 |
||
51 |
[Instance Variables:] |
|
52 |
sequence <SequenceableCollection> the sequence that will be reindexed. |
|
53 |
interval <Interval> the object that describes indicies of interest in the sequence. |
|
54 |
||
1191 | 55 |
[Origin:] |
56 |
Part of the Engineering Math Goodies package from Travis. |
|
57 |
||
58 |
[Author:] |
|
59 |
Travis Griggs (tgriggs@keyww.com or tkc@bmi.net) |
|
60 |
Ported from Squeak by Claus Gittinger (not much of a port, though) |
|
1190 | 61 |
" |
62 |
! |
|
63 |
||
64 |
examples |
|
65 |
" |
|
66 |
|coll| |
|
67 |
||
1618 | 68 |
coll := #(1 2 3 4 5 6 7 8 9 10) from:8. |
69 |
Transcript show:'from 8: '; showCR:coll. |
|
70 |
Transcript show:'size: '; showCR:(coll size). |
|
71 |
Transcript show:'at 1: '; showCR:(coll at:1). |
|
72 |
Transcript show:'first: '; showCR:(coll first). |
|
73 |
Transcript show:'last: '; showCR:(coll last). |
|
74 |
coll do:[:each | Transcript show:'do: '; showCR:each]. |
|
75 |
coll reverseDo:[:each | Transcript show:'reverseDo: '; showCR:each]. |
|
1190 | 76 |
|
77 |
||
78 |
|coll| |
|
79 |
||
80 |
coll := (1 to:10) asOrderedCollection from:3 to:8. |
|
81 |
coll. |
|
82 |
coll size. |
|
83 |
coll at:1. |
|
84 |
coll do:[:each | Transcript showCR:each]. |
|
85 |
||
86 |
||
87 |
|coll| |
|
88 |
||
89 |
coll := (1 to:10) asOrderedCollection to:4. |
|
90 |
coll. |
|
91 |
coll size. |
|
92 |
coll at:1. |
|
93 |
coll last. |
|
94 |
coll do:[:each | Transcript showCR:each]. |
|
95 |
" |
|
96 |
! ! |
|
97 |
||
98 |
!ReindexedCollection class methodsFor:'instance creation'! |
|
99 |
||
100 |
on: aSequence from: start |
|
101 |
"Create a reindexedCollection on aSequence from start to the end of aSequence" |
|
102 |
||
103 |
^ self |
|
104 |
on: aSequence |
|
105 |
from: start |
|
106 |
to: aSequence size |
|
107 |
by: 1 |
|
108 |
! |
|
109 |
||
110 |
on: aSequence from: start by: step |
|
111 |
"Create a reindexedCollection on aSequence start to the end of aSequence |
|
112 |
if step is positive, else |
|
113 |
from start to the beginning of the sequence if step is negative." |
|
114 |
||
115 |
^ self |
|
116 |
on: aSequence |
|
117 |
from: start |
|
118 |
to: (step > 0 |
|
119 |
ifTrue: [aSequence size] |
|
120 |
ifFalse: [1]) |
|
121 |
by: step |
|
122 |
! |
|
123 |
||
124 |
on: aSequence from: start to: stop |
|
125 |
"Create a reindexedCollection on aSequence from start to stop by 1 |
|
126 |
(or -1 if start is greater than stop)" |
|
127 |
||
128 |
^ self |
|
129 |
on: aSequence |
|
130 |
from: start |
|
131 |
to: stop |
|
132 |
by: (start <= stop |
|
133 |
ifTrue: [1] |
|
134 |
ifFalse: [-1]) |
|
135 |
! |
|
136 |
||
137 |
on: aSequence from: start to: stop by: step |
|
138 |
"Create a reindexedCollection on aSequence from start to stop by step" |
|
139 |
||
140 |
^ self new |
|
141 |
initialize: aSequence |
|
142 |
from: start |
|
143 |
to: stop |
|
144 |
by: step |
|
145 |
! |
|
146 |
||
147 |
on: aSequence to: stop |
|
148 |
"Create a reindexedCollection on aSequence from 1 to stop by 1" |
|
149 |
||
150 |
^ self |
|
151 |
on: aSequence |
|
152 |
from: 1 |
|
153 |
to: stop |
|
154 |
by: 1 |
|
155 |
! |
|
156 |
||
157 |
on: aSequence to: stop by: step |
|
158 |
"Create a reindexedCollection on aSequence from 1 to stop (if step is |
|
159 |
positive) or the end to stop (if |
|
160 |
step is negative). Note: if step is not 1 or -1, there is a chance that the |
|
161 |
index specified by stop may |
|
162 |
not be in the interval." |
|
163 |
||
164 |
^ self |
|
165 |
on: aSequence |
|
166 |
from: (step > 0 |
|
167 |
ifTrue: [1] |
|
168 |
ifFalse: [aSequence size]) |
|
169 |
to: stop |
|
170 |
by: step |
|
171 |
! |
|
172 |
||
173 |
on: aSequence with: anInterval |
|
174 |
"Create a reindexedCollection on aSequence" |
|
175 |
||
176 |
^ self new initialize: aSequence with: anInterval |
|
177 |
! ! |
|
178 |
||
179 |
!ReindexedCollection methodsFor:'accessing'! |
|
180 |
||
181 |
at: index |
|
182 |
"Answer the value of an indexable field in the sequence instance variable. " |
|
183 |
||
184 |
^ sequence at: (interval at: index) |
|
185 |
! |
|
186 |
||
187 |
at: index put: value |
|
188 |
"Store the argument value in the indexable field of the sequence |
|
189 |
instance variable indicated by index. |
|
190 |
Answer the value that was stored." |
|
191 |
||
192 |
^ sequence at: (interval at: index) put: value |
|
193 |
! |
|
194 |
||
195 |
size |
|
196 |
"Answer how many elements the receiver contains." |
|
197 |
||
198 |
^ interval size |
|
199 |
! |
|
200 |
||
201 |
slide |
|
202 |
"slide by 1" |
|
203 |
||
204 |
self slide: 1 |
|
205 |
! |
|
206 |
||
207 |
slide: anIncrement |
|
208 |
"given an increment, adjust the reindex map by sliding it that far" |
|
209 |
||
210 |
interval := interval + anIncrement |
|
211 |
! ! |
|
212 |
||
213 |
!ReindexedCollection methodsFor:'adding & removing'! |
|
214 |
||
215 |
add: anObject |
|
2931 | 216 |
"report an error; reindexedCollections cannot add elements" |
217 |
||
1190 | 218 |
self shouldNotImplement |
219 |
! ! |
|
220 |
||
1453 | 221 |
!ReindexedCollection methodsFor:'converting-reindexed'! |
222 |
||
223 |
from:startIndex |
|
3783 | 224 |
"return a new collection representing the receiver's elements starting at startIndex." |
1453 | 225 |
|
226 |
interval step == 1 ifTrue:[ |
|
227 |
^ ReindexedCollection |
|
228 |
on:sequence |
|
229 |
from:(interval start + startIndex - 1) |
|
230 |
to:(interval stop) |
|
231 |
by:(interval step) |
|
232 |
]. |
|
233 |
"could be more intelligent here..." |
|
234 |
^ super from:startIndex |
|
235 |
||
236 |
" |
|
237 |
|coll cdr cddr cdddr| |
|
238 |
||
239 |
coll := #(1 2 3 4 5 6 7 8 9 10). |
|
240 |
cdr := coll from:2. |
|
241 |
cddr := cdr from:2. |
|
242 |
cdddr := cddr from:2. |
|
243 |
" |
|
244 |
! |
|
245 |
||
246 |
from:startIndex to:stopIndex |
|
3783 | 247 |
"return a new collection representing the receiver's elements |
1453 | 248 |
starting at startIndex upTo and including endIndex." |
249 |
||
250 |
interval step == 1 ifTrue:[ |
|
251 |
^ ReindexedCollection |
|
252 |
on:sequence |
|
253 |
from:(interval start + startIndex - 1) |
|
254 |
to:((interval start + stopIndex - 1) min:interval stop) |
|
255 |
by:1 |
|
256 |
]. |
|
257 |
"could be more intelligent here..." |
|
258 |
^ super from:startIndex to:stopIndex |
|
259 |
||
260 |
" |
|
261 |
|coll cdrButLast cddrButLast2 cdddrButLast3| |
|
262 |
||
263 |
coll := #(1 2 3 4 5 6 7 8 9 10). |
|
264 |
cdrButLast := coll from:2 to:9. |
|
265 |
cddrButLast2 := cdrButLast from:2 to:7. |
|
266 |
cdddrButLast3 := cddrButLast2 from:2 to:5. |
|
267 |
" |
|
268 |
||
269 |
" |
|
270 |
|coll cdrButLast cddrButLast2 cdddrButLast3| |
|
271 |
||
272 |
coll := 1 to:100. |
|
273 |
cdrButLast := coll from:2 to:99. |
|
274 |
cddrButLast2 := cdrButLast from:2 to:97. |
|
275 |
cdddrButLast3 := cddrButLast2 from:2 to:95. |
|
276 |
" |
|
277 |
! |
|
278 |
||
279 |
to:stopIndex |
|
3783 | 280 |
"return a new collection representing the receiver's elements upTo and including endIndex." |
1453 | 281 |
|
282 |
interval step == 1 ifTrue:[ |
|
283 |
^ ReindexedCollection |
|
284 |
on:sequence |
|
285 |
from:(interval start) |
|
286 |
to:((interval start + stopIndex - 1) min:interval stop) |
|
287 |
by:1 |
|
288 |
]. |
|
289 |
"could be more intelligent here..." |
|
290 |
^ super to:stopIndex |
|
291 |
||
292 |
" |
|
293 |
|coll butLast butLast2 butLast3| |
|
294 |
||
295 |
coll := #(1 2 3 4 5 6 7 8 9 10). |
|
296 |
butLast := coll to:9. |
|
297 |
butLast2 := butLast to:8. |
|
298 |
butLast3 := butLast2 to:7. |
|
299 |
" |
|
300 |
! ! |
|
301 |
||
1190 | 302 |
!ReindexedCollection methodsFor:'initialization'! |
303 |
||
304 |
initialize: aSequence from: start to: stop by: step |
|
305 |
sequence := aSequence. |
|
306 |
interval := Interval from: start to: stop by: step |
|
307 |
! |
|
308 |
||
309 |
initialize: aSequence with: anInterval |
|
310 |
sequence := aSequence. |
|
311 |
interval := anInterval |
|
312 |
! ! |
|
313 |
||
314 |
!ReindexedCollection methodsFor:'queries'! |
|
315 |
||
3020 | 316 |
isFixedSize |
317 |
"return true if the receiver cannot grow" |
|
318 |
||
319 |
^ sequence isFixedSize |
|
320 |
! |
|
321 |
||
1190 | 322 |
species |
3861 | 323 |
"Answer the preferred class for reconstructing the receiver, |
324 |
that is, the sequence." |
|
1190 | 325 |
|
326 |
^ sequence species |
|
3687 | 327 |
! |
328 |
||
329 |
speciesForAdding |
|
330 |
"Answer the preferred class for reconstructing the receiver incrementally" |
|
331 |
||
332 |
^ sequence speciesForAdding |
|
1190 | 333 |
! ! |
334 |
||
335 |
!ReindexedCollection class methodsFor:'documentation'! |
|
336 |
||
337 |
version |
|
3687 | 338 |
^ '$Header$' |
1190 | 339 |
! ! |
2931 | 340 |