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