author | chzeiher |
Tue, 27 Nov 2018 16:31:23 +0100 | |
changeset 4770 | ff398f221e71 |
parent 4713 | 94dfbb936913 |
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. |
|
4145 | 43 |
The elements in the ReindexedCollection are the elements of the original collection |
44 |
at 'some start' to 'some stop' (optionally 'by 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 |
||
4145 | 49 |
For example, |
50 |
coll1 := #(1 2 3 4 5 6 7 8 9 10). |
|
51 |
coll2 := coll1 from:8. |
|
52 |
gives us a collection in coll2, which 'contains' 3 elements, 8, 9, 10 |
|
53 |
with indices 1,2,3. |
|
54 |
I.e. a slice from the other array. |
|
55 |
||
56 |
The reindexed collection is 'read-only'. I.e. it does not allow for elements to be changed. |
|
57 |
||
1190 | 58 |
See class side examples. |
59 |
||
60 |
[Instance Variables:] |
|
4145 | 61 |
sequence <SequenceableCollection> the sequence that will be reindexed. |
62 |
interval <Interval> the object that describes indicies of interest in the sequence. |
|
1190 | 63 |
|
1191 | 64 |
[Origin:] |
65 |
Part of the Engineering Math Goodies package from Travis. |
|
66 |
||
67 |
[Author:] |
|
68 |
Travis Griggs (tgriggs@keyww.com or tkc@bmi.net) |
|
69 |
Ported from Squeak by Claus Gittinger (not much of a port, though) |
|
1190 | 70 |
" |
71 |
! |
|
72 |
||
73 |
examples |
|
74 |
" |
|
75 |
|coll| |
|
76 |
||
1618 | 77 |
coll := #(1 2 3 4 5 6 7 8 9 10) from:8. |
78 |
Transcript show:'from 8: '; showCR:coll. |
|
79 |
Transcript show:'size: '; showCR:(coll size). |
|
80 |
Transcript show:'at 1: '; showCR:(coll at:1). |
|
81 |
Transcript show:'first: '; showCR:(coll first). |
|
82 |
Transcript show:'last: '; showCR:(coll last). |
|
83 |
coll do:[:each | Transcript show:'do: '; showCR:each]. |
|
84 |
coll reverseDo:[:each | Transcript show:'reverseDo: '; showCR:each]. |
|
1190 | 85 |
|
86 |
||
87 |
|coll| |
|
88 |
||
89 |
coll := (1 to:10) asOrderedCollection from:3 to:8. |
|
90 |
coll. |
|
91 |
coll size. |
|
92 |
coll at:1. |
|
93 |
coll do:[:each | Transcript showCR:each]. |
|
94 |
||
95 |
||
96 |
|coll| |
|
97 |
||
98 |
coll := (1 to:10) asOrderedCollection to:4. |
|
99 |
coll. |
|
100 |
coll size. |
|
101 |
coll at:1. |
|
102 |
coll last. |
|
103 |
coll do:[:each | Transcript showCR:each]. |
|
104 |
" |
|
105 |
! ! |
|
106 |
||
107 |
!ReindexedCollection class methodsFor:'instance creation'! |
|
108 |
||
109 |
on: aSequence from: start |
|
110 |
"Create a reindexedCollection on aSequence from start to the end of aSequence" |
|
111 |
||
112 |
^ self |
|
113 |
on: aSequence |
|
114 |
from: start |
|
115 |
to: aSequence size |
|
116 |
by: 1 |
|
117 |
! |
|
118 |
||
119 |
on: aSequence from: start by: step |
|
120 |
"Create a reindexedCollection on aSequence start to the end of aSequence |
|
121 |
if step is positive, else |
|
122 |
from start to the beginning of the sequence if step is negative." |
|
123 |
||
124 |
^ self |
|
125 |
on: aSequence |
|
126 |
from: start |
|
127 |
to: (step > 0 |
|
128 |
ifTrue: [aSequence size] |
|
129 |
ifFalse: [1]) |
|
130 |
by: step |
|
131 |
! |
|
132 |
||
133 |
on: aSequence from: start to: stop |
|
134 |
"Create a reindexedCollection on aSequence from start to stop by 1 |
|
135 |
(or -1 if start is greater than stop)" |
|
136 |
||
137 |
^ self |
|
138 |
on: aSequence |
|
139 |
from: start |
|
140 |
to: stop |
|
141 |
by: (start <= stop |
|
142 |
ifTrue: [1] |
|
143 |
ifFalse: [-1]) |
|
144 |
! |
|
145 |
||
146 |
on: aSequence from: start to: stop by: step |
|
147 |
"Create a reindexedCollection on aSequence from start to stop by step" |
|
148 |
||
149 |
^ self new |
|
150 |
initialize: aSequence |
|
151 |
from: start |
|
152 |
to: stop |
|
153 |
by: step |
|
154 |
! |
|
155 |
||
156 |
on: aSequence to: stop |
|
157 |
"Create a reindexedCollection on aSequence from 1 to stop by 1" |
|
158 |
||
159 |
^ self |
|
160 |
on: aSequence |
|
161 |
from: 1 |
|
162 |
to: stop |
|
163 |
by: 1 |
|
164 |
! |
|
165 |
||
166 |
on: aSequence to: stop by: step |
|
167 |
"Create a reindexedCollection on aSequence from 1 to stop (if step is |
|
168 |
positive) or the end to stop (if |
|
169 |
step is negative). Note: if step is not 1 or -1, there is a chance that the |
|
170 |
index specified by stop may |
|
171 |
not be in the interval." |
|
172 |
||
173 |
^ self |
|
174 |
on: aSequence |
|
175 |
from: (step > 0 |
|
176 |
ifTrue: [1] |
|
177 |
ifFalse: [aSequence size]) |
|
178 |
to: stop |
|
179 |
by: step |
|
180 |
! |
|
181 |
||
182 |
on: aSequence with: anInterval |
|
183 |
"Create a reindexedCollection on aSequence" |
|
184 |
||
185 |
^ self new initialize: aSequence with: anInterval |
|
186 |
! ! |
|
187 |
||
188 |
!ReindexedCollection methodsFor:'accessing'! |
|
189 |
||
190 |
at: index |
|
191 |
"Answer the value of an indexable field in the sequence instance variable. " |
|
192 |
||
193 |
^ sequence at: (interval at: index) |
|
194 |
! |
|
195 |
||
196 |
at: index put: value |
|
197 |
"Store the argument value in the indexable field of the sequence |
|
198 |
instance variable indicated by index. |
|
199 |
Answer the value that was stored." |
|
200 |
||
4342 | 201 |
self shouldNotImplement. "/ I am readOnly |
1190 | 202 |
^ sequence at: (interval at: index) put: value |
4342 | 203 |
|
204 |
"Modified: / 22-02-2017 / 10:46:21 / cg" |
|
1190 | 205 |
! |
206 |
||
207 |
size |
|
208 |
"Answer how many elements the receiver contains." |
|
209 |
||
210 |
^ interval size |
|
211 |
! |
|
212 |
||
213 |
slide |
|
214 |
"slide by 1" |
|
215 |
||
216 |
self slide: 1 |
|
217 |
! |
|
218 |
||
219 |
slide: anIncrement |
|
220 |
"given an increment, adjust the reindex map by sliding it that far" |
|
221 |
||
222 |
interval := interval + anIncrement |
|
223 |
! ! |
|
224 |
||
225 |
!ReindexedCollection methodsFor:'adding & removing'! |
|
226 |
||
227 |
add: anObject |
|
2931 | 228 |
"report an error; reindexedCollections cannot add elements" |
229 |
||
1190 | 230 |
self shouldNotImplement |
231 |
! ! |
|
232 |
||
1453 | 233 |
!ReindexedCollection methodsFor:'converting-reindexed'! |
234 |
||
235 |
from:startIndex |
|
3783 | 236 |
"return a new collection representing the receiver's elements starting at startIndex." |
1453 | 237 |
|
238 |
interval step == 1 ifTrue:[ |
|
239 |
^ ReindexedCollection |
|
240 |
on:sequence |
|
241 |
from:(interval start + startIndex - 1) |
|
242 |
to:(interval stop) |
|
243 |
by:(interval step) |
|
244 |
]. |
|
245 |
"could be more intelligent here..." |
|
246 |
^ super from:startIndex |
|
247 |
||
248 |
" |
|
249 |
|coll cdr cddr cdddr| |
|
250 |
||
251 |
coll := #(1 2 3 4 5 6 7 8 9 10). |
|
252 |
cdr := coll from:2. |
|
253 |
cddr := cdr from:2. |
|
254 |
cdddr := cddr from:2. |
|
255 |
" |
|
256 |
! |
|
257 |
||
258 |
from:startIndex to:stopIndex |
|
3783 | 259 |
"return a new collection representing the receiver's elements |
1453 | 260 |
starting at startIndex upTo and including endIndex." |
261 |
||
262 |
interval step == 1 ifTrue:[ |
|
263 |
^ ReindexedCollection |
|
264 |
on:sequence |
|
265 |
from:(interval start + startIndex - 1) |
|
266 |
to:((interval start + stopIndex - 1) min:interval stop) |
|
267 |
by:1 |
|
268 |
]. |
|
269 |
"could be more intelligent here..." |
|
270 |
^ super from:startIndex to:stopIndex |
|
271 |
||
272 |
" |
|
273 |
|coll cdrButLast cddrButLast2 cdddrButLast3| |
|
274 |
||
275 |
coll := #(1 2 3 4 5 6 7 8 9 10). |
|
276 |
cdrButLast := coll from:2 to:9. |
|
277 |
cddrButLast2 := cdrButLast from:2 to:7. |
|
278 |
cdddrButLast3 := cddrButLast2 from:2 to:5. |
|
279 |
" |
|
280 |
||
281 |
" |
|
282 |
|coll cdrButLast cddrButLast2 cdddrButLast3| |
|
283 |
||
284 |
coll := 1 to:100. |
|
285 |
cdrButLast := coll from:2 to:99. |
|
286 |
cddrButLast2 := cdrButLast from:2 to:97. |
|
287 |
cdddrButLast3 := cddrButLast2 from:2 to:95. |
|
288 |
" |
|
289 |
! |
|
290 |
||
291 |
to:stopIndex |
|
3783 | 292 |
"return a new collection representing the receiver's elements upTo and including endIndex." |
1453 | 293 |
|
294 |
interval step == 1 ifTrue:[ |
|
295 |
^ ReindexedCollection |
|
296 |
on:sequence |
|
297 |
from:(interval start) |
|
298 |
to:((interval start + stopIndex - 1) min:interval stop) |
|
299 |
by:1 |
|
300 |
]. |
|
301 |
"could be more intelligent here..." |
|
302 |
^ super to:stopIndex |
|
303 |
||
304 |
" |
|
305 |
|coll butLast butLast2 butLast3| |
|
306 |
||
307 |
coll := #(1 2 3 4 5 6 7 8 9 10). |
|
308 |
butLast := coll to:9. |
|
309 |
butLast2 := butLast to:8. |
|
310 |
butLast3 := butLast2 to:7. |
|
311 |
" |
|
312 |
! ! |
|
313 |
||
1190 | 314 |
!ReindexedCollection methodsFor:'initialization'! |
315 |
||
316 |
initialize: aSequence from: start to: stop by: step |
|
317 |
sequence := aSequence. |
|
318 |
interval := Interval from: start to: stop by: step |
|
319 |
! |
|
320 |
||
321 |
initialize: aSequence with: anInterval |
|
4342 | 322 |
sequence := aSequence. |
323 |
interval := anInterval |
|
324 |
||
325 |
"Modified (format): / 22-02-2017 / 10:46:38 / cg" |
|
1190 | 326 |
! ! |
327 |
||
328 |
!ReindexedCollection methodsFor:'queries'! |
|
329 |
||
4713 | 330 |
characterSize |
331 |
^ sequence characterSize |
|
332 |
||
333 |
"Created: / 26-08-2018 / 21:16:39 / Claus Gittinger" |
|
334 |
! |
|
335 |
||
3020 | 336 |
isFixedSize |
337 |
"return true if the receiver cannot grow" |
|
338 |
||
339 |
^ sequence isFixedSize |
|
340 |
! |
|
341 |
||
1190 | 342 |
species |
3861 | 343 |
"Answer the preferred class for reconstructing the receiver, |
344 |
that is, the sequence." |
|
1190 | 345 |
|
346 |
^ sequence species |
|
3687 | 347 |
! |
348 |
||
349 |
speciesForAdding |
|
350 |
"Answer the preferred class for reconstructing the receiver incrementally" |
|
351 |
||
352 |
^ sequence speciesForAdding |
|
1190 | 353 |
! ! |
354 |
||
355 |
!ReindexedCollection class methodsFor:'documentation'! |
|
356 |
||
357 |
version |
|
3687 | 358 |
^ '$Header$' |
1190 | 359 |
! ! |
2931 | 360 |