author | Claus Gittinger <cg@exept.de> |
Sat, 11 May 1996 11:16:03 +0200 | |
changeset 1370 | b051117cc302 |
parent 1369 | 5e7b6fabd248 |
child 1385 | f90c476be6e3 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 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 |
" |
|
12 |
||
13 |
Collection subclass:#SequenceableCollection |
|
911
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
14 |
instanceVariableNames:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
15 |
classVariableNames:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
16 |
poolDictionaries:'' |
8bf5e91a7f68
Use doWhile: instead of whileTrue in topological sort (is compiled to inlined code).
Stefan Vogel <sv@exept.de>
parents:
890
diff
changeset
|
17 |
category:'Collections-Abstract' |
1 | 18 |
! |
19 |
||
88 | 20 |
!SequenceableCollection class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
SequenceableCollections have ordered elements which can be accessed via |
|
39 |
a numeric index. |
|
40 |
SequenceableCollection is an abstract class - there are no instances of |
|
41 |
it in the system. See concrete subclasses (such as OrderedCollection). |
|
1289 | 42 |
|
43 |
[author:] |
|
44 |
Claus Gittinger |
|
88 | 45 |
" |
46 |
! ! |
|
1 | 47 |
|
48 |
!SequenceableCollection class methodsFor:'instance creation'! |
|
49 |
||
50 |
new:size withAll:element |
|
360 | 51 |
"return a new collection of size, where all elements are |
52 |
initialized to element." |
|
1 | 53 |
|
54 |
|newCollection| |
|
55 |
||
56 |
newCollection := self new:size. |
|
57 |
newCollection atAllPut:element. |
|
58 |
^ newCollection |
|
360 | 59 |
! |
60 |
||
61 |
withSize:size |
|
62 |
"return a new collection of size. |
|
63 |
For variable size collections, this is different from #new:, |
|
64 |
in that #new: creates an empty collection with preallocated size, |
|
65 |
while #withSize: creates a non empty one." |
|
66 |
||
67 |
|newCollection| |
|
68 |
||
69 |
newCollection := self new:size. |
|
70 |
newCollection grow:size. |
|
71 |
^ newCollection |
|
72 |
||
73 |
" |
|
74 |
(OrderedCollection new:10) inspect. |
|
75 |
(OrderedCollection withSize:10) inspect. |
|
76 |
(Array new:10) inspect. |
|
77 |
(Array withSize:10) inspect. |
|
78 |
" |
|
1 | 79 |
! ! |
80 |
||
81 |
!SequenceableCollection methodsFor:'accessing'! |
|
82 |
||
1366 | 83 |
after:anObject |
84 |
"return the element, after anObject. |
|
85 |
If anObject is not in the receiver, report an error. |
|
86 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
|
87 |
||
88 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
89 |
||
90 |
" |
|
91 |
#(4 3 2 1) asOrderedCollection after:3. |
|
92 |
#(4 3 2 1) asOrderedCollection after:5 |
|
93 |
#(4 3 2 1) asOrderedCollection after:1 |
|
94 |
" |
|
95 |
||
96 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
97 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
98 |
! |
|
99 |
||
100 |
after:anObject ifAbsent:exceptionBlock |
|
101 |
"return the element, after anObject. If there is no such element, |
|
102 |
return the value from exceptionBlock." |
|
103 |
||
104 |
^ self subclassResponsibility |
|
105 |
||
106 |
"Created: 10.5.1996 / 14:00:10 / cg" |
|
107 |
! |
|
108 |
||
32 | 109 |
at:index ifAbsent:exceptionBlock |
61 | 110 |
"return the element at index if valid. |
111 |
If the index is invalid, return the result of evaluating |
|
302 | 112 |
the exceptionblock. |
113 |
NOTICE: |
|
114 |
in ST-80, this message is only defined for Dictionaries, |
|
115 |
however, having a common protocol with indexed collections |
|
116 |
often simplifies things." |
|
32 | 117 |
|
118 |
((index < 1) or:[index > self size]) ifTrue:[ |
|
159 | 119 |
^ exceptionBlock value |
32 | 120 |
]. |
121 |
^ self at:index |
|
122 |
||
95 | 123 |
" |
124 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 125 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
126 |
||
95 | 127 |
(Dictionary with:(#foo -> #bar) |
159 | 128 |
with:(#frob -> #baz)) |
129 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 130 |
" |
362 | 131 |
! |
132 |
||
1366 | 133 |
before:anObject |
134 |
"return the element before the argument, anObject. |
|
135 |
If anObject is not in the receiver, report an error. |
|
136 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
|
137 |
||
138 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
139 |
||
140 |
" |
|
141 |
#(4 3 2 1) asOrderedCollection before:3. |
|
142 |
#(4 3 2 1) asOrderedCollection before:4 |
|
143 |
#(4 3 2 1) asOrderedCollection before:0 |
|
144 |
" |
|
145 |
||
146 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
147 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
148 |
! |
|
149 |
||
150 |
before:anObject ifAbsent:exceptionBlock |
|
151 |
"return the element, before anObject. If there is no such element, |
|
152 |
return the value from exceptionBlock." |
|
153 |
||
154 |
^ self subclassResponsibility |
|
155 |
||
156 |
"Created: 10.5.1996 / 14:03:53 / cg" |
|
157 |
! |
|
158 |
||
602 | 159 |
first |
160 |
"return the first element" |
|
161 |
||
162 |
^ self at:1 |
|
362 | 163 |
! |
164 |
||
165 |
keyAtEqualValue:value |
|
166 |
"return the index of a value. |
|
167 |
This is normally not used (use indexOf:), but makes the |
|
168 |
protocol more compatible with dictionaries." |
|
169 |
||
170 |
^ self indexOf:value |
|
171 |
! |
|
172 |
||
173 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
174 |
"return the index of a value. |
|
175 |
This is normally not used (use indexOf:), but makes the |
|
176 |
protocol more compatible with dictionaries." |
|
177 |
||
178 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 179 |
! |
180 |
||
181 |
keyAtValue:value |
|
182 |
"return the index of a value. |
|
183 |
This is normally not used (use indexOf:), but makes the |
|
184 |
protocol more compatible with dictionaries." |
|
185 |
||
186 |
^ self identityIndexOf:value |
|
187 |
! |
|
188 |
||
189 |
keyAtValue:value ifAbsent:exceptionBlock |
|
190 |
"return the index of a value. |
|
191 |
This is normally not used (use indexOf:), but makes the |
|
192 |
protocol more compatible with dictionaries." |
|
193 |
||
194 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
195 |
! |
|
196 |
||
197 |
last |
|
198 |
"return the last element" |
|
199 |
||
200 |
^ self at:(self size) |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
201 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
202 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
203 |
upTo:anElement |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
204 |
"return a new collection consisting of the receivers elements upTo |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
205 |
(but excluding) anElement. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
206 |
If anElement is not in the receiver, the returned collection |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
207 |
will consist of all elements of the receiver" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
208 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
209 |
|pos| |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
210 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
211 |
pos := self indexOf:anElement. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
212 |
pos == 0 ifTrue:[^ self copy]. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
213 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
214 |
^ self copyFrom:1 to:(pos - 1) |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
215 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
216 |
" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
217 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
218 |
'hello world' upTo:Character space |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
219 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
220 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
221 |
raises an error: |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
222 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
223 |
(Dictionary new |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
224 |
at:#foo put:'foo'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
225 |
at:#bar put:'bar'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
226 |
at:#baz put:'baz'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
227 |
yourself) upTo:#bar |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
228 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
229 |
" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
230 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
231 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
1 | 232 |
! ! |
233 |
||
602 | 234 |
!SequenceableCollection methodsFor:'adding & removing'! |
235 |
||
236 |
add:anObject |
|
237 |
"append the argument, anObject to the collection. |
|
238 |
Return the argument, anObject. |
|
239 |
||
240 |
Notice, that this is modifies the receiver NOT a copy. |
|
241 |
Also note, that it may be a slow operation for some collections, |
|
242 |
due to the grow:-message, which is inefficient for fixed size |
|
243 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
244 |
||
245 |
|newSize| |
|
246 |
||
247 |
newSize := self size + 1. |
|
248 |
self grow:newSize. |
|
249 |
self at:newSize put:anObject. |
|
250 |
^ anObject |
|
251 |
||
252 |
" |
|
253 |
|a| |
|
254 |
a := #(1 2 3 4 5 6 7 8). |
|
255 |
a add:'hello'. |
|
256 |
a |
|
257 |
" |
|
258 |
" |
|
259 |
|c| |
|
260 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
261 |
c add:'hello'. |
|
262 |
c |
|
263 |
" |
|
264 |
! |
|
265 |
||
266 |
add:anElement beforeIndex:index |
|
267 |
"insert the first argument, anObject into the collection before slot index. |
|
268 |
Return the receiver (sigh - ST-80 compatibility). |
|
269 |
||
270 |
Notice, that this is modifies the receiver NOT a copy. |
|
271 |
Also note, that it may be a slow operation for some collections, |
|
272 |
due to the grow:-message, which is inefficient for fixed size |
|
273 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
274 |
||
275 |
|newSize| |
|
276 |
||
277 |
newSize := self size + 1. |
|
278 |
self grow:newSize. |
|
279 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
280 |
self at:index put:anElement |
|
281 |
||
282 |
" |
|
283 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
284 |
" |
|
202 | 285 |
! |
286 |
||
602 | 287 |
addFirst:anObject |
288 |
"prepend the argument, anObject to the collection. |
|
289 |
Return the argument, anObject. |
|
290 |
||
291 |
Notice, that this is modifies the receiver NOT a copy. |
|
292 |
Also note, that it may be a slow operation for some collections, |
|
293 |
due to the grow:-message, which is inefficient for fixed size |
|
294 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
295 |
||
296 |
|newSize| |
|
297 |
||
298 |
newSize := self size + 1. |
|
299 |
self grow:newSize. |
|
300 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
301 |
self at:1 put:anObject. |
|
302 |
^ anObject |
|
303 |
||
304 |
" |
|
305 |
|a| |
|
306 |
a:= #(1 2 3 4 5 6 7 8). |
|
307 |
a addFirst:'hello'. |
|
308 |
a |
|
309 |
" |
|
310 |
" |
|
311 |
|c| |
|
312 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
313 |
c addFirst:'hello'. |
|
314 |
c |
|
315 |
" |
|
69 | 316 |
! |
317 |
||
602 | 318 |
remove:anElement ifAbsent:aBlock |
319 |
"search for anElement and, if present remove and return it. |
|
320 |
If not present, return the value of evaluating aBlock. |
|
321 |
||
322 |
Notice, that this is modifies the receiver NOT a copy. |
|
323 |
Also note, that it may be a slow operation for some collections, |
|
324 |
due to the grow:-message, which is inefficient for fixed size |
|
325 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
326 |
||
327 |
|any |
|
328 |
dstIndex "{ Class: SmallInteger }" |
|
329 |
sz "{ Class: SmallInteger }"| |
|
330 |
||
331 |
dstIndex := 1. |
|
332 |
any := false. |
|
333 |
sz := self size. |
|
334 |
1 to:sz do:[:srcIndex | |
|
335 |
(anElement = (self at:srcIndex)) ifTrue:[ |
|
336 |
any := true |
|
337 |
] ifFalse:[ |
|
338 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
339 |
self at:dstIndex put:(self at:srcIndex) |
|
340 |
]. |
|
341 |
dstIndex := dstIndex + 1 |
|
342 |
] |
|
343 |
]. |
|
344 |
any ifTrue:[ |
|
345 |
self grow:dstIndex - 1. |
|
346 |
^ anElement |
|
347 |
]. |
|
348 |
^ aBlock value |
|
349 |
||
350 |
" |
|
351 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCr:'no'] |
|
352 |
#(1 2 3 4 5 6 7 8 9 0) remove:99 ifAbsent:[#oops] |
|
353 |
" |
|
354 |
! |
|
355 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
356 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
357 |
"remove all elements that meet a test criteria as specified in aBlock. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
358 |
The argument, aBlock is evaluated for successive elements and all those, |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
359 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
360 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
361 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
362 |
"/ this is a q&d implementation (possibly slow). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
363 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
364 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
365 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
366 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
367 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
368 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
369 |
[runIndex <= self size] whileTrue:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
370 |
element := self at:runIndex. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
371 |
(aBlock value:element) ifTrue:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
372 |
removed add:element. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
373 |
self removeAtIndex:runIndex |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
374 |
] ifFalse:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
375 |
runIndex := runIndex + 1 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
376 |
] |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
377 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
378 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
379 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
380 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
381 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
382 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
383 |
coll := OrderedCollection withAll:(1 to:10). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
384 |
Transcript showCr:(coll removeAllSuchThat:[:el | el even]). |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
385 |
Transcript showCr:coll |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
386 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
387 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
388 |
"Created: 11.5.1996 / 09:49:30 / cg" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
389 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
390 |
|
602 | 391 |
removeAtIndex:anIndex |
392 |
"remove the element stored at anIndex. Return the removed object. |
|
393 |
||
394 |
Notice, that this is modifies the receiver NOT a copy. |
|
395 |
Also note, that it may be a slow operation for some collections, |
|
396 |
due to the grow:-message, which is inefficient for fixed size |
|
397 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
398 |
||
399 |
|element| |
|
400 |
||
401 |
element := self at:anIndex. |
|
402 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
403 |
^ element |
|
404 |
||
405 |
" |
|
406 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
407 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
408 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
409 |
" |
|
13 | 410 |
! |
411 |
||
602 | 412 |
removeFirst |
413 |
"remove the first element of the receiver and return it. |
|
414 |
||
415 |
Notice, that this is modifies the receiver NOT a copy. |
|
416 |
Also note, that it may be a slow operation for some collections, |
|
417 |
due to the grow:-message, which is inefficient for fixed size |
|
418 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
419 |
||
420 |
^ self removeAtIndex:1 |
|
421 |
||
422 |
" |
|
423 |
|a| |
|
424 |
a := #(1 2 3 4 5 6). |
|
425 |
a removeFirst. |
|
426 |
a |
|
427 |
" |
|
13 | 428 |
! |
429 |
||
602 | 430 |
removeFromIndex:startIndex toIndex:endIndex |
431 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
432 |
Return the receiver. |
|
433 |
Returning the receiver is a historic leftover - it may at one |
|
434 |
time return a collection of the removed elements. |
|
435 |
||
436 |
Notice, that this is modifies the receiver - NOT a copy; |
|
437 |
therefore any other users of the receiver will also see this change. |
|
438 |
Also note, that it may be a slow operation for some collections, |
|
439 |
due to the grow:-message, which is inefficient for fixed size |
|
440 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
441 |
||
442 |
|size newSize| |
|
443 |
||
444 |
(endIndex >= (size := self size)) ifTrue:[ |
|
445 |
self grow:(startIndex - 1) |
|
446 |
] ifFalse:[ |
|
447 |
newSize := size - endIndex + startIndex - 1. |
|
448 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
449 |
self grow:newSize |
|
450 |
] |
|
451 |
||
452 |
"/ |newSize| |
|
453 |
"/ |
|
454 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
455 |
"/ newSize <= 0 ifTrue:[ |
|
456 |
"/ self grow:0 |
|
457 |
"/ ] ifFalse:[ |
|
458 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
459 |
"/ self grow:newSize |
|
460 |
"/ ] |
|
461 |
||
462 |
" |
|
463 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
|
464 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
465 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
466 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
467 |
" |
|
468 |
! |
|
469 |
||
470 |
removeIndex:index |
|
471 |
"remove the argument stored at index. Return the receiver. |
|
472 |
||
473 |
Notice, that this is modifies the receiver NOT a copy. |
|
474 |
Also note, that it may be a slow operation for some collections, |
|
475 |
due to the grow:-message, which is inefficient for fixed size |
|
476 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
477 |
||
478 |
self removeFromIndex:index toIndex:index |
|
479 |
||
480 |
" |
|
481 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
482 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
483 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
484 |
" |
|
77 | 485 |
! |
486 |
||
602 | 487 |
removeLast |
488 |
"remove the last element of the receiver and return it. |
|
489 |
||
490 |
Notice, that this is modifies the receiver NOT a copy. |
|
491 |
Also note, that it may be a slow operation for some collections, |
|
492 |
due to the grow:-message, which is inefficient for fixed size |
|
493 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
494 |
||
495 |
^ self removeAtIndex:(self size) |
|
496 |
||
497 |
" |
|
498 |
|a| |
|
499 |
a := #(1 2 3 4 5 6). |
|
500 |
a removeLast. |
|
501 |
a |
|
502 |
" |
|
503 |
! ! |
|
504 |
||
505 |
!SequenceableCollection methodsFor:'comparing'! |
|
506 |
||
507 |
= aCollection |
|
508 |
"return true if the receiver and aCollection represent collections |
|
509 |
with equal contents." |
|
510 |
||
511 |
|index "{ Class: SmallInteger }" |
|
512 |
stop "{ Class: SmallInteger }" | |
|
513 |
||
514 |
(aCollection == self) ifTrue:[^true]. |
|
515 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
516 |
||
517 |
stop := self size. |
|
518 |
stop == (aCollection size) ifFalse:[^false]. |
|
519 |
||
520 |
index := 1. |
|
521 |
[index <= stop] whileTrue:[ |
|
522 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
523 |
index := index + 1 |
|
524 |
]. |
|
525 |
^ true |
|
526 |
||
527 |
" |
|
528 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
|
529 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
530 |
#($1 $2 $3 $4 $5) = '12345' |
|
531 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
532 |
" |
|
533 |
! |
|
534 |
||
535 |
endsWith:aCollection |
|
536 |
"return true, if the receivers last elements match those |
|
537 |
of aCollection" |
|
538 |
||
539 |
|index1 "{ Class: SmallInteger }" |
|
540 |
index2 "{ Class: SmallInteger }" |
|
541 |
stop "{ Class: SmallInteger }" |
|
542 |
sz "{ Class: SmallInteger }"| |
|
543 |
||
544 |
(aCollection == self) ifTrue:[^ true]. |
|
545 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
546 |
||
547 |
stop := aCollection size. |
|
77 | 548 |
sz := self size. |
602 | 549 |
stop > sz ifTrue:[^false]. |
550 |
||
551 |
index1 := sz. |
|
552 |
index2 := stop. |
|
553 |
[index2 > 0] whileTrue:[ |
|
554 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
|
555 |
index1 := index1 - 1. |
|
556 |
index2 := index2 - 1 |
|
77 | 557 |
]. |
602 | 558 |
^ true |
559 |
||
560 |
" |
|
561 |
'abcde' endsWith:#($d $e) |
|
562 |
#[1 2 3 4] endsWith:#(3 4) |
|
563 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
|
564 |
" |
|
565 |
! |
|
566 |
||
567 |
hash |
|
568 |
"return a hash key for the receiver" |
|
569 |
||
570 |
"this hash is stupid - but for larger collections, the hashing |
|
571 |
time can become much bigger than the time lost in added probing. |
|
572 |
Time will show ..." |
|
573 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
574 |
^ ((self at:1 ifAbsent:0) hash * self size) bitAnd:16r3FFFFFFF |
602 | 575 |
|
576 |
" |
|
577 |
#(1 2 3 4 5) hash |
|
578 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
579 |
" |
|
580 |
! |
|
581 |
||
582 |
startsWith:aCollection |
|
583 |
"return true, if the receivers first elements match those |
|
584 |
of aCollection" |
|
585 |
||
586 |
|index "{ Class: SmallInteger }" |
|
587 |
stop "{ Class: SmallInteger }" | |
|
588 |
||
589 |
(aCollection == self) ifTrue:[^true]. |
|
590 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
591 |
||
592 |
stop := aCollection size. |
|
593 |
stop > self size ifTrue:[^false]. |
|
594 |
||
595 |
index := 1. |
|
596 |
[index <= stop] whileTrue:[ |
|
597 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
598 |
index := index + 1 |
|
599 |
]. |
|
600 |
^ true |
|
601 |
||
602 |
" |
|
603 |
'abcde' startsWith:#($a $b $c) |
|
604 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
605 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
606 |
" |
|
13 | 607 |
! ! |
608 |
||
260 | 609 |
!SequenceableCollection methodsFor:'converting'! |
610 |
||
1188 | 611 |
asStringWith:sepChar |
612 |
"return a string generated by concatenating my elements |
|
613 |
(which must be strings or nil) and embedding sepChar characters in between. |
|
614 |
Nil entries and empty strings are counted as empty lines." |
|
615 |
||
616 |
^ self asStringWith:sepChar from:1 to:(self size) |
|
617 |
! |
|
618 |
||
619 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
620 |
"return part of myself as a string with embedded sepCharacters. |
|
621 |
My elements must be strings or nil; nil entries and empty strings are |
|
622 |
taken as empty lines." |
|
623 |
||
624 |
^ self |
|
625 |
asStringWith:sepCharacter |
|
626 |
from:firstLine |
|
627 |
to:lastLine |
|
628 |
compressTabs:false |
|
629 |
final:nil |
|
630 |
" |
|
631 |
creating entries for searchpath: |
|
632 |
||
633 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
|
634 |
||
635 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
636 |
" |
|
637 |
||
638 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
639 |
! |
|
640 |
||
260 | 641 |
asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter |
642 |
"return part of myself as a string with embedded sepCharacters. |
|
263 | 643 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 644 |
taken as empty lines. |
645 |
If the argument compressTabs is true, leading spaces are converted |
|
646 |
to tab-characters (8col tabs). The last line is followed by a final |
|
647 |
character (if non-nil). |
|
648 |
||
649 |
This method is tuned for big collections, in not creating many |
|
263 | 650 |
intermediate strings (has linear runtime). For very small collections |
651 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 652 |
|
293 | 653 |
|idx1 "{ Class:SmallInteger }" |
654 |
idx2 "{ Class:SmallInteger }" |
|
655 |
totalLength "{ Class:SmallInteger }" |
|
260 | 656 |
pos "{ Class:SmallInteger }" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
657 |
newString lineString spaces idx nTabs sepCnt |
1015 | 658 |
any16Bit stringClass| |
260 | 659 |
|
660 |
" |
|
661 |
first accumulate the size of the string, to avoid |
|
662 |
countless reallocations. If tabs are compressed, |
|
663 |
the size computed is not exact, but gives an upper bound ... |
|
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
664 |
On the fly, look if a 16bit string is needed. |
260 | 665 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
666 |
any16Bit := false. |
1015 | 667 |
stringClass := String. |
668 |
||
260 | 669 |
totalLength := 0. |
670 |
sepCnt := sepCharacter notNil ifTrue:[1] ifFalse:[0]. |
|
671 |
||
293 | 672 |
idx1 := firstLine. |
673 |
idx2 := lastLine. |
|
674 |
idx1 to:idx2 do:[:lineIndex | |
|
1015 | 675 |
lineString := self at:lineIndex. |
676 |
||
677 |
lineString isNil ifTrue:[ |
|
678 |
totalLength := totalLength + sepCnt |
|
679 |
] ifFalse: [ |
|
680 |
(lineString bitsPerCharacter == 16) ifTrue:[ |
|
681 |
any16Bit := true. |
|
1016 | 682 |
stringClass := lineString class |
1015 | 683 |
]. |
684 |
totalLength := totalLength + lineString size + sepCnt |
|
685 |
]. |
|
260 | 686 |
]. |
687 |
endCharacter isNil ifTrue:[ |
|
1015 | 688 |
totalLength := totalLength - 1 |
260 | 689 |
]. |
690 |
spaces := ' '. |
|
1016 | 691 |
newString := stringClass new:totalLength. |
260 | 692 |
|
693 |
" |
|
694 |
now, replace ... |
|
695 |
" |
|
696 |
pos := 1. |
|
293 | 697 |
idx1 to:idx2 do:[:lineIndex | |
1015 | 698 |
|thisLen| |
699 |
||
700 |
lineString := self at:lineIndex. |
|
701 |
thisLen := lineString size. |
|
702 |
thisLen ~~ 0 ifTrue:[ |
|
703 |
compressTabs ifTrue:[ |
|
704 |
" |
|
705 |
mhmh: could use withTabs from String-class here, |
|
706 |
but we should avoid creating too many temporary strings |
|
707 |
(especially, since this method is typically used when converting |
|
708 |
big texts such as when saving in the filebrowser ...). |
|
709 |
Therefore, we convert tabs inline here doing a direct replace |
|
710 |
in newString." |
|
711 |
||
712 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
713 |
nTabs := (idx-1) // 8. |
|
714 |
nTabs ~~ 0 ifTrue:[ |
|
715 |
"any tabs" |
|
716 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
717 |
pos := pos + nTabs. |
|
718 |
newString replaceFrom:pos with:lineString startingAt:(nTabs * 8 + 1). |
|
719 |
pos := pos + thisLen - (nTabs * 8). |
|
720 |
] ifFalse:[ |
|
721 |
newString replaceFrom:pos with:lineString. |
|
722 |
pos := pos + thisLen |
|
723 |
]. |
|
724 |
] ifFalse:[ |
|
725 |
newString replaceFrom:pos with:lineString. |
|
726 |
pos := pos + thisLen |
|
727 |
] |
|
728 |
]. |
|
729 |
(sepCharacter notNil |
|
730 |
and:[lineIndex ~~ lastLine]) ifTrue:[ |
|
731 |
newString at:pos put:sepCharacter. |
|
732 |
pos := pos + 1 |
|
733 |
] ifFalse:[ |
|
734 |
endCharacter notNil ifTrue:[ |
|
735 |
newString at:pos put:endCharacter. |
|
736 |
pos := pos + 1 |
|
737 |
] |
|
738 |
] |
|
260 | 739 |
]. |
740 |
||
741 |
" |
|
742 |
in case of tab compression, the result has to be |
|
743 |
cut to size ... sorry |
|
744 |
" |
|
745 |
pos ~~ totalLength ifTrue:[ |
|
1015 | 746 |
^ newString copyTo:(pos - 1) |
260 | 747 |
]. |
748 |
^ newString |
|
749 |
||
750 |
" |
|
751 |
creating entries for searchpath: |
|
752 |
||
753 |
#('foo' 'bar' 'baz' '/foo/bar') |
|
1015 | 754 |
asStringWith:$: from:1 to:4 compressTabs:false final:nil |
260 | 755 |
|
756 |
with trailing colon: |
|
757 |
||
758 |
#('foo' 'bar' 'baz' '/foo/bar') |
|
1015 | 759 |
asStringWith:$: from:1 to:4 compressTabs:false final:$: |
260 | 760 |
|
761 |
concatenating all elements: |
|
762 |
||
763 |
#('foo' 'bar' 'baz') |
|
1015 | 764 |
asStringWith:nil from:1 to:3 compressTabs:false final:nil |
260 | 765 |
|
766 |
creating a string from a collection of lines: |
|
767 |
||
768 |
#('foo' 'bar' 'baz') |
|
1015 | 769 |
asStringWith:(Character cr) from:1 to:3 compressTabs:false final:(Character cr) |
260 | 770 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
771 |
|
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
772 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
379 | 773 |
! |
774 |
||
602 | 775 |
asStringWithCRs |
776 |
"return a string generated by concatenating my elements |
|
777 |
(which must be strings or nil) and embedding cr characters in between. |
|
778 |
Nil entries and empty strings are counted as empty lines." |
|
779 |
||
780 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
781 |
! |
|
782 |
||
783 |
asStringWithCRsFrom:firstLine to:lastLine |
|
784 |
"return a string generated by concatenating some of my elements |
|
785 |
(which must be strings or nil) and embedding cr characters in between. |
|
786 |
Nil entries and empty strings are counted as empty lines." |
|
787 |
||
788 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
789 |
! |
|
790 |
||
791 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
792 |
"return part of myself as a string with embedded cr's. |
|
793 |
My elements must be strings or nil. |
|
794 |
If the argument compressTabs is true, leading spaces are converted |
|
795 |
to tab-characters (8col tabs). |
|
796 |
Nil entries and empty strings are taken as empty lines." |
|
797 |
||
798 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
799 |
! |
|
800 |
||
801 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
802 |
"return part of myself as a string with embedded cr's. |
|
803 |
My elements must be strings or nil; nil entries and empty strings are |
|
804 |
taken as empty lines. |
|
805 |
If the argument compressTabs is true, leading spaces are converted |
|
806 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
807 |
should be followed by a cr or not." |
|
808 |
||
809 |
^ self asStringWith:(Character cr) |
|
810 |
from:firstLine |
|
811 |
to:lastLine |
|
812 |
compressTabs:compressTabs |
|
813 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
814 |
! |
|
815 |
||
379 | 816 |
decodeAsLiteralArray |
1245 | 817 |
"given a literalEncoding in the receiver, |
818 |
create & return the corresponding object. |
|
819 |
The inverse operation to #literalArrayEncoding." |
|
820 |
||
379 | 821 |
|cls| |
822 |
||
823 |
cls := Smalltalk at:(self first) ifAbsent:nil. |
|
824 |
cls isNil ifTrue:[ |
|
1245 | 825 |
^ self error:'unknown class in spec:' , self first. |
379 | 826 |
]. |
827 |
^ cls new fromLiteralArrayEncoding:self. |
|
828 |
||
829 |
" |
|
830 |
#(Point 10 20) decodeAsLiteralArray |
|
831 |
#(Rectangle 100 200 400 500) decodeAsLiteralArray |
|
832 |
" |
|
1245 | 833 |
|
834 |
"Modified: 22.4.1996 / 12:59:03 / cg" |
|
422 | 835 |
! |
836 |
||
837 |
literalArrayEncoding |
|
1245 | 838 |
"encode myself as an array literal, from which a copy of the receiver |
839 |
can be reconstructed with #decodeAsLiteralArray." |
|
840 |
||
422 | 841 |
^ (self collect:[:el | el literalArrayEncoding]) asArray |
842 |
||
843 |
" |
|
844 |
(Array with:(Color red:50 green:50 blue:50) |
|
1245 | 845 |
with:(1 @ 2) |
422 | 846 |
) literalArrayEncoding |
847 |
" |
|
1245 | 848 |
|
849 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
260 | 850 |
! ! |
851 |
||
1 | 852 |
!SequenceableCollection methodsFor:'copying'! |
853 |
||
854 |
, aCollection |
|
855 |
"return a new collection formed from concatenating the receiver with |
|
44 | 856 |
the argument. The class of the new collection is determined by the |
857 |
receivers class, so mixing classes is possible, if the second collections |
|
858 |
elements can be stored into instances of the receivers class." |
|
1 | 859 |
|
860 |
|newCollection |
|
861 |
mySize "{ Class: SmallInteger }" |
|
862 |
newSize "{ Class: SmallInteger }" |
|
863 |
otherSize "{ Class: SmallInteger }" |
|
864 |
dstIndex "{ Class: SmallInteger }"| |
|
865 |
||
866 |
mySize := self size. |
|
867 |
otherSize := aCollection size. |
|
868 |
newSize := mySize + otherSize. |
|
202 | 869 |
|
252 | 870 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 871 |
|
872 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
873 |
dstIndex := mySize + 1. |
|
202 | 874 |
aCollection isSequenceable ifTrue:[ |
875 |
" |
|
876 |
yes, aCollection has indexed elements |
|
877 |
" |
|
159 | 878 |
newCollection replaceFrom:dstIndex to:newSize |
879 |
with:aCollection startingAt:1. |
|
880 |
^ newCollection |
|
1 | 881 |
] ifFalse:[ |
202 | 882 |
" |
883 |
no, enumerate aCollection |
|
884 |
" |
|
159 | 885 |
aCollection do:[:element | |
886 |
newCollection at:dstIndex put:element. |
|
887 |
dstIndex := dstIndex + 1 |
|
888 |
] |
|
1 | 889 |
]. |
890 |
^ newCollection |
|
44 | 891 |
|
95 | 892 |
" |
893 |
#($a $b $c) , #(1 2 3) |
|
894 |
#($a $b $c) , '123' |
|
895 |
'abc' , '123' |
|
896 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
897 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
898 |
'abc' asArray , #(1 2 3 4 5) |
|
899 |
" |
|
1 | 900 |
! |
901 |
||
602 | 902 |
copyFirst:count |
903 |
"return a new collection consisting of the receivers first count |
|
904 |
elements - this is just a rename of copyTo: - for compatibility." |
|
905 |
||
906 |
^ self copyFrom:1 to:count |
|
907 |
||
908 |
" |
|
909 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
910 |
'1234567890' copyFirst:4 |
|
911 |
" |
|
912 |
! |
|
913 |
||
914 |
copyFrom:start |
|
915 |
"return a new collection consisting of receivers elements |
|
916 |
from start to the end of the collection" |
|
917 |
||
918 |
^ self copyFrom:start to:(self size) |
|
919 |
||
920 |
" |
|
921 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
922 |
'1234567890' copyFrom:2 |
|
923 |
" |
|
924 |
! |
|
925 |
||
926 |
copyFrom:start to:stop |
|
927 |
"return a new collection consisting of receivers elements |
|
928 |
between start and stop" |
|
929 |
||
930 |
|newCollection newSize| |
|
931 |
||
932 |
newSize := stop - start + 1. |
|
933 |
newCollection := self copyEmptyAndGrow:newSize. |
|
934 |
newCollection replaceFrom:1 to:newSize with:self startingAt:start. |
|
935 |
^ newCollection |
|
936 |
||
937 |
" |
|
938 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
939 |
'1234567890' copyFrom:2 to:5 |
|
940 |
" |
|
941 |
! |
|
942 |
||
943 |
copyLast:count |
|
944 |
"return a new collection consisting of the receivers last count |
|
945 |
elements." |
|
946 |
||
947 |
|sz| |
|
948 |
||
949 |
sz := self size. |
|
950 |
^ self copyFrom:(sz - count + 1) to:sz |
|
951 |
||
952 |
" |
|
953 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
954 |
'1234567890' copyLast:4 |
|
955 |
" |
|
956 |
! |
|
957 |
||
958 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
|
959 |
"return a copy of the receiver, where the elements from startIndex to |
|
960 |
endIndex have been replaced by the elements of aCollection" |
|
961 |
||
962 |
|newColl sz replSize| |
|
963 |
||
964 |
replSize := aCollection size. |
|
965 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
966 |
newColl := self copyEmptyAndGrow:sz. |
|
967 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
968 |
newColl replaceFrom:startIndex with:aCollection. |
|
969 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
970 |
^ newColl |
|
971 |
||
972 |
" |
|
973 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 to:6 with:#(a b c d e f g h i j k) |
|
974 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
|
975 |
" |
|
976 |
! |
|
977 |
||
978 |
copyReplaceFrom:startIndex with:aCollection |
|
979 |
"return a copy of the receiver, where the elements from startIndex to |
|
980 |
the end have been replaced by the elements of aCollection" |
|
981 |
||
982 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
983 |
||
984 |
" |
|
985 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
|
986 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
987 |
" |
|
988 |
! |
|
989 |
||
990 |
copyThrough:element |
|
991 |
"return a new collection consisting of the receiver elements |
|
992 |
up-to (AND including) the first occurence of element." |
|
993 |
||
994 |
|idx| |
|
995 |
||
996 |
idx := self indexOf:element. |
|
997 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
998 |
^ self copyFrom:1 to:idx |
|
999 |
||
1000 |
" |
|
1001 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
1002 |
'1234567890' copyThrough:$5 |
|
1003 |
'1234567890' copyThrough:$a |
|
1004 |
'1234567890' copyThrough:$1 |
|
1005 |
" |
|
1006 |
! |
|
1007 |
||
1008 |
copyTo:stop |
|
1009 |
"return a new collection consisting of receivers elements |
|
1010 |
from 1 up to (including) index stop" |
|
1011 |
||
1012 |
^ self copyFrom:1 to:stop |
|
1013 |
||
1014 |
" |
|
1015 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
1016 |
'1234567890' copyTo:4 |
|
1017 |
" |
|
1018 |
! |
|
1019 |
||
1020 |
copyUpTo:element |
|
1021 |
"return a new collection consisting of the receiver elements |
|
1022 |
up-to (but excluding) the first occurence of element; |
|
1023 |
or to the end, if element is not included" |
|
1024 |
||
1025 |
|idx| |
|
1026 |
||
1027 |
idx := self indexOf:element. |
|
1028 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
1029 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
1030 |
^ self copyFrom:1 to:(idx-1) |
|
1031 |
||
1032 |
" |
|
1033 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
1034 |
'1234567890' copyUpTo:$5 |
|
1035 |
'1234567890' copyUpTo:$a |
|
1036 |
'1234567890' copyUpTo:$1 |
|
1037 |
'123456789' copyUpTo:$0 |
|
1038 |
" |
|
1039 |
! |
|
1040 |
||
1 | 1041 |
copyWith:newElement |
233 | 1042 |
"return a new collection containing the receivers elements |
1043 |
and the single new element, newElement. |
|
1044 |
This is different from concatentation, which expects another collection |
|
1045 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 1046 |
|
1047 |
|newCollection mySize newSize| |
|
1048 |
||
1049 |
mySize := self size. |
|
1050 |
newSize := mySize + 1. |
|
252 | 1051 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 1052 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
1053 |
newCollection at:newSize put:newElement. |
|
1054 |
^newCollection |
|
44 | 1055 |
|
95 | 1056 |
" |
1057 |
#(1 2 3 4 5) copyWith:$a |
|
1058 |
'abcdefg' copyWith:$h |
|
1059 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
1060 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
1061 |
" |
|
1 | 1062 |
! |
1063 |
||
61 | 1064 |
copyWithout:elementToSkip |
1065 |
"return a new collection consisting of a copy of the receiver, with |
|
1066 |
ALL elements equal to elementToSkip are left out. |
|
1067 |
No error is reported, if elementToSkip is not in the collection." |
|
1068 |
||
293 | 1069 |
|n "{ Class: SmallInteger }" |
1070 |
sz "{ Class: SmallInteger }" |
|
1071 |
srcIndex "{ Class: SmallInteger }" |
|
1072 |
dstIndex "{ Class: SmallInteger }" |
|
1073 |
skipIndex "{ Class: SmallInteger }" |
|
1074 |
copy l| |
|
61 | 1075 |
|
1076 |
"the code below may look like overkill, |
|
1077 |
however, for big collections its better to move data |
|
1078 |
around in big chunks" |
|
1079 |
||
1080 |
n := self occurrencesOf:elementToSkip. |
|
1081 |
n == 0 ifTrue:[^ self copy]. |
|
1082 |
||
1083 |
sz := self size. |
|
252 | 1084 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 1085 |
|
1086 |
srcIndex := 1. |
|
1087 |
dstIndex := 1. |
|
1088 |
||
1089 |
n timesRepeat:[ |
|
159 | 1090 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
1091 |
l := skipIndex - srcIndex. |
|
1092 |
l ~~ 0 ifTrue:[ |
|
1093 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
1094 |
with:self startingAt:srcIndex. |
|
1095 |
dstIndex := dstIndex + l |
|
1096 |
]. |
|
1097 |
srcIndex := skipIndex + 1 |
|
61 | 1098 |
]. |
1099 |
l := sz - srcIndex. |
|
1100 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
159 | 1101 |
with:self startingAt:srcIndex. |
61 | 1102 |
^ copy |
1103 |
||
95 | 1104 |
" |
1105 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
1106 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
1107 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
202 | 1108 |
'abcdefghi' copyWithout:$h |
1109 |
'abcdefg' copyWithout:$h |
|
95 | 1110 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
1111 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
1112 |
#(90 80 70 60 50) copyWithout:70 |
|
1113 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
1114 |
" |
|
61 | 1115 |
! |
1116 |
||
602 | 1117 |
copyWithoutFirst:elementToSkip |
1118 |
"return a new collection consisting of a copy of the receivers elements |
|
1119 |
without the first elementToSkip, if it was present. |
|
1120 |
No error is reported, if elementToSkip is not in the collection." |
|
1121 |
||
1122 |
|copy skipIndex sz| |
|
1123 |
||
1124 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
1125 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
1126 |
||
1127 |
sz := self size - 1. |
|
1128 |
copy := self copyEmptyAndGrow:sz. |
|
1129 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
1130 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
1131 |
^ copy |
|
1132 |
||
1133 |
" |
|
1134 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
1135 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
1136 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
1137 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
1138 |
" |
|
1139 |
! |
|
1140 |
||
61 | 1141 |
copyWithoutIndex:omitIndex |
1142 |
"return a new collection consisting of receivers elements |
|
1143 |
without the argument stored at omitIndex" |
|
1144 |
||
1145 |
|copy sz| |
|
1146 |
||
202 | 1147 |
sz := self size - 1. |
252 | 1148 |
copy := self copyEmptyAndGrow:sz. |
293 | 1149 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
1150 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 1151 |
^ copy |
1152 |
||
95 | 1153 |
" |
1154 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
1155 |
'abcdefghijkl' copyWithoutIndex:5 |
|
1156 |
" |
|
1 | 1157 |
! |
1158 |
||
202 | 1159 |
copyWithoutLast:count |
1160 |
"return a new collection consisting of the receivers elements |
|
1161 |
except the last count elements." |
|
1162 |
||
1163 |
|sz| |
|
1164 |
||
1165 |
sz := self size. |
|
1166 |
^ self copyTo:(sz - count) |
|
1167 |
||
1168 |
" |
|
1169 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
|
1170 |
'1234567890' copyWithoutLast:4 |
|
1171 |
" |
|
602 | 1172 |
! ! |
1173 |
||
1174 |
!SequenceableCollection methodsFor:'enumerating'! |
|
1175 |
||
1176 |
collect:aBlock |
|
1177 |
"evaluate the argument, aBlock for every element in the collection |
|
1178 |
and return a collection of the results" |
|
1179 |
||
1180 |
|newCollection |
|
1181 |
sz "{ Class:SmallInteger }"| |
|
1182 |
||
1183 |
sz := self size. |
|
1184 |
newCollection := self copyEmptyAndGrow:sz. |
|
1185 |
1 to:sz do:[:index | |
|
1186 |
newCollection at:index put:(aBlock value:(self at:index)). |
|
1187 |
]. |
|
1188 |
^ newCollection |
|
1189 |
||
1190 |
" |
|
1191 |
#(one two three four five six) collect:[:element | element asUppercase] |
|
1192 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
1193 |
(1 to:9) collect:[:element | element * element] |
|
1194 |
" |
|
202 | 1195 |
! |
1196 |
||
602 | 1197 |
do:aBlock |
1198 |
"evaluate the argument, aBlock for every element in the collection." |
|
1199 |
||
1200 |
|stop "{ Class:SmallInteger }"| |
|
1201 |
||
1202 |
stop := self size. |
|
1203 |
1 to:stop do:[:index | |
|
1204 |
aBlock value:(self at:index). |
|
1205 |
] |
|
1206 |
" |
|
1207 |
#(one two three four five six) do:[:element | Transcript showCr:element] |
|
1208 |
" |
|
1209 |
! |
|
1210 |
||
1211 |
from:startIndex do:aBlock |
|
1212 |
"evaluate the argument, aBlock for the elements starting with the |
|
1213 |
element at startIndex to the end." |
|
1214 |
||
1215 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 1216 |
|
95 | 1217 |
" |
602 | 1218 |
#(one two three four five six) from:3 do:[:element | Transcript showCr:element] |
1219 |
" |
|
1220 |
! |
|
1221 |
||
1222 |
from:start to:stop collect:aBlock |
|
1223 |
"evaluate the argument, aBlock for the elements indexed by start |
|
1224 |
to stop in the collection and return a collection of the results" |
|
1225 |
||
1226 |
|newCollection sz |
|
1227 |
idx "{ Class:SmallInteger }"| |
|
1228 |
||
1229 |
sz := stop - start + 1. |
|
1230 |
newCollection := self copyEmptyAndGrow:sz. |
|
1231 |
idx := 1. |
|
1232 |
start to:stop do:[:index | |
|
1233 |
newCollection at:idx put:(aBlock value:(self at:index)). |
|
1234 |
idx := idx + 1 |
|
1235 |
]. |
|
1236 |
^ newCollection |
|
1237 |
||
1238 |
" |
|
1239 |
#(one two three four five six) from:2 to:4 collect:[:element | element asUppercase] |
|
1240 |
" |
|
1241 |
! |
|
1242 |
||
1243 |
from:index1 to:index2 do:aBlock |
|
1244 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1245 |
index2 in the collection" |
|
1246 |
||
1247 |
|start "{ Class:SmallInteger }" |
|
1248 |
stop "{ Class:SmallInteger }" | |
|
1249 |
||
1250 |
start := index1. |
|
1251 |
stop := index2. |
|
1252 |
start to:stop do:[:index | |
|
1253 |
aBlock value:(self at:index). |
|
1254 |
] |
|
1255 |
||
1256 |
" |
|
1257 |
#(one two three four five six) from:3 to:5 do:[:element | Transcript showCr:element] |
|
1258 |
" |
|
1259 |
! |
|
1260 |
||
1261 |
from:index1 to:index2 reverseDo:aBlock |
|
1262 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1263 |
index2 in the collection. Step in reverse order" |
|
1264 |
||
1265 |
|start "{ Class:SmallInteger }" |
|
1266 |
stop "{ Class:SmallInteger }" | |
|
1267 |
||
1268 |
start := index1. |
|
1269 |
stop := index2. |
|
1270 |
stop to:start by:-1 do:[:index | |
|
1271 |
aBlock value:(self at:index). |
|
1272 |
] |
|
1273 |
||
1274 |
" |
|
1275 |
#(one two three four five six) from:3 to:5 reverseDo:[:element | Transcript showCr:element] |
|
95 | 1276 |
" |
61 | 1277 |
! |
1278 |
||
602 | 1279 |
keysAndValuesDo:aTwoArgBlock |
1280 |
"evaluate the argument, aBlock for every element in the collection, |
|
1281 |
passing both index and element as arguments." |
|
1282 |
||
1283 |
|stop "{ Class:SmallInteger }"| |
|
1284 |
||
1285 |
stop := self size. |
|
1286 |
1 to:stop do:[:index | |
|
1287 |
aTwoArgBlock value:index value:(self at:index). |
|
1288 |
] |
|
1289 |
" |
|
1290 |
#(one two three four five six) keysAndValuesDo:[:key :element | Transcript show:key; space; showCr:element] |
|
1291 |
" |
|
1292 |
! |
|
1293 |
||
1359 | 1294 |
keysAndValuesReverseDo:aTwoArgBlock |
1295 |
"evaluate the argument, aBlock in reverse order, for every element |
|
1296 |
in the collection, passing both index and element as arguments." |
|
1297 |
||
1298 |
|stop "{ Class:SmallInteger }"| |
|
1299 |
||
1300 |
stop := self size. |
|
1301 |
stop to:1 by:-1 do:[:index | |
|
1302 |
aTwoArgBlock value:index value:(self at:index). |
|
1303 |
] |
|
1304 |
||
1305 |
" |
|
1306 |
#(one two three four five six) |
|
1307 |
keysAndValuesReverseDo:[:key :element | Transcript show:key; space; showCr:element] |
|
1308 |
" |
|
1309 |
||
1310 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
1311 |
! |
|
1312 |
||
602 | 1313 |
nonNilElementsDo:aBlock |
1314 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
1315 |
||
1316 |
|stop "{ Class:SmallInteger }" |
|
1317 |
element| |
|
1318 |
||
1319 |
stop := self size. |
|
1320 |
1 to:stop do:[:index | |
|
1321 |
(element := self at:index) notNil ifTrue:[ |
|
1322 |
aBlock value:element. |
|
1323 |
] |
|
1324 |
] |
|
1325 |
" |
|
1326 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCr:element] |
|
1327 |
" |
|
1328 |
! |
|
1329 |
||
1330 |
pairWiseCollect:aBlock |
|
1331 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1332 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1333 |
An error will be reported, if the number of elements in the receiver |
|
1334 |
is not a multiple of 2. |
|
1335 |
Collect the results and return a new collection containing those." |
|
1336 |
||
1337 |
|stop newCollection dstIdx "{ Class:SmallInteger }"| |
|
1338 |
||
1339 |
stop := self size. |
|
1340 |
newCollection := self copyEmptyAndGrow:stop // 2. |
|
1341 |
dstIdx := 1. |
|
1342 |
1 to:stop by:2 do:[:index | |
|
1343 |
newCollection at:dstIdx put:(aBlock value:(self at:index) value:(self at:index+1)). |
|
1344 |
dstIdx := dstIdx + 1 |
|
1345 |
]. |
|
1346 |
^ newCollection |
|
44 | 1347 |
|
95 | 1348 |
" |
602 | 1349 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
1350 |
pairWiseCollect:[:num :sym | sym->num] |
|
1351 |
||
1352 |
||
1353 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1354 |
pairWiseCollect:[:x :y | x@y] |
|
1355 |
" |
|
1356 |
! |
|
1357 |
||
1358 |
pairWiseDo:aBlock |
|
1359 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1360 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1361 |
An error will be reported, if the number of elements in the receiver |
|
1362 |
is not a multiple of 2." |
|
1363 |
||
1364 |
|stop "{ Class:SmallInteger }"| |
|
1365 |
||
1366 |
stop := self size. |
|
1367 |
1 to:stop by:2 do:[:index | |
|
1368 |
aBlock value:(self at:index) value:(self at:index+1). |
|
1369 |
] |
|
1370 |
" |
|
1371 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
1372 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCr:sym] |
|
1373 |
||
1374 |
||
1375 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1376 |
pairWiseDo:[:x :y | Transcript showCr:x@y] |
|
95 | 1377 |
" |
61 | 1378 |
! |
1379 |
||
602 | 1380 |
reverseDo:aBlock |
1381 |
"evaluate the argument, aBlock for every element in the collection |
|
1382 |
in reverse order" |
|
1383 |
||
1384 |
|sz "{ Class:SmallInteger }"| |
|
1385 |
||
1386 |
sz := self size. |
|
1387 |
sz to:1 by:-1 do:[:index | |
|
1388 |
aBlock value:(self at:index). |
|
1389 |
] |
|
1390 |
||
1391 |
" |
|
1392 |
#(one two three four five six) reverseDo:[:element | Transcript showCr:element] |
|
1393 |
" |
|
1394 |
! |
|
1395 |
||
1396 |
select:aBlock |
|
1397 |
"evaluate the argument, aBlock for every element in the collection |
|
1398 |
and return a collection of all elements for which the block return |
|
1399 |
true" |
|
1400 |
||
1401 |
|element newColl species needCopy |
|
1402 |
sz "{ Class:SmallInteger }"| |
|
1403 |
||
1404 |
sz := self size. |
|
1405 |
species := self species. |
|
1406 |
species growIsCheap ifFalse:[ |
|
1407 |
newColl := OrderedCollection new:sz. |
|
1408 |
needCopy := true |
|
1409 |
] ifTrue:[ |
|
1410 |
newColl := self copyEmpty:sz. |
|
1411 |
needCopy := false |
|
1412 |
]. |
|
1413 |
1 to:sz do:[:index | |
|
1414 |
element := self at:index. |
|
1415 |
(aBlock value:element) ifTrue:[ |
|
1416 |
newColl add:element |
|
1417 |
]. |
|
1418 |
]. |
|
1419 |
needCopy ifTrue:[ |
|
1420 |
newColl := (species withAll:newColl) postCopyFrom:self |
|
1421 |
]. |
|
61 | 1422 |
^ newColl |
1423 |
||
95 | 1424 |
" |
602 | 1425 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
1426 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
1427 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
1428 |
(1 to:9) select:[:element | element odd] |
|
1429 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 1430 |
" |
186 | 1431 |
! |
1432 |
||
602 | 1433 |
with:aSequenceableCollection do:aTwoArgBlock |
1434 |
"evaluate the argument, aBlock for successive elements from |
|
1435 |
each the receiver and the argument, aSequenceableCollection. |
|
1436 |
The second argument, aBlock must be a two-argument block. |
|
1437 |
The collection argument must implement access via a numeric key." |
|
1438 |
||
1439 |
|stop "{ Class: SmallInteger }" | |
|
1440 |
||
1441 |
stop := self size. |
|
1442 |
1 to:stop do:[:index | |
|
1443 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
|
1444 |
] |
|
186 | 1445 |
" |
602 | 1446 |
#(one two three four five six) with:(1 to:10) do:[:el1 :el2 | Transcript show:el1; space; showCr:el2] |
186 | 1447 |
" |
1 | 1448 |
! ! |
1449 |
||
1450 |
!SequenceableCollection methodsFor:'filling and replacing'! |
|
1451 |
||
602 | 1452 |
atAllPut:anObject |
1453 |
"replace all elements of the collection by the argument, anObject. |
|
1454 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1455 |
therefore the change may affect all others referencing the receiver." |
|
1456 |
||
1457 |
self from:1 to:(self size) put:anObject |
|
1458 |
||
1459 |
" |
|
1460 |
(Array new:10) atAllPut:1 |
|
1461 |
(String new:10) atAllPut:$a |
|
1462 |
" |
|
1463 |
! |
|
1464 |
||
1 | 1465 |
from:index1 to:index2 put:anObject |
1466 |
"replace the elements from index1 to index2 of the collection |
|
44 | 1467 |
by the argument, anObject. |
1468 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1469 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1470 |
|
1471 |
|index "{ Class: SmallInteger }" |
|
1472 |
end "{ Class: SmallInteger }"| |
|
1473 |
||
1474 |
index := index1. |
|
1475 |
end := index2. |
|
1476 |
[index <= end] whileTrue:[ |
|
159 | 1477 |
self at:index put:anObject. |
1478 |
index := index + 1 |
|
1 | 1479 |
] |
44 | 1480 |
|
95 | 1481 |
" |
1482 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
|
1483 |
'abcdefghijkl' from:3 to:6 put:$X |
|
1484 |
" |
|
1 | 1485 |
! |
1486 |
||
1487 |
replaceAll:oldObject by:newObject |
|
44 | 1488 |
"replace all oldObjects by newObject in the receiver. |
1489 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1490 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1491 |
|
362 | 1492 |
^ self replaceAll:oldObject by:newObject from:1 to:self size |
1493 |
||
1494 |
" |
|
1495 |
'123123abc123' replaceAll:$1 by:$* |
|
1496 |
" |
|
1497 |
! |
|
293 | 1498 |
|
362 | 1499 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
1500 |
"replace all oldObjects found between startIndex and endIndex, |
|
1501 |
by newObject in the receiver. |
|
1502 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1503 |
therefore the change may affect all others referencing the receiver." |
|
1504 |
||
1505 |
|start "{ Class: SmallInteger }" |
|
1506 |
stop "{ Class: SmallInteger }"| |
|
1507 |
||
1508 |
start := startIndex. |
|
1509 |
stop := stopIndex. |
|
1510 |
startIndex to:stopIndex do:[:index | |
|
159 | 1511 |
(self at:index) = oldObject ifTrue:[ |
1512 |
self at:index put:newObject |
|
1513 |
] |
|
1 | 1514 |
] |
44 | 1515 |
|
95 | 1516 |
" |
362 | 1517 |
'123123abc123' replaceAll:$1 by:$* |
1518 |
'123123abc123' replaceAll:$1 by:$* from:1 to:6 |
|
95 | 1519 |
" |
1520 |
! |
|
1521 |
||
602 | 1522 |
replaceAll:oldObject with:newObject |
1523 |
"ST-80 compatibility: an alias for #replaceAll:by:" |
|
1524 |
||
1525 |
^ self replaceAll:oldObject by:newObject |
|
1526 |
! |
|
1527 |
||
1528 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
|
1529 |
"ST-80 compatibility: an alias for #replaceAll:by:from:to:" |
|
1530 |
||
1531 |
^ self replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
1532 |
! |
|
1533 |
||
95 | 1534 |
replaceAny:aCollection by:newObject |
1535 |
"replace all elements, which are in aCollection by newObject in the receiver. |
|
1536 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1537 |
therefore the change may affect all others referencing the receiver." |
|
1538 |
||
362 | 1539 |
^ self replaceAny:aCollection by:newObject from:1 to:self size |
1540 |
||
1541 |
" |
|
1542 |
'123123abc123' replaceAny:#($1 $2) by:$* |
|
1543 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) by:'*' |
|
1544 |
" |
|
1545 |
! |
|
293 | 1546 |
|
362 | 1547 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
1548 |
"replace all elements within a range, |
|
1549 |
which are in contained in aCollection by newObject. |
|
1550 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1551 |
therefore the change may affect all others referencing the receiver." |
|
1552 |
||
1553 |
|start "{ Class: SmallInteger }" |
|
1554 |
stop "{ Class: SmallInteger }"| |
|
1555 |
||
1556 |
start := startIndex. |
|
1557 |
stop := stopIndex. |
|
1558 |
startIndex to:stopIndex do:[:index | |
|
159 | 1559 |
(aCollection includes:(self at:index)) ifTrue:[ |
1560 |
self at:index put:newObject |
|
1561 |
] |
|
95 | 1562 |
] |
1563 |
||
1564 |
" |
|
362 | 1565 |
'123123abc123' replaceAny:#($1 $2) by:$* from:1 to:6 |
95 | 1566 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) by:'*' |
1567 |
" |
|
1 | 1568 |
! |
1569 |
||
1570 |
replaceFrom:start to:stop with:replacementCollection |
|
44 | 1571 |
"replace elements in the receiver between index start and stop, |
1572 |
with elements taken from replacementCollection starting at 1. |
|
1573 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1574 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1575 |
|
1576 |
^ self replaceFrom:start |
|
159 | 1577 |
to:stop |
1578 |
with:replacementCollection |
|
1579 |
startingAt:1 |
|
61 | 1580 |
|
95 | 1581 |
" |
1582 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
1583 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
1584 |
" |
|
1 | 1585 |
! |
1586 |
||
1587 |
replaceFrom:start to:stop with:replacementCollection startingAt:repStart |
|
44 | 1588 |
"replace elements in the receiver between index start and stop, |
1589 |
with elements taken from replacementCollection starting at repStart. |
|
1590 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1591 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1592 |
|
1593 |
|srcIndex "{ Class: SmallInteger }" |
|
1594 |
dstIndex "{ Class: SmallInteger }" |
|
1595 |
end "{ Class: SmallInteger }" | |
|
1596 |
||
293 | 1597 |
(repStart < start) ifTrue:[ |
1598 |
" must do reverse copy " |
|
1599 |
srcIndex := repStart + (stop - start). |
|
1600 |
dstIndex := stop. |
|
1601 |
end := start. |
|
1602 |
[dstIndex >= end] whileTrue:[ |
|
1603 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
1604 |
srcIndex := srcIndex - 1. |
|
1605 |
dstIndex := dstIndex - 1 |
|
1606 |
]. |
|
1607 |
^ self |
|
1 | 1608 |
]. |
1609 |
||
1610 |
srcIndex := repStart. |
|
1611 |
dstIndex := start. |
|
1612 |
end := stop. |
|
1613 |
[dstIndex <= end] whileTrue:[ |
|
159 | 1614 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
1615 |
srcIndex := srcIndex + 1. |
|
1616 |
dstIndex := dstIndex + 1 |
|
1 | 1617 |
] |
61 | 1618 |
|
95 | 1619 |
" |
282 | 1620 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
1621 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1622 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1623 |
||
1624 |
|c| |
|
1625 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1626 |
c replaceFrom:2 to:3 with:c startingAt:4 |
|
1627 |
||
1628 |
|c| |
|
1629 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1630 |
c replaceFrom:4 to:5 with:c startingAt:2 |
|
95 | 1631 |
" |
61 | 1632 |
! |
1633 |
||
602 | 1634 |
replaceFrom:start with:replacementCollection |
1635 |
"replace elements in the receiver starting at start, |
|
1636 |
with elements taken from replacementCollection starting at 1 |
|
1637 |
to the end of replacementCollection. |
|
1638 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1639 |
therefore the change may affect all others referencing the receiver." |
|
1640 |
||
1641 |
^ self replaceFrom:start |
|
1642 |
to:(start + replacementCollection size - 1) |
|
1643 |
with:replacementCollection |
|
1644 |
startingAt:1 |
|
1645 |
||
1646 |
" |
|
1647 |
'1234567890' replaceFrom:5 with:'abc' |
|
1648 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
1649 |
" |
|
1650 |
! |
|
1651 |
||
1652 |
replaceFrom:start with:replacementCollection startingAt:offset |
|
1653 |
"replace elements in the receiver starting at start, |
|
1654 |
with elements taken from replacementCollection starting at offset |
|
1655 |
to the end of replacementCollection. |
|
1656 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1657 |
therefore the change may affect all others referencing the receiver." |
|
1658 |
||
1659 |
^ self replaceFrom:start |
|
1660 |
to:(start + replacementCollection size - offset) |
|
1661 |
with:replacementCollection |
|
1662 |
startingAt:offset |
|
1663 |
||
1664 |
" |
|
1665 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
|
1666 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
|
1667 |
" |
|
1668 |
! |
|
1669 |
||
61 | 1670 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
1671 |
"replace elements in destColl with elements from the receiver. |
|
1672 |
Notice: This operation modifies the destination collection, NOT a copy; |
|
1673 |
therefore the change may affect all others referencing this object." |
|
1674 |
||
1675 |
destColl replaceFrom:destStartIndex to:destEndIndex with:self startingAt:sourceStart |
|
1676 |
||
1677 |
" |
|
1678 |
|s| |
|
1679 |
s := 'abcdefghijklmnop'. |
|
1680 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
1681 |
s'123defghijklmnop' |
|
1682 |
" |
|
1 | 1683 |
! ! |
1684 |
||
602 | 1685 |
!SequenceableCollection methodsFor:'queries'! |
1686 |
||
1687 |
firstIndex |
|
1688 |
"return the first elements index" |
|
1689 |
||
1690 |
^ 1 |
|
1691 |
! |
|
1692 |
||
1693 |
isSequenceable |
|
1694 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
1695 |
||
1696 |
^ true |
|
1 | 1697 |
! |
1698 |
||
602 | 1699 |
isSequenceableCollection |
1700 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
1701 |
This method is a historic leftover and will be removed." |
|
1702 |
||
1703 |
^ true |
|
1 | 1704 |
! |
1705 |
||
602 | 1706 |
keys |
1707 |
"return a collection with all keys in the Smalltalk dictionary" |
|
1708 |
||
1709 |
|sz| |
|
1710 |
||
1 | 1711 |
sz := self size. |
602 | 1712 |
sz == 0 ifTrue:[ |
1713 |
^ #() |
|
61 | 1714 |
]. |
602 | 1715 |
^ 1 to:sz |
1 | 1716 |
! |
1717 |
||
602 | 1718 |
lastIndex |
1719 |
"return the last elements index" |
|
1720 |
||
1721 |
^ self size |
|
61 | 1722 |
! |
1723 |
||
602 | 1724 |
size |
1725 |
"return the number of elements in the collection. |
|
1726 |
concrete implementations must define this" |
|
1727 |
||
1728 |
^ self subclassResponsibility |
|
1 | 1729 |
! ! |
1730 |
||
1731 |
!SequenceableCollection methodsFor:'searching'! |
|
1732 |
||
1733 |
detect:aBlock ifNone:exceptionBlock |
|
1734 |
"find the first element, for which evaluation of the argument, aBlock |
|
1735 |
return true; if none does so, return the evaluation of exceptionBlock |
|
1736 |
||
1737 |
reimplemented here for speed" |
|
1738 |
||
32 | 1739 |
|stop "{ Class: SmallInteger }" |
1 | 1740 |
element| |
1741 |
||
32 | 1742 |
stop := self size. |
1743 |
1 to:stop do:[:index | |
|
159 | 1744 |
element := self at:index. |
1745 |
(aBlock value:element) ifTrue:[ |
|
1746 |
^ element |
|
1747 |
]. |
|
1 | 1748 |
]. |
1749 |
^ exceptionBlock value |
|
159 | 1750 |
|
1751 |
" |
|
1752 |
#(11 12 13 14) detect:[:n | n odd] ifNone:['sorry'] |
|
1753 |
#(12 14 16 18) detect:[:n | n odd] ifNone:['sorry'] |
|
1754 |
" |
|
1 | 1755 |
! |
1756 |
||
602 | 1757 |
findFirst:aBlock |
1758 |
"find the first element, for which evaluation of the argument, aBlock |
|
1759 |
returns true; return its index or 0 if none detected." |
|
1760 |
||
1761 |
|stop "{ Class: SmallInteger }" | |
|
1762 |
||
32 | 1763 |
stop := self size. |
602 | 1764 |
1 to:stop do:[:index | |
1765 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
1 | 1766 |
]. |
1767 |
^ 0 |
|
159 | 1768 |
|
95 | 1769 |
" |
602 | 1770 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
1771 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
1772 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
1773 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
95 | 1774 |
" |
1 | 1775 |
! |
1776 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1777 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1778 |
"find the first element, for which evaluation of the argument, aBlock |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1779 |
returns true; return its index or 0 if none detected." |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1780 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1781 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1782 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1783 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1784 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1785 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1786 |
start to:stop do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1787 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1788 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1789 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1790 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1791 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1792 |
#(1 4 3 4 3 6) findFirst:[:x | (x > 3)] startingAt:4 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1793 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1794 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1795 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1796 |
|
602 | 1797 |
findLast:aBlock |
1798 |
"find the last element, for which evaluation of the argument, aBlock |
|
1799 |
returns true; return its index or 0 if none detected." |
|
1800 |
||
1801 |
|start "{ Class: SmallInteger }"| |
|
1802 |
||
1803 |
start := self size. |
|
1804 |
start to:1 by:-1 do:[:index | |
|
1805 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
252 | 1806 |
]. |
1807 |
^ 0 |
|
1808 |
||
1809 |
" |
|
602 | 1810 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
1811 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 1812 |
" |
1813 |
! |
|
1814 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1815 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1816 |
"find the last element, for which evaluation of the argument, aBlock |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1817 |
returns true; return its index or 0 if none detected." |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1818 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1819 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1820 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1821 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1822 |
start to:1 by:-1 do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1823 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1824 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1825 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1826 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1827 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1828 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1829 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1830 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1831 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
1832 |
|
1 | 1833 |
identityIndexOf:anElement |
1834 |
"search the collection for anElement using identity compare (i.e. ==); |
|
1835 |
if found, return the index otherwise return 0." |
|
1836 |
||
32 | 1837 |
^ self identityIndexOf:anElement startingAt:1 |
44 | 1838 |
|
95 | 1839 |
" |
159 | 1840 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
1841 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 |
|
1842 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
360 | 1843 |
|
1844 |
be careful: |
|
1845 |
||
1846 |
#(10 20 30 40.0 50 60 70) indexOf:40.0 |
|
1847 |
#(10 20 30 40.0 50 60 70) identityIndexOf:40.0 |
|
1848 |
||
95 | 1849 |
" |
1 | 1850 |
! |
1851 |
||
1852 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
|
1853 |
"search the collection for anElement using identity compare (i.e. ==); |
|
1854 |
if found, return the index otherwise return the value of the |
|
1855 |
exceptionBlock." |
|
1856 |
||
1857 |
|index| |
|
1858 |
||
32 | 1859 |
index := self identityIndexOf:anElement startingAt:1. |
1 | 1860 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
1861 |
^ index |
|
159 | 1862 |
|
1863 |
" |
|
1864 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
1865 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
1866 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
1867 |
#(10 20 30 40 50 60 70) indexOf:40.0 ifAbsent:['none'] |
|
1868 |
" |
|
1 | 1869 |
! |
1870 |
||
1871 |
identityIndexOf:anElement startingAt:start |
|
159 | 1872 |
"search the collection for anElement, starting search at index start |
1 | 1873 |
using identity compare (i.e. ==); |
1874 |
if found, return the index otherwise return 0." |
|
1875 |
||
32 | 1876 |
|startIndex "{ Class: SmallInteger }" |
1877 |
stop "{ Class: SmallInteger }" | |
|
1 | 1878 |
|
32 | 1879 |
startIndex := start. |
1880 |
stop := self size. |
|
1881 |
startIndex to:stop do:[:index | |
|
159 | 1882 |
anElement == (self at:index) ifTrue:[^ index]. |
1 | 1883 |
]. |
1884 |
^ 0 |
|
44 | 1885 |
|
95 | 1886 |
" |
159 | 1887 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40 startingAt:5 |
1888 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
|
1889 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
95 | 1890 |
" |
1 | 1891 |
! |
1892 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1893 |
identityIndexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1894 |
"search the collection for anElement, starting the search at index start; |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1895 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1896 |
If found (within the range), return the index, otherwise return 0. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1897 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1898 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1899 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1900 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1901 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1902 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1903 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1904 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1905 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1906 |
anElement == (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1907 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1908 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1909 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1910 |
"Created: 12.4.1996 / 18:23:07 / cg" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1911 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
1912 |
|
1 | 1913 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
159 | 1914 |
"search the collection for anElement, starting search at index start; |
1 | 1915 |
if found, return the index otherwise return the value of the |
1916 |
exceptionBlock. |
|
1917 |
This one searches for identical objects (i.e. ==)." |
|
1918 |
||
1919 |
|index| |
|
1920 |
||
1921 |
index := self identityIndexOf:anElement startingAt:start. |
|
1922 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
1923 |
^ index |
|
159 | 1924 |
|
1925 |
" |
|
1926 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
1927 |
#(10 20 30 40 10) identityIndexOf:35 startingAt:3 ifAbsent:['none'] |
|
1928 |
" |
|
1929 |
! |
|
1930 |
||
602 | 1931 |
includes:anElement |
1932 |
"return true if the collection contains anElement; false otherwise. |
|
1933 |
Comparison is done using equality compare (i.e. =). |
|
1934 |
See #includesIdentical: if identity is asked for." |
|
1935 |
||
1936 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
1937 |
^ true |
|
1938 |
||
1939 |
" |
|
1940 |
#(10 20 30 40 50 60 70) includes:99 |
|
1941 |
#(10 20 30 40 50 60 70) includes:40 |
|
1942 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
1943 |
" |
|
1944 |
! |
|
1945 |
||
1946 |
includesIdentical:anElement |
|
1947 |
"return true if the collection contains anElement; false otherwise. |
|
1948 |
Comparison is done using identity compare (i.e. ==). |
|
1949 |
See #includes: if equality is asked for." |
|
1950 |
||
1951 |
((self identityIndexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
1952 |
^ true |
|
1953 |
||
1954 |
" |
|
1955 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
1956 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
1957 |
#(10 20 30 40 50 60 70) includes:40 |
|
1958 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
1959 |
||
1960 |
be careful: |
|
1961 |
||
1962 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
|
1963 |
#(10 20 30 40.0 50 60 70) includesIdentical:40.0 |
|
1964 |
" |
|
1965 |
! |
|
1966 |
||
1967 |
indexOf:anElement |
|
1968 |
"search the collection for anElement; |
|
1969 |
if found, return the index otherwise return 0. |
|
1970 |
The comparison is done using = |
|
1971 |
(i.e. equality test - not identity test)." |
|
1972 |
||
1973 |
^ self indexOf:anElement startingAt:1 |
|
1974 |
||
1975 |
" |
|
1976 |
#(10 20 30 40 50 60 70) indexOf:40 |
|
1977 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
1978 |
" |
|
1979 |
! |
|
1980 |
||
1981 |
indexOf:anElement ifAbsent:exceptionBlock |
|
1982 |
"search the collection for anElement; |
|
1983 |
if found, return the index otherwise return the value of the |
|
1984 |
exceptionBlock. |
|
1985 |
The comparison is done using = |
|
1986 |
(i.e. equality test - not identity test)." |
|
1987 |
||
1988 |
|index| |
|
1989 |
||
1990 |
index := self indexOf:anElement startingAt:1. |
|
1991 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
1992 |
^ index |
|
1993 |
||
1994 |
" |
|
1995 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
1996 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
1997 |
#(10 20 30 40 10 20 30 40) indexOf:35 ifAbsent:['none'] |
|
1998 |
" |
|
1999 |
! |
|
2000 |
||
2001 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
|
2002 |
"search for the first occurence of elementToFind starting at start, |
|
2003 |
stopping the search at stop. If found, replace the element by replacement |
|
2004 |
and return the index. |
|
2005 |
If not found, return 0." |
|
2006 |
||
2007 |
|idx| |
|
2008 |
||
2009 |
idx := self indexOf:elementToFind startingAt:start. |
|
2010 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
|
2011 |
self at:idx put:replacement. |
|
2012 |
^ idx |
|
2013 |
]. |
|
2014 |
^ 0 |
|
2015 |
||
2016 |
" |
|
2017 |
|a| |
|
2018 |
||
2019 |
a := #(10 20 30 40 50 60 70). |
|
2020 |
(a indexOf:30 replaceWith:nil startingAt:1 stoppingAt:7) printNL. |
|
2021 |
a printNL. |
|
2022 |
" |
|
2023 |
! |
|
2024 |
||
2025 |
indexOf:anElement startingAt:start |
|
2026 |
"search the collection for anElement, starting the search at index start; |
|
2027 |
if found, return the index otherwise return 0. |
|
2028 |
The comparison is done using = |
|
2029 |
(i.e. equality test - not identity test)." |
|
2030 |
||
2031 |
|startIndex "{ Class: SmallInteger }" |
|
2032 |
stop "{ Class: SmallInteger }" | |
|
2033 |
||
2034 |
startIndex := start. |
|
2035 |
stop := self size. |
|
2036 |
startIndex to:stop do:[:index | |
|
2037 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2038 |
]. |
|
2039 |
^ 0 |
|
2040 |
||
2041 |
" |
|
2042 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
2043 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
2044 |
" |
|
2045 |
! |
|
2046 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2047 |
indexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2048 |
"search the collection for anElement, starting the search at index start; |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2049 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2050 |
If found (within the range), return the index, otherwise return 0. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2051 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2052 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2053 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2054 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2055 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2056 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2057 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2058 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2059 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2060 |
anElement = (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2061 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2062 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2063 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2064 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2065 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2066 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2067 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2068 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2069 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2070 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2071 |
"Created: 12.4.1996 / 17:37:13 / cg" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2072 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2073 |
|
602 | 2074 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
2075 |
"search the collection for anElement starting the search at index start; |
|
2076 |
if found, return the index otherwise return the value of the |
|
2077 |
exceptionBlock. |
|
2078 |
The comparison is done using = |
|
2079 |
(i.e. equality test - not identity test)." |
|
2080 |
||
2081 |
|index| |
|
2082 |
||
2083 |
index := self indexOf:anElement startingAt:start. |
|
2084 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2085 |
^ index |
|
2086 |
||
2087 |
" |
|
2088 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
2089 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
2090 |
#(10 20 30 40 10 20 30 40) indexOf:35 startingAt:5 ifAbsent:['none'] |
|
2091 |
" |
|
2092 |
! |
|
2093 |
||
159 | 2094 |
indexOfAny:aCollection |
2095 |
"search the collection for an element in aCollection. |
|
2096 |
if found, return the index otherwise return 0. |
|
2097 |
The comparison is done using = |
|
2098 |
(i.e. equality test - not identity test). |
|
2099 |
||
2100 |
Notice, that for big collections, the runtime of this search |
|
2101 |
grows proportional to size(receiver) * size(aCollection). |
|
2102 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
2103 |
||
2104 |
^ self indexOfAny:aCollection startingAt:1 |
|
2105 |
||
2106 |
" |
|
2107 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
2108 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
2109 |
" |
|
2110 |
! |
|
2111 |
||
2112 |
indexOfAny:aCollection startingAt:start |
|
2113 |
"search the collection for an element in aCollection, |
|
2114 |
starting the search at index start; |
|
2115 |
if found, return the index otherwise return 0. |
|
2116 |
The comparison is done using = |
|
2117 |
(i.e. equality test - not identity test). |
|
2118 |
||
2119 |
Notice, that for big collections, the runtime of this search |
|
2120 |
grows proportional to size(receiver) * size(aCollection). |
|
2121 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
2122 |
||
2123 |
||
2124 |
|startIndex "{ Class: SmallInteger }" |
|
2125 |
stop "{ Class: SmallInteger }" | |
|
2126 |
||
2127 |
startIndex := start. |
|
2128 |
stop := self size. |
|
2129 |
startIndex to:stop do:[:index | |
|
2130 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
|
2131 |
]. |
|
2132 |
^ 0 |
|
2133 |
" |
|
2134 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
|
2135 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
2136 |
" |
|
1 | 2137 |
! |
2138 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2139 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2140 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2141 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2142 |
if found, return the index. If not, return the value returned by exceptionBlock. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2143 |
The comparison is done using = |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2144 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2145 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2146 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2147 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2148 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2149 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2150 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2151 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2152 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2153 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2154 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2155 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2156 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2157 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2158 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2159 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2160 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(99 88 77) startingAt:5 ifAbsent:['oops'] |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2161 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2162 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2163 |
|
368 | 2164 |
indexOfSubCollection:aCollection |
2165 |
"find a subcollection. |
|
2166 |
If found, return the index; if not found, return 0." |
|
2167 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2168 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 2169 |
|
2170 |
" |
|
2171 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
|
2172 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
2173 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
2174 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
2175 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
2176 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
2177 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
2178 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
2179 |
" |
|
2180 |
! |
|
2181 |
||
602 | 2182 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
2183 |
"find a subcollection. If found, return the index; |
|
2184 |
if not found, return the result of evaluating exceptionBlock." |
|
2185 |
||
2186 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
2187 |
! |
|
2188 |
||
368 | 2189 |
indexOfSubCollection:aCollection startingAt:startIndex |
2190 |
"find a subcollection starting at index. |
|
2191 |
If found, return the index; if not found, return 0." |
|
2192 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2193 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 2194 |
! |
2195 |
||
2196 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
2197 |
"find a subcollection, starting at index. If found, return the index; |
|
2198 |
if not found, return the result of evaluating exceptionBlock. |
|
2199 |
This is a q&d hack - not very efficient" |
|
2200 |
||
2201 |
|same first |
|
2202 |
mySize "{Class: SmallInteger }" |
|
2203 |
checkIndex "{Class: SmallInteger }" |
|
2204 |
cmpIndex "{Class: SmallInteger }" |
|
2205 |
sz "{Class: SmallInteger }"| |
|
2206 |
||
2207 |
mySize := self size. |
|
2208 |
sz := aCollection size. |
|
2209 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
2210 |
first := aCollection at:1. |
|
2211 |
||
2212 |
checkIndex := startIndex - 1. |
|
2213 |
[true] whileTrue:[ |
|
2214 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
|
2215 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
2216 |
||
2217 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
2218 |
^ 0 |
|
2219 |
]. |
|
2220 |
same := true. |
|
2221 |
cmpIndex := 1. |
|
2222 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
2223 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
2224 |
same := false |
|
2225 |
] ifTrue:[ |
|
2226 |
cmpIndex := cmpIndex + 1 |
|
2227 |
] |
|
2228 |
]. |
|
2229 |
same ifTrue:[^ checkIndex]. |
|
2230 |
] |
|
2231 |
||
2232 |
" |
|
2233 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
2234 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
2235 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
2236 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
2237 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
2238 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
2239 |
" |
|
602 | 2240 |
! |
2241 |
||
2242 |
lastIndexOf:anElement |
|
2243 |
"search the collection backwards for anElement; |
|
2244 |
if found, return the index otherwise return 0. |
|
2245 |
The comparison is done using = |
|
2246 |
(i.e. equality test - not identity test)." |
|
2247 |
||
2248 |
^ self lastIndexOf:anElement startingAt:self size |
|
2249 |
||
2250 |
" |
|
2251 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
2252 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
2253 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
2254 |
#(10 20 30 40 50 60 70) lastIndexOf:10 |
|
2255 |
" |
|
2256 |
! |
|
2257 |
||
2258 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
|
2259 |
"search the collection backwards for anElement; |
|
2260 |
if found, return the index otherwise return the value of the |
|
2261 |
exceptionBlock. |
|
2262 |
The comparison is done using = |
|
2263 |
(i.e. equality test - not identity test)." |
|
2264 |
||
2265 |
|index| |
|
2266 |
||
2267 |
index := self lastIndexOf:anElement startingAt:self size. |
|
2268 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2269 |
^ index |
|
2270 |
||
2271 |
" |
|
2272 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
|
2273 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
2274 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
2275 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 ifAbsent:['none'] |
|
2276 |
" |
|
2277 |
! |
|
2278 |
||
2279 |
lastIndexOf:anElement startingAt:start |
|
2280 |
"search the collection backwards for anElement, starting the search at index start; |
|
2281 |
if found, return the index otherwise return 0. |
|
2282 |
The comparison is done using = |
|
2283 |
(i.e. equality test - not identity test)." |
|
2284 |
||
2285 |
|startIndex "{ Class: SmallInteger }"| |
|
2286 |
||
2287 |
startIndex := start. |
|
2288 |
startIndex to:1 by:-1 do:[:index | |
|
2289 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2290 |
]. |
|
2291 |
^ 0 |
|
2292 |
||
2293 |
" |
|
2294 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
|
2295 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
2296 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
2297 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
2298 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:4 |
|
2299 |
" |
|
2300 |
! |
|
2301 |
||
2302 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
|
2303 |
"search the collection backwards for anElement starting the search at |
|
2304 |
index start; if found, return the index |
|
2305 |
otherwise return the value of the exceptionBlock. |
|
2306 |
The comparison is done using = |
|
2307 |
(i.e. equality test - not identity test)." |
|
2308 |
||
2309 |
|index| |
|
2310 |
||
2311 |
index := self lastIndexOf:anElement startingAt:start. |
|
2312 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2313 |
^ index |
|
2314 |
||
2315 |
" |
|
2316 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
2317 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
2318 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
2319 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 ifAbsent:['none'] |
|
2320 |
" |
|
2321 |
! |
|
2322 |
||
2323 |
nextIndexOf:anElement from:start to:stop |
|
2324 |
"search the collection for anElement, starting the search at index start, |
|
2325 |
stopping at:stop; |
|
2326 |
if found, return the index otherwise return 0. |
|
2327 |
The comparison is done using = |
|
2328 |
(i.e. equality test - not identity test)." |
|
2329 |
||
2330 |
|startIndex "{ Class: SmallInteger }" |
|
2331 |
stopIndex "{ Class: SmallInteger }" | |
|
2332 |
||
2333 |
startIndex := start. |
|
2334 |
stopIndex := stop. |
|
2335 |
startIndex to:stop do:[:index | |
|
2336 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2337 |
]. |
|
2338 |
^ 0 |
|
2339 |
||
2340 |
" |
|
2341 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
2342 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
2343 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
2344 |
" |
|
2345 |
! |
|
2346 |
||
2347 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
2348 |
"search the collection for anElement, starting the search at index start |
|
2349 |
and stopping at stop; |
|
2350 |
if found, return the index otherwise return the value of the |
|
2351 |
exceptionBlock. |
|
2352 |
The comparison is done using = |
|
2353 |
(i.e. equality test - not identity test)." |
|
2354 |
||
2355 |
|index| |
|
2356 |
||
2357 |
index := self nextIndexOf:anElement from:start to:stop. |
|
2358 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2359 |
^ index |
|
2360 |
||
2361 |
" |
|
2362 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
2363 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
2364 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
2365 |
" |
|
1 | 2366 |
! ! |
2367 |
||
2368 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
2369 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2370 |
quickSortFrom:inBegin to:inEnd |
1 | 2371 |
"actual quicksort worker for sort-message" |
2372 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2373 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2374 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2375 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2376 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2377 |
middleElement temp1 temp2 | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2378 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2379 |
begin := inBegin. "/ this also does a type-check |
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2380 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2381 |
|
1 | 2382 |
b := begin. |
2383 |
e := end. |
|
2384 |
middleElement := self at:((b + e) // 2). |
|
2385 |
||
2386 |
[b < e] whileTrue:[ |
|
159 | 2387 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
2388 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
1 | 2389 |
|
159 | 2390 |
(b <= e) ifTrue:[ |
2391 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2392 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2393 |
self at:b put:temp2. self at:e put:temp1 |
159 | 2394 |
]. |
2395 |
b := b + 1. |
|
2396 |
e := e - 1 |
|
2397 |
] |
|
1 | 2398 |
]. |
2399 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e]. |
|
2400 |
(b < end) ifTrue:[self quickSortFrom:b to:end] |
|
2401 |
! |
|
2402 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2403 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
1 | 2404 |
"actual quicksort worker for sort:-message" |
2405 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2406 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2407 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2408 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2409 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2410 |
middleElement temp1 temp2 | |
1 | 2411 |
|
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2412 |
begin := inBegin. "/ this also does a type-check |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2413 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2414 |
|
1 | 2415 |
b := begin. |
2416 |
e := end. |
|
2417 |
middleElement := self at:((b + e) // 2). |
|
2418 |
||
2419 |
[b < e] whileTrue:[ |
|
159 | 2420 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
2421 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
1 | 2422 |
|
159 | 2423 |
(b <= e) ifTrue:[ |
2424 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2425 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2426 |
self at:b put:temp2. self at:e put:temp1 |
159 | 2427 |
]. |
2428 |
b := b + 1. |
|
2429 |
e := e - 1 |
|
2430 |
] |
|
1 | 2431 |
]. |
2432 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock]. |
|
2433 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock] |
|
2434 |
! |
|
2435 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2436 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
1 | 2437 |
"actual quicksort worker for sort:with:-message" |
2438 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2439 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2440 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2441 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2442 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2443 |
middleElement temp1 temp2 | |
1 | 2444 |
|
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2445 |
begin := inBegin. "/ this also does a type-check |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2446 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2447 |
|
1 | 2448 |
b := begin. |
2449 |
e := end. |
|
2450 |
middleElement := self at:((b + e) // 2). |
|
2451 |
||
2452 |
[b < e] whileTrue:[ |
|
159 | 2453 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
2454 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
1 | 2455 |
|
159 | 2456 |
(b <= e) ifTrue:[ |
2457 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2458 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2459 |
self at:b put:temp2. self at:e put:temp1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2460 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2461 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
159 | 2462 |
]. |
2463 |
b := b + 1. |
|
2464 |
e := e - 1 |
|
2465 |
] |
|
1 | 2466 |
]. |
2467 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
|
2468 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
|
2469 |
! |
|
2470 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2471 |
quickSortFrom:inBegin to:inEnd with:aCollection |
602 | 2472 |
"actual quicksort worker for sortWith-message" |
2473 |
||
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2474 |
|begin "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2475 |
end "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2476 |
b "{ Class: SmallInteger }" |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2477 |
e "{ Class: SmallInteger }" |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2478 |
middleElement temp1 temp2 | |
602 | 2479 |
|
890
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2480 |
begin := inBegin. "/ this also does a type-check |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2481 |
end := inEnd. |
0235f2dc2d74
more integer decls for inlinable loops in sort methods
Claus Gittinger <cg@exept.de>
parents:
869
diff
changeset
|
2482 |
|
602 | 2483 |
b := begin. |
2484 |
e := end. |
|
2485 |
middleElement := self at:((b + e) // 2). |
|
2486 |
||
2487 |
[b < e] whileTrue:[ |
|
2488 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
2489 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
2490 |
||
2491 |
(b <= e) ifTrue:[ |
|
2492 |
(b == e) ifFalse:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2493 |
temp1 := self at:b. temp2 := self at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2494 |
self at:b put:temp2. self at:e put:temp1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2495 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2496 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
602 | 2497 |
]. |
2498 |
b := b + 1. |
|
2499 |
e := e - 1 |
|
2500 |
] |
|
2501 |
]. |
|
2502 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e with:aCollection]. |
|
2503 |
(b < end) ifTrue:[self quickSortFrom:b to:end with:aCollection] |
|
2504 |
! |
|
2505 |
||
2506 |
reverse |
|
2507 |
"reverse the order of the elements inplace" |
|
2508 |
||
2509 |
|lowIndex "{ Class: SmallInteger }" |
|
2510 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2511 |
t1 t2| |
602 | 2512 |
|
2513 |
hiIndex := self size. |
|
2514 |
lowIndex := 1. |
|
2515 |
[lowIndex < hiIndex] whileTrue:[ |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2516 |
t1 := self at:lowIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2517 |
t2 := self at:hiIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2518 |
self at:lowIndex put:t2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2519 |
self at:hiIndex put:t1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2520 |
|
602 | 2521 |
lowIndex := lowIndex + 1. |
2522 |
hiIndex := hiIndex - 1 |
|
2523 |
] |
|
69 | 2524 |
" |
602 | 2525 |
#(4 5 6 7 7) reverse |
2526 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 2527 |
" |
1 | 2528 |
! |
2529 |
||
2530 |
sort |
|
2531 |
"sort the collection inplace. The elements are compared using |
|
159 | 2532 |
> and < i.e. they should offer a magnitude-like protocol. |
2533 |
The implementation uses the quicksort algorithm, which may not be |
|
2534 |
the best possible for all situations." |
|
13 | 2535 |
|
32 | 2536 |
|stop| |
1 | 2537 |
|
32 | 2538 |
stop := self size. |
2539 |
(stop > 1) ifTrue:[ |
|
159 | 2540 |
self quickSortFrom:1 to:stop |
1 | 2541 |
] |
2542 |
||
95 | 2543 |
" |
2544 |
#(1 16 7 98 3 19 4 0) sort |
|
2545 |
||
2546 |
|data| |
|
2547 |
data := Random new next:100000. |
|
2548 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2549 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2550 |
data reverse. |
|
2551 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
2552 |
" |
|
1 | 2553 |
! |
2554 |
||
2555 |
sort:sortBlock |
|
2556 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2557 |
for comparison. This allows any sort criteria to be implemented." |
|
2558 |
||
32 | 2559 |
|stop| |
1 | 2560 |
|
32 | 2561 |
stop := self size. |
2562 |
(stop > 1) ifTrue:[ |
|
159 | 2563 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
1 | 2564 |
] |
2565 |
||
95 | 2566 |
" |
2567 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
2568 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
2569 |
" |
|
1 | 2570 |
! |
2571 |
||
2572 |
sort:sortBlock with:aCollection |
|
2573 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 2574 |
for comparison. Also reorder the elements in aCollection. |
2575 |
Use, when you have a key collection to sort some other collection with." |
|
1 | 2576 |
|
32 | 2577 |
|stop| |
1 | 2578 |
|
32 | 2579 |
stop := self size. |
2580 |
(stop > 1) ifTrue:[ |
|
159 | 2581 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
1 | 2582 |
] |
2583 |
||
69 | 2584 |
" |
2585 |
|c1 c2| |
|
1 | 2586 |
c1 := #(1 16 7 9). |
2587 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
2588 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 2589 |
c1 printNL. |
2590 |
c2 printNL |
|
69 | 2591 |
" |
1 | 2592 |
! |
2593 |
||
602 | 2594 |
sortWith:aCollection |
2595 |
"sort the receiver collection inplace, also sort aCollection with it. |
|
2596 |
Use, when you have a key collection to sort another collection with." |
|
2597 |
||
2598 |
|stop| |
|
356 | 2599 |
|
2600 |
stop := self size. |
|
602 | 2601 |
(stop > 1) ifTrue:[ |
2602 |
self quickSortFrom:1 to:stop with:aCollection |
|
1 | 2603 |
] |
77 | 2604 |
|
2605 |
" |
|
602 | 2606 |
|c1 c2| |
2607 |
c1 := #(1 16 7 9). |
|
2608 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
2609 |
c1 sortWith:c2. |
|
2610 |
c1 printNL. |
|
2611 |
c2 printNL |
|
424 | 2612 |
" |
2613 |
! |
|
2614 |
||
602 | 2615 |
topologicalSort:sortBlock |
2616 |
"sort the collection inplace using a sloooow sort algorithm. |
|
2617 |
This algorithm has O-square runtime behavior and should be used only |
|
2618 |
in special situations. |
|
2619 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
2620 |
a>c (for example, to sort classes by inheritance) |
|
2621 |
||
2622 |
In other situations, use #sort; which implements the quicksort algorithm. |
|
77 | 2623 |
" |
602 | 2624 |
|
2625 |
|smallestIndex "{ Class: SmallInteger }" |
|
2626 |
end "{ Class: SmallInteger }" |
|
2627 |
smallest thisOne| |
|
2628 |
||
2629 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
2630 |
||
2631 |
end := self size. |
|
862 | 2632 |
1 to:end-1 do:[:index | |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2633 |
|index2| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2634 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2635 |
smallest := self at:index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2636 |
smallestIndex := index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2637 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2638 |
index2 := index + 1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2639 |
[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2640 |
thisOne := self at:index2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2641 |
(index2 ~~ smallestIndex and:[(sortBlock value:thisOne value:smallest)]) ifTrue:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2642 |
smallestIndex := index2. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2643 |
smallest := thisOne. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2644 |
index2 := index + 1. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2645 |
] ifFalse:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2646 |
index2 := index2 + 1.. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2647 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2648 |
] doWhile:[index2 <= end]. |
862 | 2649 |
|
2650 |
"/ (index + 1) to:end do:[:index2 | |
|
2651 |
"/ thisOne := self at:index2. |
|
2652 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
2653 |
"/ smallestIndex := index2. |
|
2654 |
"/ smallest := thisOne |
|
2655 |
"/ ]. |
|
2656 |
"/ ]. |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2657 |
(smallestIndex ~~ index) ifTrue:[ |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2658 |
thisOne := self at:index. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2659 |
self at:index put:smallest. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2660 |
self at:smallestIndex put:thisOne |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2661 |
]. |
1 | 2662 |
] |
95 | 2663 |
|
77 | 2664 |
" |
602 | 2665 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
2666 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
2667 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
2668 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 2669 |
" |
1 | 2670 |
! ! |
602 | 2671 |
|
628 | 2672 |
!SequenceableCollection class methodsFor:'documentation'! |
2673 |
||
2674 |
version |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
2675 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.60 1996-05-11 09:16:03 cg Exp $' |
628 | 2676 |
! ! |