author | Claus Gittinger <cg@exept.de> |
Mon, 12 Jan 1998 14:23:21 +0100 | |
changeset 3165 | b6bde90005a8 |
parent 3055 | 0832fba4c740 |
child 3234 | 4cc58bc6a8ed |
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 |
||
2069 | 20 |
!SequenceableCollection class methodsFor:'documentation'! |
88 | 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 |
|
1385 | 41 |
it in the system. |
42 |
||
43 |
The methods found here assume that implementations of (at least) |
|
44 |
#at:/#at:put: are implemented (which are always found in Object for |
|
45 |
indexable objects). |
|
46 |
||
47 |
For performance, some subclasses redefine more methods, knowing the details |
|
48 |
of how elements are stored. |
|
49 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
|
50 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this |
|
51 |
kind of tuneup. |
|
52 |
||
53 |
See concrete subclasses (such as OrderedCollection, Array or String). |
|
1289 | 54 |
|
55 |
[author:] |
|
56 |
Claus Gittinger |
|
1385 | 57 |
|
58 |
[see also:] |
|
59 |
OrderedCollection Array |
|
60 |
CharacterArray String |
|
88 | 61 |
" |
62 |
! ! |
|
1 | 63 |
|
2069 | 64 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 65 |
|
66 |
new:size withAll:element |
|
360 | 67 |
"return a new collection of size, where all elements are |
68 |
initialized to element." |
|
1 | 69 |
|
70 |
|newCollection| |
|
71 |
||
72 |
newCollection := self new:size. |
|
73 |
newCollection atAllPut:element. |
|
74 |
^ newCollection |
|
360 | 75 |
! |
76 |
||
77 |
withSize:size |
|
78 |
"return a new collection of size. |
|
79 |
For variable size collections, this is different from #new:, |
|
80 |
in that #new: creates an empty collection with preallocated size, |
|
81 |
while #withSize: creates a non empty one." |
|
82 |
||
83 |
|newCollection| |
|
84 |
||
85 |
newCollection := self new:size. |
|
86 |
newCollection grow:size. |
|
87 |
^ newCollection |
|
88 |
||
89 |
" |
|
90 |
(OrderedCollection new:10) inspect. |
|
91 |
(OrderedCollection withSize:10) inspect. |
|
92 |
(Array new:10) inspect. |
|
93 |
(Array withSize:10) inspect. |
|
94 |
" |
|
1 | 95 |
! ! |
96 |
||
97 |
!SequenceableCollection methodsFor:'accessing'! |
|
98 |
||
1366 | 99 |
after:anObject |
100 |
"return the element, after anObject. |
|
101 |
If anObject is not in the receiver, report an error. |
|
102 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
|
103 |
||
104 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
105 |
||
106 |
" |
|
107 |
#(4 3 2 1) asOrderedCollection after:3. |
|
108 |
#(4 3 2 1) asOrderedCollection after:5 |
|
109 |
#(4 3 2 1) asOrderedCollection after:1 |
|
110 |
" |
|
111 |
||
112 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
113 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
114 |
! |
|
115 |
||
116 |
after:anObject ifAbsent:exceptionBlock |
|
117 |
"return the element, after anObject. If there is no such element, |
|
118 |
return the value from exceptionBlock." |
|
119 |
||
120 |
^ self subclassResponsibility |
|
121 |
||
122 |
"Created: 10.5.1996 / 14:00:10 / cg" |
|
123 |
! |
|
124 |
||
32 | 125 |
at:index ifAbsent:exceptionBlock |
61 | 126 |
"return the element at index if valid. |
127 |
If the index is invalid, return the result of evaluating |
|
302 | 128 |
the exceptionblock. |
129 |
NOTICE: |
|
130 |
in ST-80, this message is only defined for Dictionaries, |
|
131 |
however, having a common protocol with indexed collections |
|
132 |
often simplifies things." |
|
32 | 133 |
|
134 |
((index < 1) or:[index > self size]) ifTrue:[ |
|
159 | 135 |
^ exceptionBlock value |
32 | 136 |
]. |
137 |
^ self at:index |
|
138 |
||
95 | 139 |
" |
140 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 141 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
142 |
||
95 | 143 |
(Dictionary with:(#foo -> #bar) |
159 | 144 |
with:(#frob -> #baz)) |
145 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 146 |
" |
362 | 147 |
! |
148 |
||
1366 | 149 |
before:anObject |
150 |
"return the element before the argument, anObject. |
|
151 |
If anObject is not in the receiver, report an error. |
|
152 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
|
153 |
||
154 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
155 |
||
156 |
" |
|
157 |
#(4 3 2 1) asOrderedCollection before:3. |
|
158 |
#(4 3 2 1) asOrderedCollection before:4 |
|
159 |
#(4 3 2 1) asOrderedCollection before:0 |
|
160 |
" |
|
161 |
||
162 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
163 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
164 |
! |
|
165 |
||
166 |
before:anObject ifAbsent:exceptionBlock |
|
167 |
"return the element, before anObject. If there is no such element, |
|
168 |
return the value from exceptionBlock." |
|
169 |
||
170 |
^ self subclassResponsibility |
|
171 |
||
172 |
"Created: 10.5.1996 / 14:03:53 / cg" |
|
173 |
! |
|
174 |
||
602 | 175 |
first |
176 |
"return the first element" |
|
177 |
||
178 |
^ self at:1 |
|
362 | 179 |
! |
180 |
||
181 |
keyAtEqualValue: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 indexOf:value |
|
187 |
! |
|
188 |
||
189 |
keyAtEqualValue: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 indexOf:value ifAbsent:exceptionBlock |
|
602 | 195 |
! |
196 |
||
197 |
keyAtValue:value |
|
198 |
"return the index of a value. |
|
199 |
This is normally not used (use indexOf:), but makes the |
|
200 |
protocol more compatible with dictionaries." |
|
201 |
||
202 |
^ self identityIndexOf:value |
|
203 |
! |
|
204 |
||
205 |
keyAtValue:value ifAbsent:exceptionBlock |
|
206 |
"return the index of a value. |
|
207 |
This is normally not used (use indexOf:), but makes the |
|
208 |
protocol more compatible with dictionaries." |
|
209 |
||
210 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
211 |
! |
|
212 |
||
213 |
last |
|
214 |
"return the last element" |
|
215 |
||
216 |
^ self at:(self size) |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
217 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
218 |
|
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
219 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
220 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
221 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
222 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
223 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
224 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
225 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
226 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
227 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
228 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
229 |
! |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
230 |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
231 |
upTo:anElement |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
232 |
"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
|
233 |
(but excluding) anElement. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
234 |
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
|
235 |
will consist of all elements of the receiver" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
236 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
237 |
|pos| |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
238 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
239 |
pos := self indexOf:anElement. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
240 |
pos == 0 ifTrue:[^ self copy]. |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
241 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
242 |
^ self copyFrom:1 to:(pos - 1) |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
243 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
244 |
" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
245 |
#(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
|
246 |
'hello world' upTo:Character space |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
247 |
#(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
|
248 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
249 |
raises an error: |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
250 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
251 |
(Dictionary new |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
252 |
at:#foo put:'foo'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
253 |
at:#bar put:'bar'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
254 |
at:#baz put:'baz'; |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
255 |
yourself) upTo:#bar |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
256 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
257 |
" |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
258 |
|
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
259 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
1 | 260 |
! ! |
261 |
||
602 | 262 |
!SequenceableCollection methodsFor:'adding & removing'! |
263 |
||
264 |
add:anObject |
|
265 |
"append the argument, anObject to the collection. |
|
266 |
Return the argument, anObject. |
|
267 |
||
268 |
Notice, that this is modifies the receiver NOT a copy. |
|
269 |
Also note, that it may be a slow operation for some collections, |
|
270 |
due to the grow:-message, which is inefficient for fixed size |
|
271 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
272 |
||
273 |
|newSize| |
|
274 |
||
275 |
newSize := self size + 1. |
|
276 |
self grow:newSize. |
|
277 |
self at:newSize put:anObject. |
|
278 |
^ anObject |
|
279 |
||
280 |
" |
|
281 |
|a| |
|
282 |
a := #(1 2 3 4 5 6 7 8). |
|
283 |
a add:'hello'. |
|
284 |
a |
|
285 |
" |
|
286 |
" |
|
287 |
|c| |
|
288 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
289 |
c add:'hello'. |
|
290 |
c |
|
291 |
" |
|
292 |
! |
|
293 |
||
294 |
add:anElement beforeIndex:index |
|
295 |
"insert the first argument, anObject into the collection before slot index. |
|
296 |
Return the receiver (sigh - ST-80 compatibility). |
|
297 |
||
298 |
Notice, that this is modifies the receiver NOT a copy. |
|
299 |
Also note, that it may be a slow operation for some collections, |
|
300 |
due to the grow:-message, which is inefficient for fixed size |
|
301 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
302 |
||
303 |
|newSize| |
|
304 |
||
305 |
newSize := self size + 1. |
|
306 |
self grow:newSize. |
|
307 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
308 |
self at:index put:anElement |
|
309 |
||
310 |
" |
|
311 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
312 |
" |
|
202 | 313 |
! |
314 |
||
602 | 315 |
addFirst:anObject |
316 |
"prepend the argument, anObject to the collection. |
|
317 |
Return the argument, anObject. |
|
318 |
||
319 |
Notice, that this is modifies the receiver NOT a copy. |
|
320 |
Also note, that it may be a slow operation for some collections, |
|
321 |
due to the grow:-message, which is inefficient for fixed size |
|
322 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
323 |
||
324 |
|newSize| |
|
325 |
||
326 |
newSize := self size + 1. |
|
327 |
self grow:newSize. |
|
328 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
329 |
self at:1 put:anObject. |
|
330 |
^ anObject |
|
331 |
||
332 |
" |
|
333 |
|a| |
|
334 |
a:= #(1 2 3 4 5 6 7 8). |
|
335 |
a addFirst:'hello'. |
|
336 |
a |
|
337 |
" |
|
338 |
" |
|
339 |
|c| |
|
340 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
341 |
c addFirst:'hello'. |
|
342 |
c |
|
343 |
" |
|
69 | 344 |
! |
345 |
||
602 | 346 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
347 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
348 |
if found remove and return it; if not, return the value from evaluating aBlock. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
349 |
Use equality compare (=) to search for an occurrence. |
602 | 350 |
|
351 |
Notice, that this is modifies the receiver NOT a copy. |
|
352 |
Also note, that it may be a slow operation for some collections, |
|
353 |
due to the grow:-message, which is inefficient for fixed size |
|
354 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
355 |
||
356 |
|any |
|
357 |
dstIndex "{ Class: SmallInteger }" |
|
358 |
sz "{ Class: SmallInteger }"| |
|
359 |
||
360 |
dstIndex := 1. |
|
361 |
any := false. |
|
362 |
sz := self size. |
|
363 |
1 to:sz do:[:srcIndex | |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
364 |
(anElement = (self at:srcIndex)) ifTrue:[ |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
365 |
any := true |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
366 |
] ifFalse:[ |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
367 |
(dstIndex ~~ srcIndex) ifTrue:[ |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
368 |
self at:dstIndex put:(self at:srcIndex) |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
369 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
370 |
dstIndex := dstIndex + 1 |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
371 |
] |
602 | 372 |
]. |
373 |
any ifTrue:[ |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
374 |
self grow:dstIndex - 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
375 |
^ anElement |
602 | 376 |
]. |
377 |
^ aBlock value |
|
378 |
||
379 |
" |
|
1422 | 380 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 381 |
#(1 2 3 4 5 6 7 8 9 0) remove:99 ifAbsent:[#oops] |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
382 |
#(1.0 2.0 3.0 4.0 5.0) remove:5 ifAbsent:[#oops] |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
383 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 384 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
385 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
386 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 387 |
! |
388 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
389 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
390 |
"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
|
391 |
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
|
392 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
393 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
394 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
395 |
"/ 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
|
396 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
397 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
398 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
399 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
400 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
401 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
402 |
[runIndex <= self size] whileTrue:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
403 |
element := self at:runIndex. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
404 |
(aBlock value:element) ifTrue:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
405 |
removed add:element. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
406 |
self removeAtIndex:runIndex |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
407 |
] ifFalse:[ |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
408 |
runIndex := runIndex + 1 |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
409 |
] |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
410 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
411 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
412 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
413 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
414 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
415 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
416 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 417 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
418 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
419 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
420 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
421 |
"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
|
422 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
423 |
|
602 | 424 |
removeAtIndex:anIndex |
425 |
"remove the element stored at anIndex. Return the removed object. |
|
426 |
||
427 |
Notice, that this is modifies the receiver NOT a copy. |
|
428 |
Also note, that it may be a slow operation for some collections, |
|
429 |
due to the grow:-message, which is inefficient for fixed size |
|
430 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
431 |
||
432 |
|element| |
|
433 |
||
434 |
element := self at:anIndex. |
|
435 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
436 |
^ element |
|
437 |
||
438 |
" |
|
439 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
440 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
441 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
442 |
" |
|
13 | 443 |
! |
444 |
||
602 | 445 |
removeFirst |
446 |
"remove the first element of the receiver and return it. |
|
447 |
||
448 |
Notice, that this is modifies the receiver NOT a copy. |
|
449 |
Also note, that it may be a slow operation for some collections, |
|
450 |
due to the grow:-message, which is inefficient for fixed size |
|
451 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
452 |
||
453 |
^ self removeAtIndex:1 |
|
454 |
||
455 |
" |
|
456 |
|a| |
|
457 |
a := #(1 2 3 4 5 6). |
|
458 |
a removeFirst. |
|
459 |
a |
|
460 |
" |
|
13 | 461 |
! |
462 |
||
2294 | 463 |
removeFromIndex:startIndex |
464 |
"remove the elements stored at indexes from startIndex to the end. |
|
465 |
Return the receiver. |
|
466 |
Returning the receiver is a historic leftover - it may at one |
|
467 |
time return a collection of the removed elements. |
|
468 |
||
469 |
Notice, that this is modifies the receiver - NOT a copy; |
|
470 |
therefore any other users of the receiver will also see this change. |
|
471 |
Also note, that it may be a slow operation for some collections, |
|
472 |
due to the grow:-message, which is inefficient for fixed size |
|
473 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
474 |
||
475 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
476 |
||
477 |
" |
|
478 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
|
479 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
|
480 |
" |
|
481 |
||
482 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
483 |
! |
|
484 |
||
602 | 485 |
removeFromIndex:startIndex toIndex:endIndex |
486 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
487 |
Return the receiver. |
|
488 |
Returning the receiver is a historic leftover - it may at one |
|
489 |
time return a collection of the removed elements. |
|
490 |
||
491 |
Notice, that this is modifies the receiver - NOT a copy; |
|
492 |
therefore any other users of the receiver will also see this change. |
|
493 |
Also note, that it may be a slow operation for some collections, |
|
494 |
due to the grow:-message, which is inefficient for fixed size |
|
495 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
496 |
||
497 |
|size newSize| |
|
498 |
||
499 |
(endIndex >= (size := self size)) ifTrue:[ |
|
500 |
self grow:(startIndex - 1) |
|
501 |
] ifFalse:[ |
|
502 |
newSize := size - endIndex + startIndex - 1. |
|
503 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
504 |
self grow:newSize |
|
505 |
] |
|
506 |
||
507 |
"/ |newSize| |
|
508 |
"/ |
|
509 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
510 |
"/ newSize <= 0 ifTrue:[ |
|
511 |
"/ self grow:0 |
|
512 |
"/ ] ifFalse:[ |
|
513 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
514 |
"/ self grow:newSize |
|
515 |
"/ ] |
|
516 |
||
517 |
" |
|
518 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
|
519 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
520 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
521 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
522 |
" |
|
523 |
! |
|
524 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
525 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
526 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
527 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 528 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
529 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
530 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
531 |
Also note, that it may be a slow operation for some collections, |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
532 |
due to the grow:-message, which is inefficient for fixed size |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
533 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
534 |
|
2363 | 535 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
536 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
537 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
538 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
539 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
540 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
541 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
542 |
1 to:sz do:[:srcIndex | |
2363 | 543 |
el := self at:srcIndex. |
544 |
(anElement == el) ifTrue:[ |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
545 |
any := true |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
546 |
] ifFalse:[ |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
547 |
(dstIndex ~~ srcIndex) ifTrue:[ |
2363 | 548 |
self at:dstIndex put:el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
549 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
550 |
dstIndex := dstIndex + 1 |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
551 |
] |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
552 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
553 |
any ifTrue:[ |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
554 |
self grow:dstIndex - 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
555 |
^ anElement |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
556 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
557 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
558 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
559 |
" |
2347
b79cdb6b553e
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2346
diff
changeset
|
560 |
#(1.0 2.0 3.0 4.0 5.0) remove:5 ifAbsent:[#oops] |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
561 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
562 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
563 |
|
2363 | 564 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
565 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
566 |
|
602 | 567 |
removeIndex:index |
568 |
"remove the argument stored at index. Return the receiver. |
|
569 |
||
570 |
Notice, that this is modifies the receiver NOT a copy. |
|
571 |
Also note, that it may be a slow operation for some collections, |
|
572 |
due to the grow:-message, which is inefficient for fixed size |
|
573 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
574 |
||
575 |
self removeFromIndex:index toIndex:index |
|
576 |
||
577 |
" |
|
578 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
579 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
580 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
581 |
" |
|
77 | 582 |
! |
583 |
||
602 | 584 |
removeLast |
585 |
"remove the last element of the receiver and return it. |
|
586 |
||
587 |
Notice, that this is modifies the receiver NOT a copy. |
|
588 |
Also note, that it may be a slow operation for some collections, |
|
589 |
due to the grow:-message, which is inefficient for fixed size |
|
590 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
591 |
||
592 |
^ self removeAtIndex:(self size) |
|
593 |
||
594 |
" |
|
595 |
|a| |
|
596 |
a := #(1 2 3 4 5 6). |
|
597 |
a removeLast. |
|
598 |
a |
|
599 |
" |
|
600 |
! ! |
|
601 |
||
602 |
!SequenceableCollection methodsFor:'comparing'! |
|
603 |
||
604 |
= aCollection |
|
605 |
"return true if the receiver and aCollection represent collections |
|
606 |
with equal contents." |
|
607 |
||
608 |
|index "{ Class: SmallInteger }" |
|
609 |
stop "{ Class: SmallInteger }" | |
|
610 |
||
611 |
(aCollection == self) ifTrue:[^true]. |
|
612 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
613 |
||
614 |
stop := self size. |
|
615 |
stop == (aCollection size) ifFalse:[^false]. |
|
616 |
||
617 |
index := 1. |
|
618 |
[index <= stop] whileTrue:[ |
|
619 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
620 |
index := index + 1 |
|
621 |
]. |
|
622 |
^ true |
|
623 |
||
624 |
" |
|
625 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
|
626 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
627 |
#($1 $2 $3 $4 $5) = '12345' |
|
628 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
629 |
" |
|
630 |
! |
|
631 |
||
632 |
endsWith:aCollection |
|
633 |
"return true, if the receivers last elements match those |
|
634 |
of aCollection" |
|
635 |
||
636 |
|index1 "{ Class: SmallInteger }" |
|
637 |
index2 "{ Class: SmallInteger }" |
|
638 |
stop "{ Class: SmallInteger }" |
|
639 |
sz "{ Class: SmallInteger }"| |
|
640 |
||
641 |
(aCollection == self) ifTrue:[^ true]. |
|
642 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
643 |
||
644 |
stop := aCollection size. |
|
77 | 645 |
sz := self size. |
602 | 646 |
stop > sz ifTrue:[^false]. |
647 |
||
648 |
index1 := sz. |
|
649 |
index2 := stop. |
|
650 |
[index2 > 0] whileTrue:[ |
|
651 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
|
652 |
index1 := index1 - 1. |
|
653 |
index2 := index2 - 1 |
|
77 | 654 |
]. |
602 | 655 |
^ true |
656 |
||
657 |
" |
|
658 |
'abcde' endsWith:#($d $e) |
|
659 |
#[1 2 3 4] endsWith:#(3 4) |
|
660 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
|
661 |
" |
|
662 |
! |
|
663 |
||
664 |
hash |
|
665 |
"return a hash key for the receiver" |
|
666 |
||
667 |
"this hash is stupid - but for larger collections, the hashing |
|
668 |
time can become much bigger than the time lost in added probing. |
|
669 |
Time will show ..." |
|
670 |
||
2411
6b3e74bf1703
Use #times: #instead of #* in hash to avoid long arithmethic.
Stefan Vogel <sv@exept.de>
parents:
2371
diff
changeset
|
671 |
^ ((self at:1 ifAbsent:0) hash times:self size) bitAnd:16r3FFFFFFF |
602 | 672 |
|
673 |
" |
|
674 |
#(1 2 3 4 5) hash |
|
675 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
676 |
" |
|
677 |
! |
|
678 |
||
679 |
startsWith:aCollection |
|
680 |
"return true, if the receivers first elements match those |
|
681 |
of aCollection" |
|
682 |
||
683 |
|index "{ Class: SmallInteger }" |
|
684 |
stop "{ Class: SmallInteger }" | |
|
685 |
||
686 |
(aCollection == self) ifTrue:[^true]. |
|
687 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
688 |
||
689 |
stop := aCollection size. |
|
690 |
stop > self size ifTrue:[^false]. |
|
691 |
||
692 |
index := 1. |
|
693 |
[index <= stop] whileTrue:[ |
|
694 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
|
695 |
index := index + 1 |
|
696 |
]. |
|
697 |
^ true |
|
698 |
||
699 |
" |
|
700 |
'abcde' startsWith:#($a $b $c) |
|
701 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
702 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
703 |
" |
|
13 | 704 |
! ! |
705 |
||
260 | 706 |
!SequenceableCollection methodsFor:'converting'! |
707 |
||
1413 | 708 |
asStringCollection |
709 |
"return a new string collection containing the elements; |
|
710 |
these ought to be strings. (i.e. String or Text instances)" |
|
711 |
||
712 |
|newColl sz| |
|
713 |
||
714 |
sz := self size. |
|
715 |
newColl := (StringCollection new:sz) grow:sz. |
|
716 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
|
717 |
^ newColl |
|
718 |
||
719 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
720 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
721 |
! |
|
722 |
||
1188 | 723 |
asStringWith:sepChar |
724 |
"return a string generated by concatenating my elements |
|
725 |
(which must be strings or nil) and embedding sepChar characters in between. |
|
726 |
Nil entries and empty strings are counted as empty lines." |
|
727 |
||
728 |
^ self asStringWith:sepChar from:1 to:(self size) |
|
1413 | 729 |
|
730 |
" |
|
731 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
732 |
" |
|
733 |
||
734 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 735 |
! |
736 |
||
737 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
738 |
"return part of myself as a string with embedded sepCharacters. |
|
739 |
My elements must be strings or nil; nil entries and empty strings are |
|
740 |
taken as empty lines." |
|
741 |
||
742 |
^ self |
|
743 |
asStringWith:sepCharacter |
|
744 |
from:firstLine |
|
745 |
to:lastLine |
|
746 |
compressTabs:false |
|
747 |
final:nil |
|
748 |
" |
|
749 |
creating entries for searchpath: |
|
750 |
||
751 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
|
752 |
||
753 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
754 |
" |
|
755 |
||
756 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
757 |
! |
|
758 |
||
260 | 759 |
asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
760 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 761 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 762 |
taken as empty lines. |
763 |
If the argument compressTabs is true, leading spaces are converted |
|
764 |
to tab-characters (8col tabs). The last line is followed by a final |
|
765 |
character (if non-nil). |
|
766 |
||
767 |
This method is tuned for big collections, in not creating many |
|
263 | 768 |
intermediate strings (has linear runtime). For very small collections |
769 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 770 |
|
293 | 771 |
|idx1 "{ Class:SmallInteger }" |
772 |
idx2 "{ Class:SmallInteger }" |
|
773 |
totalLength "{ Class:SmallInteger }" |
|
260 | 774 |
pos "{ Class:SmallInteger }" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
775 |
newString lineString spaces idx nTabs sepCnt |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
776 |
any16Bit stringClass needEmphasis newRuns c |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
777 |
thisLen anyTab| |
260 | 778 |
|
779 |
" |
|
780 |
first accumulate the size of the string, to avoid |
|
781 |
countless reallocations. If tabs are compressed, |
|
782 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 783 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 784 |
" |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
785 |
any16Bit := needEmphasis := false. |
1015 | 786 |
stringClass := String. |
787 |
||
260 | 788 |
totalLength := 0. |
789 |
sepCnt := sepCharacter notNil ifTrue:[1] ifFalse:[0]. |
|
790 |
||
293 | 791 |
idx1 := firstLine. |
792 |
idx2 := lastLine. |
|
793 |
idx1 to:idx2 do:[:lineIndex | |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
794 |
lineString := self at:lineIndex. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
795 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
796 |
lineString isNil ifTrue:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
797 |
totalLength := totalLength + sepCnt |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
798 |
] ifFalse: [ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
799 |
lineString hasChangeOfEmphasis ifTrue:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
800 |
needEmphasis := true |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
801 |
]. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
802 |
(lineString bitsPerCharacter == 16) ifTrue:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
803 |
any16Bit := true. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
804 |
stringClass := lineString class |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
805 |
]. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
806 |
totalLength := totalLength + lineString size + sepCnt |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
807 |
]. |
260 | 808 |
]. |
809 |
endCharacter isNil ifTrue:[ |
|
1673
eea933dbf42d
care for empty collection in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
1537
diff
changeset
|
810 |
totalLength := totalLength - 1. |
eea933dbf42d
care for empty collection in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
1537
diff
changeset
|
811 |
totalLength < 0 ifTrue:[^ '']. |
260 | 812 |
]. |
813 |
spaces := ' '. |
|
1016 | 814 |
newString := stringClass new:totalLength. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
815 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
816 |
needEmphasis ifTrue:[ |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
817 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
818 |
]. |
260 | 819 |
|
820 |
" |
|
821 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
822 |
Be careful with runArrays: |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
823 |
replacing individual elements is VERY expensive. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
824 |
Therefore, create a new runArray from scratch. |
260 | 825 |
" |
826 |
pos := 1. |
|
293 | 827 |
idx1 to:idx2 do:[:lineIndex | |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
828 |
lineString := self at:lineIndex. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
829 |
thisLen := lineString size. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
830 |
thisLen ~~ 0 ifTrue:[ |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
831 |
(anyTab := compressTabs) ifTrue:[ |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
832 |
" |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
833 |
mhmh: could use withTabs from String-class here, |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
834 |
but we should avoid creating too many temporary strings |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
835 |
(especially, since this method is typically used when converting |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
836 |
big texts such as when saving in the filebrowser ...). |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
837 |
Therefore, we convert tabs inline here doing a direct replace |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
838 |
in newString." |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
839 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
840 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
841 |
nTabs := (idx-1) // 8. |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
842 |
nTabs ~~ 0 ifTrue:[ |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
843 |
anyTab := true |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
844 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
845 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
846 |
anyTab ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
847 |
"any tabs" |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
848 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
849 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
850 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
851 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
852 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
853 |
newRuns add:nil withOccurrences:nTabs |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
854 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
855 |
pos := pos + nTabs. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
856 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
857 |
newString replaceFrom:pos with:lineString startingAt:idx. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
858 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
859 |
lineString hasChangeOfEmphasis ifTrue:[ |
2860
760052935b5a
oops - asStringWith: was wrong
Claus Gittinger <cg@exept.de>
parents:
2610
diff
changeset
|
860 |
idx to:lineString size do:[:pos | |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
861 |
newRuns add:(lineString emphasisAt:pos) |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
862 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
863 |
] ifFalse:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
864 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
865 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
866 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
867 |
pos := pos + thisLen - (nTabs * 8). |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
868 |
] ifFalse:[ |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
869 |
newString replaceFrom:pos with:lineString. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
870 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
871 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
872 |
lineString hasChangeOfEmphasis ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
873 |
newRuns addAll:(lineString emphasis) |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
874 |
] ifFalse:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
875 |
newRuns add:nil withOccurrences:lineString size |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
876 |
] |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
877 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
878 |
pos := pos + thisLen. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
879 |
]. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
880 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
881 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
882 |
lineIndex ~~ lastLine ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
883 |
c := sepCharacter |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
884 |
] ifFalse:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
885 |
c := endCharacter |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
886 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
887 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
888 |
c notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
889 |
newString at:pos put:c. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
890 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
891 |
newRuns add:nil. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
892 |
]. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
893 |
pos := pos + 1 |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
894 |
]. |
260 | 895 |
]. |
896 |
||
897 |
" |
|
898 |
in case of tab compression, the result has to be |
|
899 |
cut to size ... sorry |
|
900 |
" |
|
901 |
pos ~~ totalLength ifTrue:[ |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
902 |
newString := newString copyTo:(pos - 1) |
260 | 903 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
904 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
905 |
newRuns notNil ifTrue:[ |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
906 |
newString := Text string:newString runs:newRuns. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
907 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
908 |
|
260 | 909 |
^ newString |
910 |
||
911 |
" |
|
912 |
creating entries for searchpath: |
|
913 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
914 |
#('foo' 'bar' 'baz' '/foo/bar') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
915 |
asStringWith:$: |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
916 |
from:1 to:4 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
917 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
918 |
final:nil |
260 | 919 |
|
920 |
with trailing colon: |
|
921 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
922 |
#('foo' 'bar' 'baz' '/foo/bar') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
923 |
asStringWith:$: |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
924 |
from:1 to:4 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
925 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
926 |
final:$: |
260 | 927 |
|
928 |
concatenating all elements: |
|
929 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
930 |
#('foo' 'bar' 'baz') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
931 |
asStringWith:nil |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
932 |
from:1 to:3 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
933 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
934 |
final:nil |
260 | 935 |
|
936 |
creating a string from a collection of lines: |
|
937 |
||
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
938 |
#('foo' 'bar' 'baz') |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
939 |
asStringWith:(Character cr) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
940 |
from:1 to:3 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
941 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
942 |
final:(Character cr) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
943 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
944 |
creating a text from a collection of mixed texts and strings: |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
945 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
946 |
(Array |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
947 |
with:'foo' |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
948 |
with:('bar' asText allBold) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
949 |
with:'baz' |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
950 |
with:('baz2' asText emphasizeAllWith:#italic) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
951 |
) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
952 |
asStringWith:(Character cr) |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
953 |
from:1 to:4 |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
954 |
compressTabs:false |
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
955 |
final:(Character cr) |
260 | 956 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
957 |
|
2860
760052935b5a
oops - asStringWith: was wrong
Claus Gittinger <cg@exept.de>
parents:
2610
diff
changeset
|
958 |
"Modified: 16.8.1997 / 04:44:46 / cg" |
379 | 959 |
! |
960 |
||
602 | 961 |
asStringWithCRs |
962 |
"return a string generated by concatenating my elements |
|
963 |
(which must be strings or nil) and embedding cr characters in between. |
|
964 |
Nil entries and empty strings are counted as empty lines." |
|
965 |
||
966 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
1413 | 967 |
|
968 |
" |
|
969 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
|
970 |
||
971 |
(OrderedCollection new |
|
972 |
add:'hello'; |
|
973 |
add:'world'; |
|
974 |
add:'foo'; |
|
975 |
add:('bar' asText allBold); |
|
976 |
yourself) asStringWithCRs |
|
1421 | 977 |
|
1422 | 978 |
Transcript showCR: |
1421 | 979 |
(OrderedCollection new |
980 |
add:'hello'; |
|
981 |
add:'world'; |
|
982 |
add:'foo'; |
|
983 |
add:('bar' asText allBold); |
|
984 |
yourself) asStringWithCRs |
|
1413 | 985 |
" |
986 |
||
1421 | 987 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
602 | 988 |
! |
989 |
||
990 |
asStringWithCRsFrom:firstLine to:lastLine |
|
991 |
"return a string generated by concatenating some of my elements |
|
992 |
(which must be strings or nil) and embedding cr characters in between. |
|
993 |
Nil entries and empty strings are counted as empty lines." |
|
994 |
||
995 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
1421 | 996 |
|
997 |
" |
|
998 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
|
999 |
||
1000 |
" |
|
1001 |
||
1002 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
602 | 1003 |
! |
1004 |
||
1005 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
1006 |
"return part of myself as a string with embedded cr's. |
|
1007 |
My elements must be strings or nil. |
|
1008 |
If the argument compressTabs is true, leading spaces are converted |
|
1009 |
to tab-characters (8col tabs). |
|
1010 |
Nil entries and empty strings are taken as empty lines." |
|
1011 |
||
1012 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
1013 |
! |
|
1014 |
||
1015 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
1016 |
"return part of myself as a string with embedded cr's. |
|
1017 |
My elements must be strings or nil; nil entries and empty strings are |
|
1018 |
taken as empty lines. |
|
1019 |
If the argument compressTabs is true, leading spaces are converted |
|
1020 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
1021 |
should be followed by a cr or not." |
|
1022 |
||
1023 |
^ self asStringWith:(Character cr) |
|
1024 |
from:firstLine |
|
1025 |
to:lastLine |
|
1026 |
compressTabs:compressTabs |
|
1027 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
1028 |
! |
|
1029 |
||
379 | 1030 |
decodeAsLiteralArray |
1245 | 1031 |
"given a literalEncoding in the receiver, |
1032 |
create & return the corresponding object. |
|
1033 |
The inverse operation to #literalArrayEncoding." |
|
1034 |
||
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
1035 |
|clsName cls| |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
1036 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
1037 |
clsName := self first. |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
1038 |
cls := Smalltalk at:clsName ifAbsent:nil. |
379 | 1039 |
cls isNil ifTrue:[ |
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
1040 |
^ self error:'unknown class in spec: ' , clsName. |
379 | 1041 |
]. |
2371 | 1042 |
^ cls decodeFromLiteralArray:self. |
379 | 1043 |
|
1044 |
" |
|
1045 |
#(Point 10 20) decodeAsLiteralArray |
|
1046 |
#(Rectangle 100 200 400 500) decodeAsLiteralArray |
|
2417 | 1047 |
#(#MenuItem #rawLabel: 'right' #value: #right) decodeAsLiteralArray |
379 | 1048 |
" |
1245 | 1049 |
|
2417 | 1050 |
"Modified: 25.2.1997 / 19:52:40 / cg" |
422 | 1051 |
! |
1052 |
||
1053 |
literalArrayEncoding |
|
1245 | 1054 |
"encode myself as an array literal, from which a copy of the receiver |
1055 |
can be reconstructed with #decodeAsLiteralArray." |
|
1056 |
||
422 | 1057 |
^ (self collect:[:el | el literalArrayEncoding]) asArray |
1058 |
||
1059 |
" |
|
1060 |
(Array with:(Color red:50 green:50 blue:50) |
|
1245 | 1061 |
with:(1 @ 2) |
422 | 1062 |
) literalArrayEncoding |
1063 |
" |
|
1245 | 1064 |
|
1065 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
260 | 1066 |
! ! |
1067 |
||
1 | 1068 |
!SequenceableCollection methodsFor:'copying'! |
1069 |
||
1070 |
, aCollection |
|
1071 |
"return a new collection formed from concatenating the receiver with |
|
44 | 1072 |
the argument. The class of the new collection is determined by the |
1073 |
receivers class, so mixing classes is possible, if the second collections |
|
1074 |
elements can be stored into instances of the receivers class." |
|
1 | 1075 |
|
1076 |
|newCollection |
|
1077 |
mySize "{ Class: SmallInteger }" |
|
1078 |
newSize "{ Class: SmallInteger }" |
|
1079 |
otherSize "{ Class: SmallInteger }" |
|
1080 |
dstIndex "{ Class: SmallInteger }"| |
|
1081 |
||
1082 |
mySize := self size. |
|
1083 |
otherSize := aCollection size. |
|
1084 |
newSize := mySize + otherSize. |
|
202 | 1085 |
|
252 | 1086 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 1087 |
|
1088 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
1089 |
dstIndex := mySize + 1. |
|
202 | 1090 |
aCollection isSequenceable ifTrue:[ |
1091 |
" |
|
1092 |
yes, aCollection has indexed elements |
|
1093 |
" |
|
159 | 1094 |
newCollection replaceFrom:dstIndex to:newSize |
1095 |
with:aCollection startingAt:1. |
|
1096 |
^ newCollection |
|
1 | 1097 |
] ifFalse:[ |
202 | 1098 |
" |
1099 |
no, enumerate aCollection |
|
1100 |
" |
|
159 | 1101 |
aCollection do:[:element | |
1102 |
newCollection at:dstIndex put:element. |
|
1103 |
dstIndex := dstIndex + 1 |
|
1104 |
] |
|
1 | 1105 |
]. |
1106 |
^ newCollection |
|
44 | 1107 |
|
95 | 1108 |
" |
1109 |
#($a $b $c) , #(1 2 3) |
|
1110 |
#($a $b $c) , '123' |
|
1111 |
'abc' , '123' |
|
1112 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
1113 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
1114 |
'abc' asArray , #(1 2 3 4 5) |
|
1115 |
" |
|
1 | 1116 |
! |
1117 |
||
602 | 1118 |
copyFirst:count |
1119 |
"return a new collection consisting of the receivers first count |
|
1120 |
elements - this is just a rename of copyTo: - for compatibility." |
|
1121 |
||
1122 |
^ self copyFrom:1 to:count |
|
1123 |
||
1124 |
" |
|
1125 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
1126 |
'1234567890' copyFirst:4 |
|
1127 |
" |
|
1128 |
! |
|
1129 |
||
1130 |
copyFrom:start |
|
1131 |
"return a new collection consisting of receivers elements |
|
1132 |
from start to the end of the collection" |
|
1133 |
||
1134 |
^ self copyFrom:start to:(self size) |
|
1135 |
||
1136 |
" |
|
1137 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
1138 |
'1234567890' copyFrom:2 |
|
1139 |
" |
|
1140 |
! |
|
1141 |
||
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1142 |
copyFrom:start count:numberOfElements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1143 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1144 |
extracted from the receiver starting at index start |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1145 |
(i.e. extract a slice)" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1146 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1147 |
^ self |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1148 |
copyFrom:start |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1149 |
to:(start + numberOfElements - 1) |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1150 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1151 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1152 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1153 |
'1234567890' copyFrom:2 count:5 |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1154 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1155 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1156 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1157 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1158 |
|
602 | 1159 |
copyFrom:start to:stop |
1160 |
"return a new collection consisting of receivers elements |
|
1161 |
between start and stop" |
|
1162 |
||
1163 |
|newCollection newSize| |
|
1164 |
||
1165 |
newSize := stop - start + 1. |
|
1166 |
newCollection := self copyEmptyAndGrow:newSize. |
|
1167 |
newCollection replaceFrom:1 to:newSize with:self startingAt:start. |
|
1168 |
^ newCollection |
|
1169 |
||
1170 |
" |
|
1171 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
1172 |
'1234567890' copyFrom:2 to:5 |
|
1173 |
" |
|
1174 |
! |
|
1175 |
||
1176 |
copyLast:count |
|
1177 |
"return a new collection consisting of the receivers last count |
|
1178 |
elements." |
|
1179 |
||
1180 |
|sz| |
|
1181 |
||
1182 |
sz := self size. |
|
1183 |
^ self copyFrom:(sz - count + 1) to:sz |
|
1184 |
||
1185 |
" |
|
1186 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
1187 |
'1234567890' copyLast:4 |
|
1188 |
" |
|
1189 |
! |
|
1190 |
||
1191 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
|
1192 |
"return a copy of the receiver, where the elements from startIndex to |
|
1193 |
endIndex have been replaced by the elements of aCollection" |
|
1194 |
||
1195 |
|newColl sz replSize| |
|
1196 |
||
1197 |
replSize := aCollection size. |
|
1198 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
1199 |
newColl := self copyEmptyAndGrow:sz. |
|
1200 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
1201 |
newColl replaceFrom:startIndex with:aCollection. |
|
1202 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
1203 |
^ newColl |
|
1204 |
||
1205 |
" |
|
1206 |
#(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) |
|
1207 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
|
1208 |
" |
|
1209 |
! |
|
1210 |
||
1211 |
copyReplaceFrom:startIndex with:aCollection |
|
1212 |
"return a copy of the receiver, where the elements from startIndex to |
|
1213 |
the end have been replaced by the elements of aCollection" |
|
1214 |
||
1215 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
1216 |
||
1217 |
" |
|
1218 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
|
1219 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
1220 |
" |
|
1221 |
! |
|
1222 |
||
1223 |
copyThrough:element |
|
1224 |
"return a new collection consisting of the receiver elements |
|
1225 |
up-to (AND including) the first occurence of element." |
|
1226 |
||
1227 |
|idx| |
|
1228 |
||
1229 |
idx := self indexOf:element. |
|
1230 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
1231 |
^ self copyFrom:1 to:idx |
|
1232 |
||
1233 |
" |
|
1234 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
1235 |
'1234567890' copyThrough:$5 |
|
1236 |
'1234567890' copyThrough:$a |
|
1237 |
'1234567890' copyThrough:$1 |
|
1238 |
" |
|
1239 |
! |
|
1240 |
||
1241 |
copyTo:stop |
|
1242 |
"return a new collection consisting of receivers elements |
|
1243 |
from 1 up to (including) index stop" |
|
1244 |
||
1245 |
^ self copyFrom:1 to:stop |
|
1246 |
||
1247 |
" |
|
1248 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
1249 |
'1234567890' copyTo:4 |
|
1250 |
" |
|
1251 |
! |
|
1252 |
||
1253 |
copyUpTo:element |
|
1254 |
"return a new collection consisting of the receiver elements |
|
1255 |
up-to (but excluding) the first occurence of element; |
|
1256 |
or to the end, if element is not included" |
|
1257 |
||
1258 |
|idx| |
|
1259 |
||
1260 |
idx := self indexOf:element. |
|
1261 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
1262 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
1263 |
^ self copyFrom:1 to:(idx-1) |
|
1264 |
||
1265 |
" |
|
1266 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
1267 |
'1234567890' copyUpTo:$5 |
|
1268 |
'1234567890' copyUpTo:$a |
|
1269 |
'1234567890' copyUpTo:$1 |
|
1270 |
'123456789' copyUpTo:$0 |
|
1271 |
" |
|
1272 |
! |
|
1273 |
||
1 | 1274 |
copyWith:newElement |
233 | 1275 |
"return a new collection containing the receivers elements |
1276 |
and the single new element, newElement. |
|
1277 |
This is different from concatentation, which expects another collection |
|
1278 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 1279 |
|
1280 |
|newCollection mySize newSize| |
|
1281 |
||
1282 |
mySize := self size. |
|
1283 |
newSize := mySize + 1. |
|
252 | 1284 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 1285 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
1286 |
newCollection at:newSize put:newElement. |
|
1287 |
^newCollection |
|
44 | 1288 |
|
95 | 1289 |
" |
1290 |
#(1 2 3 4 5) copyWith:$a |
|
1291 |
'abcdefg' copyWith:$h |
|
1292 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
1293 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
1294 |
" |
|
1 | 1295 |
! |
1296 |
||
61 | 1297 |
copyWithout:elementToSkip |
1298 |
"return a new collection consisting of a copy of the receiver, with |
|
1299 |
ALL elements equal to elementToSkip are left out. |
|
1300 |
No error is reported, if elementToSkip is not in the collection." |
|
1301 |
||
293 | 1302 |
|n "{ Class: SmallInteger }" |
1303 |
sz "{ Class: SmallInteger }" |
|
1304 |
srcIndex "{ Class: SmallInteger }" |
|
1305 |
dstIndex "{ Class: SmallInteger }" |
|
1306 |
skipIndex "{ Class: SmallInteger }" |
|
1307 |
copy l| |
|
61 | 1308 |
|
1309 |
"the code below may look like overkill, |
|
1310 |
however, for big collections its better to move data |
|
1311 |
around in big chunks" |
|
1312 |
||
1313 |
n := self occurrencesOf:elementToSkip. |
|
1314 |
n == 0 ifTrue:[^ self copy]. |
|
1315 |
||
1316 |
sz := self size. |
|
252 | 1317 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 1318 |
|
1319 |
srcIndex := 1. |
|
1320 |
dstIndex := 1. |
|
1321 |
||
1322 |
n timesRepeat:[ |
|
159 | 1323 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
1324 |
l := skipIndex - srcIndex. |
|
1325 |
l ~~ 0 ifTrue:[ |
|
1326 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
1327 |
with:self startingAt:srcIndex. |
|
1328 |
dstIndex := dstIndex + l |
|
1329 |
]. |
|
1330 |
srcIndex := skipIndex + 1 |
|
61 | 1331 |
]. |
1332 |
l := sz - srcIndex. |
|
1333 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
159 | 1334 |
with:self startingAt:srcIndex. |
61 | 1335 |
^ copy |
1336 |
||
95 | 1337 |
" |
1338 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
1339 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
1340 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
202 | 1341 |
'abcdefghi' copyWithout:$h |
1342 |
'abcdefg' copyWithout:$h |
|
95 | 1343 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
1344 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
1345 |
#(90 80 70 60 50) copyWithout:70 |
|
1346 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
1347 |
" |
|
61 | 1348 |
! |
1349 |
||
602 | 1350 |
copyWithoutFirst:elementToSkip |
1351 |
"return a new collection consisting of a copy of the receivers elements |
|
1352 |
without the first elementToSkip, if it was present. |
|
1353 |
No error is reported, if elementToSkip is not in the collection." |
|
1354 |
||
1355 |
|copy skipIndex sz| |
|
1356 |
||
1357 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
1358 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
1359 |
||
1360 |
sz := self size - 1. |
|
1361 |
copy := self copyEmptyAndGrow:sz. |
|
1362 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
1363 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
1364 |
^ copy |
|
1365 |
||
1366 |
" |
|
1367 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
1368 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
1369 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
1370 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
1371 |
" |
|
1372 |
! |
|
1373 |
||
61 | 1374 |
copyWithoutIndex:omitIndex |
1375 |
"return a new collection consisting of receivers elements |
|
1376 |
without the argument stored at omitIndex" |
|
1377 |
||
1378 |
|copy sz| |
|
1379 |
||
202 | 1380 |
sz := self size - 1. |
252 | 1381 |
copy := self copyEmptyAndGrow:sz. |
293 | 1382 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
1383 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 1384 |
^ copy |
1385 |
||
95 | 1386 |
" |
1387 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
1388 |
'abcdefghijkl' copyWithoutIndex:5 |
|
1389 |
" |
|
1 | 1390 |
! |
1391 |
||
202 | 1392 |
copyWithoutLast:count |
1393 |
"return a new collection consisting of the receivers elements |
|
1394 |
except the last count elements." |
|
1395 |
||
1396 |
|sz| |
|
1397 |
||
1398 |
sz := self size. |
|
1399 |
^ self copyTo:(sz - count) |
|
1400 |
||
1401 |
" |
|
1402 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
|
1403 |
'1234567890' copyWithoutLast:4 |
|
1404 |
" |
|
602 | 1405 |
! ! |
1406 |
||
1407 |
!SequenceableCollection methodsFor:'enumerating'! |
|
1408 |
||
1409 |
collect:aBlock |
|
1410 |
"evaluate the argument, aBlock for every element in the collection |
|
1411 |
and return a collection of the results" |
|
1412 |
||
1413 |
|newCollection |
|
1414 |
sz "{ Class:SmallInteger }"| |
|
1415 |
||
1416 |
sz := self size. |
|
1417 |
newCollection := self copyEmptyAndGrow:sz. |
|
1418 |
1 to:sz do:[:index | |
|
1419 |
newCollection at:index put:(aBlock value:(self at:index)). |
|
1420 |
]. |
|
1421 |
^ newCollection |
|
1422 |
||
1423 |
" |
|
1424 |
#(one two three four five six) collect:[:element | element asUppercase] |
|
1425 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
1426 |
(1 to:9) collect:[:element | element * element] |
|
1427 |
" |
|
202 | 1428 |
! |
1429 |
||
602 | 1430 |
do:aBlock |
1431 |
"evaluate the argument, aBlock for every element in the collection." |
|
1432 |
||
1433 |
|stop "{ Class:SmallInteger }"| |
|
1434 |
||
1435 |
stop := self size. |
|
1436 |
1 to:stop do:[:index | |
|
1437 |
aBlock value:(self at:index). |
|
1438 |
] |
|
2927 | 1439 |
|
602 | 1440 |
" |
1422 | 1441 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 1442 |
" |
1443 |
! |
|
1444 |
||
2927 | 1445 |
do:aBlock separatedBy:sepBlock |
1446 |
"evaluate the argument, aBlock for every element in the collection. |
|
1447 |
Between each element (i.e. not before the first element and not after the |
|
1448 |
last element), evaluate sepBlock. |
|
1449 |
This supports printing of collections with elements separated by some |
|
1450 |
string." |
|
1451 |
||
1452 |
|stop "{ Class:SmallInteger }"| |
|
1453 |
||
1454 |
stop := self size. |
|
1455 |
stop < 1 ifTrue:[^ self]. |
|
1456 |
aBlock value:(self at:1). |
|
1457 |
2 to:stop do:[:index | |
|
1458 |
sepBlock value. |
|
1459 |
aBlock value:(self at:index) |
|
1460 |
]. |
|
1461 |
||
1462 |
" |
|
1463 |
#(one two three four five six) |
|
1464 |
do:[:each | Transcript show:each] |
|
1465 |
separatedBy:[Transcript show:' , '] |
|
1466 |
" |
|
1467 |
! |
|
3003 | 1468 |
|
3044 | 1469 |
doWithIndex:aBlock |
1470 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
1471 |
the index as second argument." |
|
1472 |
||
1473 |
self keysAndValuesDo:[:index :value | |
|
1474 |
aBlock value:value value:index |
|
1475 |
]. |
|
1476 |
||
1477 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
1478 |
! |
|
1479 |
||
602 | 1480 |
from:startIndex do:aBlock |
1481 |
"evaluate the argument, aBlock for the elements starting with the |
|
1482 |
element at startIndex to the end." |
|
1483 |
||
1484 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 1485 |
|
95 | 1486 |
" |
2927 | 1487 |
#(one two three four five six) |
1488 |
from:3 |
|
1489 |
do:[:element | Transcript showCR:element] |
|
602 | 1490 |
" |
1491 |
! |
|
1492 |
||
1493 |
from:start to:stop collect:aBlock |
|
1494 |
"evaluate the argument, aBlock for the elements indexed by start |
|
1495 |
to stop in the collection and return a collection of the results" |
|
1496 |
||
1497 |
|newCollection sz |
|
1498 |
idx "{ Class:SmallInteger }"| |
|
1499 |
||
1500 |
sz := stop - start + 1. |
|
1501 |
newCollection := self copyEmptyAndGrow:sz. |
|
1502 |
idx := 1. |
|
1503 |
start to:stop do:[:index | |
|
1504 |
newCollection at:idx put:(aBlock value:(self at:index)). |
|
1505 |
idx := idx + 1 |
|
1506 |
]. |
|
1507 |
^ newCollection |
|
1508 |
||
1509 |
" |
|
2927 | 1510 |
#(one two three four five six) |
1511 |
from:2 |
|
1512 |
to:4 |
|
1513 |
collect:[:element | element asUppercase] |
|
602 | 1514 |
" |
1515 |
! |
|
1516 |
||
1517 |
from:index1 to:index2 do:aBlock |
|
1518 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1519 |
index2 in the collection" |
|
1520 |
||
1521 |
|start "{ Class:SmallInteger }" |
|
1522 |
stop "{ Class:SmallInteger }" | |
|
1523 |
||
1524 |
start := index1. |
|
1525 |
stop := index2. |
|
1526 |
start to:stop do:[:index | |
|
1527 |
aBlock value:(self at:index). |
|
1528 |
] |
|
1529 |
||
1530 |
" |
|
2927 | 1531 |
#(one two three four five six) |
1532 |
from:3 |
|
1533 |
to:5 |
|
1534 |
do:[:element | Transcript showCR:element] |
|
602 | 1535 |
" |
1536 |
! |
|
1537 |
||
1538 |
from:index1 to:index2 reverseDo:aBlock |
|
1539 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
1540 |
index2 in the collection. Step in reverse order" |
|
1541 |
||
1542 |
|start "{ Class:SmallInteger }" |
|
1543 |
stop "{ Class:SmallInteger }" | |
|
1544 |
||
1545 |
start := index1. |
|
1546 |
stop := index2. |
|
1547 |
stop to:start by:-1 do:[:index | |
|
1548 |
aBlock value:(self at:index). |
|
1549 |
] |
|
1550 |
||
1551 |
" |
|
2927 | 1552 |
#(one two three four five six) |
1553 |
from:3 |
|
1554 |
to:5 |
|
1555 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 1556 |
" |
61 | 1557 |
! |
1558 |
||
602 | 1559 |
keysAndValuesDo:aTwoArgBlock |
1560 |
"evaluate the argument, aBlock for every element in the collection, |
|
1561 |
passing both index and element as arguments." |
|
1562 |
||
1563 |
|stop "{ Class:SmallInteger }"| |
|
1564 |
||
1565 |
stop := self size. |
|
1566 |
1 to:stop do:[:index | |
|
1567 |
aTwoArgBlock value:index value:(self at:index). |
|
1568 |
] |
|
1569 |
" |
|
2927 | 1570 |
#(one two three four five six) |
1571 |
keysAndValuesDo:[:key :element | |
|
1572 |
Transcript show:key; space; showCR:element |
|
1573 |
] |
|
602 | 1574 |
" |
1575 |
! |
|
1576 |
||
1359 | 1577 |
keysAndValuesReverseDo:aTwoArgBlock |
1578 |
"evaluate the argument, aBlock in reverse order, for every element |
|
1579 |
in the collection, passing both index and element as arguments." |
|
1580 |
||
1581 |
|stop "{ Class:SmallInteger }"| |
|
1582 |
||
1583 |
stop := self size. |
|
1584 |
stop to:1 by:-1 do:[:index | |
|
1585 |
aTwoArgBlock value:index value:(self at:index). |
|
1586 |
] |
|
1587 |
||
1588 |
" |
|
1589 |
#(one two three four five six) |
|
2927 | 1590 |
keysAndValuesReverseDo:[:key :element | |
1591 |
Transcript show:key; space; showCR:element |
|
1592 |
] |
|
1359 | 1593 |
" |
1594 |
||
1595 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
1596 |
! |
|
1597 |
||
602 | 1598 |
nonNilElementsDo:aBlock |
1599 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
1600 |
||
1601 |
|stop "{ Class:SmallInteger }" |
|
1602 |
element| |
|
1603 |
||
1604 |
stop := self size. |
|
1605 |
1 to:stop do:[:index | |
|
1606 |
(element := self at:index) notNil ifTrue:[ |
|
1607 |
aBlock value:element. |
|
1608 |
] |
|
1609 |
] |
|
1610 |
" |
|
1422 | 1611 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 1612 |
" |
1613 |
! |
|
1614 |
||
1615 |
pairWiseCollect:aBlock |
|
1616 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1617 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1618 |
An error will be reported, if the number of elements in the receiver |
|
1619 |
is not a multiple of 2. |
|
1620 |
Collect the results and return a new collection containing those." |
|
1621 |
||
1622 |
|stop newCollection dstIdx "{ Class:SmallInteger }"| |
|
1623 |
||
1624 |
stop := self size. |
|
1625 |
newCollection := self copyEmptyAndGrow:stop // 2. |
|
1626 |
dstIdx := 1. |
|
1627 |
1 to:stop by:2 do:[:index | |
|
1628 |
newCollection at:dstIdx put:(aBlock value:(self at:index) value:(self at:index+1)). |
|
1629 |
dstIdx := dstIdx + 1 |
|
1630 |
]. |
|
1631 |
^ newCollection |
|
44 | 1632 |
|
95 | 1633 |
" |
602 | 1634 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
1635 |
pairWiseCollect:[:num :sym | sym->num] |
|
1636 |
||
1637 |
||
1638 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1639 |
pairWiseCollect:[:x :y | x@y] |
|
1640 |
" |
|
1641 |
! |
|
1642 |
||
1643 |
pairWiseDo:aBlock |
|
1644 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
1645 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
1646 |
An error will be reported, if the number of elements in the receiver |
|
1647 |
is not a multiple of 2." |
|
1648 |
||
1649 |
|stop "{ Class:SmallInteger }"| |
|
1650 |
||
1651 |
stop := self size. |
|
1652 |
1 to:stop by:2 do:[:index | |
|
1653 |
aBlock value:(self at:index) value:(self at:index+1). |
|
1654 |
] |
|
1655 |
" |
|
1656 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
1422 | 1657 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
602 | 1658 |
|
1659 |
||
1660 |
#(1 1 1 2 1 3 1 4 1 5) |
|
1422 | 1661 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
95 | 1662 |
" |
61 | 1663 |
! |
1664 |
||
602 | 1665 |
reverseDo:aBlock |
1666 |
"evaluate the argument, aBlock for every element in the collection |
|
1667 |
in reverse order" |
|
1668 |
||
1669 |
|sz "{ Class:SmallInteger }"| |
|
1670 |
||
1671 |
sz := self size. |
|
1672 |
sz to:1 by:-1 do:[:index | |
|
1673 |
aBlock value:(self at:index). |
|
1674 |
] |
|
1675 |
||
1676 |
" |
|
1422 | 1677 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 1678 |
" |
1679 |
! |
|
1680 |
||
1681 |
select:aBlock |
|
1682 |
"evaluate the argument, aBlock for every element in the collection |
|
1683 |
and return a collection of all elements for which the block return |
|
1684 |
true" |
|
1685 |
||
1686 |
|element newColl species needCopy |
|
1687 |
sz "{ Class:SmallInteger }"| |
|
1688 |
||
1689 |
sz := self size. |
|
1690 |
species := self species. |
|
1691 |
species growIsCheap ifFalse:[ |
|
1692 |
newColl := OrderedCollection new:sz. |
|
1693 |
needCopy := true |
|
1694 |
] ifTrue:[ |
|
1695 |
newColl := self copyEmpty:sz. |
|
1696 |
needCopy := false |
|
1697 |
]. |
|
1698 |
1 to:sz do:[:index | |
|
1699 |
element := self at:index. |
|
1700 |
(aBlock value:element) ifTrue:[ |
|
1701 |
newColl add:element |
|
1702 |
]. |
|
1703 |
]. |
|
1704 |
needCopy ifTrue:[ |
|
1705 |
newColl := (species withAll:newColl) postCopyFrom:self |
|
1706 |
]. |
|
61 | 1707 |
^ newColl |
1708 |
||
95 | 1709 |
" |
602 | 1710 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
1711 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
1712 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
1713 |
(1 to:9) select:[:element | element odd] |
|
1714 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 1715 |
" |
186 | 1716 |
! |
1717 |
||
602 | 1718 |
with:aSequenceableCollection do:aTwoArgBlock |
1719 |
"evaluate the argument, aBlock for successive elements from |
|
1720 |
each the receiver and the argument, aSequenceableCollection. |
|
1721 |
The second argument, aBlock must be a two-argument block. |
|
1722 |
The collection argument must implement access via a numeric key." |
|
1723 |
||
1724 |
|stop "{ Class: SmallInteger }" | |
|
1725 |
||
1726 |
stop := self size. |
|
1727 |
1 to:stop do:[:index | |
|
1728 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
|
1729 |
] |
|
186 | 1730 |
" |
1422 | 1731 |
#(one two three four five six) with:(1 to:10) do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
186 | 1732 |
" |
1 | 1733 |
! ! |
1734 |
||
1735 |
!SequenceableCollection methodsFor:'filling and replacing'! |
|
1736 |
||
602 | 1737 |
atAllPut:anObject |
1738 |
"replace all elements of the collection by the argument, anObject. |
|
1739 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1740 |
therefore the change may affect all others referencing the receiver." |
|
1741 |
||
1742 |
self from:1 to:(self size) put:anObject |
|
1743 |
||
1744 |
" |
|
1745 |
(Array new:10) atAllPut:1 |
|
1746 |
(String new:10) atAllPut:$a |
|
1747 |
" |
|
1748 |
! |
|
1749 |
||
1 | 1750 |
from:index1 to:index2 put:anObject |
1751 |
"replace the elements from index1 to index2 of the collection |
|
44 | 1752 |
by the argument, anObject. |
1753 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1754 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1755 |
|
1756 |
|index "{ Class: SmallInteger }" |
|
1757 |
end "{ Class: SmallInteger }"| |
|
1758 |
||
1759 |
index := index1. |
|
1760 |
end := index2. |
|
1761 |
[index <= end] whileTrue:[ |
|
159 | 1762 |
self at:index put:anObject. |
1763 |
index := index + 1 |
|
1 | 1764 |
] |
44 | 1765 |
|
95 | 1766 |
" |
1767 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
|
1768 |
'abcdefghijkl' from:3 to:6 put:$X |
|
1769 |
" |
|
1 | 1770 |
! |
1771 |
||
3003 | 1772 |
replaceAll:oldObject by:newObject |
1773 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
1774 |
Pleace do no longer use this one; |
|
1775 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
1776 |
||
1777 |
"/ self obsoleteMethodWarning:'use #replaceAll:with:'. |
|
1778 |
^ self replaceAll:oldObject with:newObject |
|
1779 |
||
1780 |
"Modified: 23.9.1997 / 02:17:49 / cg" |
|
1781 |
! |
|
1782 |
||
1783 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
1784 |
"backward ST/X compatibility: an alias for #replaceAll:with:from:to:. |
|
1785 |
Pleace do no longer use this one; |
|
1786 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
|
1787 |
||
1788 |
^ self replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
|
1789 |
! |
|
1790 |
||
2927 | 1791 |
replaceAll:oldObject with:newObject |
44 | 1792 |
"replace all oldObjects by newObject in the receiver. |
1793 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1794 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1795 |
|
2980 | 1796 |
^ self replaceAll:oldObject with:newObject from:1 to:self size |
362 | 1797 |
|
1798 |
" |
|
2980 | 1799 |
'123123abc123' replaceAll:$1 with:$* |
1800 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 1801 |
" |
1802 |
! |
|
293 | 1803 |
|
2927 | 1804 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 1805 |
"replace all oldObjects found between startIndex and endIndex, |
1806 |
by newObject in the receiver. |
|
1807 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1808 |
therefore the change may affect all others referencing the receiver." |
|
1809 |
||
1810 |
|start "{ Class: SmallInteger }" |
|
1811 |
stop "{ Class: SmallInteger }"| |
|
1812 |
||
1813 |
start := startIndex. |
|
1814 |
stop := stopIndex. |
|
1815 |
startIndex to:stopIndex do:[:index | |
|
159 | 1816 |
(self at:index) = oldObject ifTrue:[ |
1817 |
self at:index put:newObject |
|
1818 |
] |
|
1 | 1819 |
] |
44 | 1820 |
|
95 | 1821 |
" |
2980 | 1822 |
'123123abc123' replaceAll:$1 with:$* |
1823 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
1824 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 1825 |
" |
1826 |
! |
|
1827 |
||
3003 | 1828 |
replaceAny:aCollection by:newObject |
1829 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 1830 |
Pleace do no longer use this one; |
3003 | 1831 |
instead use #replaceAny:with: for naming consistence." |
1832 |
||
1833 |
"/ self obsoleteMethodWarning:'use #replaceAny:with:'. |
|
1834 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
|
1835 |
||
1836 |
" |
|
1837 |
'123123abc123' replaceAny:#($1 $2) with:$* |
|
1838 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
1839 |
" |
|
1840 |
||
1841 |
"Modified: 23.9.1997 / 02:17:53 / cg" |
|
602 | 1842 |
! |
1843 |
||
3003 | 1844 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
1845 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 1846 |
Pleace do no longer use this one; |
3003 | 1847 |
instead use #replaceAny:with:from:to: for naming consistence." |
1848 |
||
1849 |
"/ self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
|
1850 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
|
1851 |
||
1852 |
" |
|
1853 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
|
1854 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
1855 |
" |
|
1856 |
||
1857 |
"Modified: 23.9.1997 / 02:17:58 / cg" |
|
2927 | 1858 |
! |
1859 |
||
1860 |
replaceAny:aCollection with:newObject |
|
1861 |
"replace all elements, which are in aCollection, by newObject. |
|
1862 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1863 |
therefore the change may affect all others referencing the receiver." |
|
1864 |
||
1865 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
|
602 | 1866 |
! |
1867 |
||
2927 | 1868 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 1869 |
"replace all elements within a range, |
1870 |
which are in contained in aCollection by newObject. |
|
1871 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1872 |
therefore the change may affect all others referencing the receiver." |
|
1873 |
||
1874 |
|start "{ Class: SmallInteger }" |
|
1875 |
stop "{ Class: SmallInteger }"| |
|
1876 |
||
1877 |
start := startIndex. |
|
1878 |
stop := stopIndex. |
|
1879 |
startIndex to:stopIndex do:[:index | |
|
159 | 1880 |
(aCollection includes:(self at:index)) ifTrue:[ |
1881 |
self at:index put:newObject |
|
1882 |
] |
|
95 | 1883 |
] |
1884 |
||
1885 |
" |
|
2927 | 1886 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
1887 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 1888 |
" |
1 | 1889 |
! |
1890 |
||
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1891 |
replaceFrom:start count:numberOfElements with:replacementCollection |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1892 |
"replace numberOfElements elements in the receiver from index start, |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1893 |
with elements taken from replacementCollection starting at 1. |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1894 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1895 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1896 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1897 |
^ self replaceFrom:start |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1898 |
to:(start + numberOfElements - 1) |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1899 |
with:replacementCollection |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1900 |
startingAt:1 |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1901 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1902 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1903 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1904 |
#($a $b $c $d $e) replaceFrom:2 count:2 with:'12345' |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1905 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1906 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1907 |
"Modified: 20.2.1997 / 14:24:38 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1908 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1909 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1910 |
replaceFrom:start count:numberOfElementsToReplace with:replacementCollection startingAt:repStart |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1911 |
"replace numberOfElementsToReplace elements in the receiver from index start, |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1912 |
with elements taken from replacementCollection starting at repStart. |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1913 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1914 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1915 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1916 |
^ self |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1917 |
replaceFrom:start |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1918 |
to:(start + numberOfElementsToReplace - 1) |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1919 |
with:replacementCollection |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1920 |
startingAt:repStart |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1921 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1922 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1923 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1924 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1925 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 count:3 with:'12345' startingAt:4 |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1926 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1927 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1928 |
"Modified: 20.2.1997 / 14:26:12 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1929 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
1930 |
|
1 | 1931 |
replaceFrom:start to:stop with:replacementCollection |
44 | 1932 |
"replace elements in the receiver between index start and stop, |
1933 |
with elements taken from replacementCollection starting at 1. |
|
1934 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1935 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1936 |
|
1937 |
^ self replaceFrom:start |
|
159 | 1938 |
to:stop |
1939 |
with:replacementCollection |
|
1940 |
startingAt:1 |
|
61 | 1941 |
|
95 | 1942 |
" |
1943 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
1944 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
1945 |
" |
|
1 | 1946 |
! |
1947 |
||
1948 |
replaceFrom:start to:stop with:replacementCollection startingAt:repStart |
|
44 | 1949 |
"replace elements in the receiver between index start and stop, |
1950 |
with elements taken from replacementCollection starting at repStart. |
|
1951 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1952 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1953 |
|
1954 |
|srcIndex "{ Class: SmallInteger }" |
|
1955 |
dstIndex "{ Class: SmallInteger }" |
|
1956 |
end "{ Class: SmallInteger }" | |
|
1957 |
||
293 | 1958 |
(repStart < start) ifTrue:[ |
1959 |
" must do reverse copy " |
|
1960 |
srcIndex := repStart + (stop - start). |
|
1961 |
dstIndex := stop. |
|
1962 |
end := start. |
|
1963 |
[dstIndex >= end] whileTrue:[ |
|
1964 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
1965 |
srcIndex := srcIndex - 1. |
|
1966 |
dstIndex := dstIndex - 1 |
|
1967 |
]. |
|
1968 |
^ self |
|
1 | 1969 |
]. |
1970 |
||
1971 |
srcIndex := repStart. |
|
1972 |
dstIndex := start. |
|
1973 |
end := stop. |
|
1974 |
[dstIndex <= end] whileTrue:[ |
|
159 | 1975 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
1976 |
srcIndex := srcIndex + 1. |
|
1977 |
dstIndex := dstIndex + 1 |
|
1 | 1978 |
] |
61 | 1979 |
|
95 | 1980 |
" |
282 | 1981 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
1982 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1983 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
1984 |
||
1985 |
|c| |
|
1986 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1987 |
c replaceFrom:2 to:3 with:c startingAt:4 |
|
1988 |
||
1989 |
|c| |
|
1990 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
1991 |
c replaceFrom:4 to:5 with:c startingAt:2 |
|
95 | 1992 |
" |
61 | 1993 |
! |
1994 |
||
602 | 1995 |
replaceFrom:start with:replacementCollection |
1996 |
"replace elements in the receiver starting at start, |
|
1997 |
with elements taken from replacementCollection starting at 1 |
|
1998 |
to the end of replacementCollection. |
|
1999 |
Notice: This operation modifies the receiver, NOT a copy; |
|
2000 |
therefore the change may affect all others referencing the receiver." |
|
2001 |
||
2002 |
^ self replaceFrom:start |
|
2003 |
to:(start + replacementCollection size - 1) |
|
2004 |
with:replacementCollection |
|
2005 |
startingAt:1 |
|
2006 |
||
2007 |
" |
|
2008 |
'1234567890' replaceFrom:5 with:'abc' |
|
2009 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
2010 |
" |
|
2011 |
! |
|
2012 |
||
2013 |
replaceFrom:start with:replacementCollection startingAt:offset |
|
2014 |
"replace elements in the receiver starting at start, |
|
2015 |
with elements taken from replacementCollection starting at offset |
|
2016 |
to the end of replacementCollection. |
|
2017 |
Notice: This operation modifies the receiver, NOT a copy; |
|
2018 |
therefore the change may affect all others referencing the receiver." |
|
2019 |
||
2020 |
^ self replaceFrom:start |
|
2021 |
to:(start + replacementCollection size - offset) |
|
2022 |
with:replacementCollection |
|
2023 |
startingAt:offset |
|
2024 |
||
2025 |
" |
|
2026 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
|
2027 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
|
2028 |
" |
|
2029 |
! |
|
2030 |
||
61 | 2031 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
2032 |
"replace elements in destColl with elements from the receiver. |
|
2033 |
Notice: This operation modifies the destination collection, NOT a copy; |
|
2034 |
therefore the change may affect all others referencing this object." |
|
2035 |
||
2036 |
destColl replaceFrom:destStartIndex to:destEndIndex with:self startingAt:sourceStart |
|
2037 |
||
2038 |
" |
|
2039 |
|s| |
|
2040 |
s := 'abcdefghijklmnop'. |
|
2041 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
2042 |
s'123defghijklmnop' |
|
2043 |
" |
|
1 | 2044 |
! ! |
2045 |
||
2069 | 2046 |
!SequenceableCollection methodsFor:'private sorting helpers'! |
2047 |
||
2048 |
quickSortFrom:inBegin to:inEnd |
|
2049 |
"actual quicksort worker for sort-message" |
|
2050 |
||
2051 |
|begin "{ Class: SmallInteger }" |
|
2052 |
end "{ Class: SmallInteger }" |
|
2053 |
b "{ Class: SmallInteger }" |
|
2054 |
e "{ Class: SmallInteger }" |
|
2055 |
middleElement temp1 temp2 | |
|
2056 |
||
2057 |
begin := inBegin. "/ this also does a type-check |
|
2058 |
end := inEnd. |
|
2059 |
||
2060 |
b := begin. |
|
2061 |
e := end. |
|
2062 |
middleElement := self at:((b + e) // 2). |
|
2063 |
||
2064 |
[b < e] whileTrue:[ |
|
2065 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
2066 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
2067 |
||
2068 |
(b <= e) ifTrue:[ |
|
2069 |
(b == e) ifFalse:[ |
|
2070 |
temp1 := self at:b. temp2 := self at:e. |
|
2071 |
self at:b put:temp2. self at:e put:temp1 |
|
2072 |
]. |
|
2073 |
b := b + 1. |
|
2074 |
e := e - 1 |
|
2075 |
] |
|
2076 |
]. |
|
2077 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e]. |
|
2078 |
(b < end) ifTrue:[self quickSortFrom:b to:end] |
|
2079 |
! |
|
2080 |
||
2081 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
|
2082 |
"actual quicksort worker for sort:-message" |
|
2083 |
||
2084 |
|begin "{ Class: SmallInteger }" |
|
2085 |
end "{ Class: SmallInteger }" |
|
2086 |
b "{ Class: SmallInteger }" |
|
2087 |
e "{ Class: SmallInteger }" |
|
2088 |
middleElement temp1 temp2 | |
|
2089 |
||
2090 |
begin := inBegin. "/ this also does a type-check |
|
2091 |
end := inEnd. |
|
2092 |
||
2093 |
b := begin. |
|
2094 |
e := end. |
|
2095 |
middleElement := self at:((b + e) // 2). |
|
2096 |
||
2097 |
[b < e] whileTrue:[ |
|
2098 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
|
2099 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
2100 |
||
2101 |
(b <= e) ifTrue:[ |
|
2102 |
(b == e) ifFalse:[ |
|
2103 |
temp1 := self at:b. temp2 := self at:e. |
|
2104 |
self at:b put:temp2. self at:e put:temp1 |
|
2105 |
]. |
|
2106 |
b := b + 1. |
|
2107 |
e := e - 1 |
|
2108 |
] |
|
2109 |
]. |
|
2110 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock]. |
|
2111 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock] |
|
2112 |
! |
|
2113 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2114 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock policy:p |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2115 |
"actual quicksort worker for sort:-message" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2116 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2117 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2118 |
end "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2119 |
b "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2120 |
e "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2121 |
middleElement temp1 temp2 | |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2122 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2123 |
begin := inBegin. "/ this also does a type-check |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2124 |
end := inEnd. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2125 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2126 |
b := begin. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2127 |
e := end. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2128 |
middleElement := self at:((b + e) // 2). |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2129 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2130 |
[b < e] whileTrue:[ |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2131 |
[b < end and:[sortBlock value:p value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2132 |
[e > begin and:[sortBlock value:p value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2133 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2134 |
(b <= e) ifTrue:[ |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2135 |
(b == e) ifFalse:[ |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2136 |
temp1 := self at:b. temp2 := self at:e. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2137 |
self at:b put:temp2. self at:e put:temp1 |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2138 |
]. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2139 |
b := b + 1. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2140 |
e := e - 1 |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2141 |
] |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2142 |
]. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2143 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock policy:p]. |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2144 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock policy:p] |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2145 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2146 |
"Created: / 27.10.1997 / 19:57:12 / cg" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2147 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
2148 |
|
2069 | 2149 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
2150 |
"actual quicksort worker for sort:with:-message" |
|
2151 |
||
2152 |
|begin "{ Class: SmallInteger }" |
|
2153 |
end "{ Class: SmallInteger }" |
|
2154 |
b "{ Class: SmallInteger }" |
|
2155 |
e "{ Class: SmallInteger }" |
|
2156 |
middleElement temp1 temp2 | |
|
2157 |
||
2158 |
begin := inBegin. "/ this also does a type-check |
|
2159 |
end := inEnd. |
|
2160 |
||
2161 |
b := begin. |
|
2162 |
e := end. |
|
2163 |
middleElement := self at:((b + e) // 2). |
|
2164 |
||
2165 |
[b < e] whileTrue:[ |
|
2166 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
|
2167 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
2168 |
||
2169 |
(b <= e) ifTrue:[ |
|
2170 |
(b == e) ifFalse:[ |
|
2171 |
temp1 := self at:b. temp2 := self at:e. |
|
2172 |
self at:b put:temp2. self at:e put:temp1. |
|
2173 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
2174 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
2175 |
]. |
|
2176 |
b := b + 1. |
|
2177 |
e := e - 1 |
|
2178 |
] |
|
2179 |
]. |
|
2180 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
|
2181 |
(b < end) ifTrue:[self quickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
|
2182 |
||
2183 |
"Modified: 5.1.1997 / 18:12:36 / cg" |
|
2184 |
! |
|
2185 |
||
2186 |
quickSortFrom:inBegin to:inEnd with:aCollection |
|
2187 |
"actual quicksort worker for sortWith-message" |
|
2188 |
||
2189 |
|begin "{ Class: SmallInteger }" |
|
2190 |
end "{ Class: SmallInteger }" |
|
2191 |
b "{ Class: SmallInteger }" |
|
2192 |
e "{ Class: SmallInteger }" |
|
2193 |
middleElement temp1 temp2 | |
|
2194 |
||
2195 |
begin := inBegin. "/ this also does a type-check |
|
2196 |
end := inEnd. |
|
2197 |
||
2198 |
b := begin. |
|
2199 |
e := end. |
|
2200 |
middleElement := self at:((b + e) // 2). |
|
2201 |
||
2202 |
[b < e] whileTrue:[ |
|
2203 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
2204 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
2205 |
||
2206 |
(b <= e) ifTrue:[ |
|
2207 |
(b == e) ifFalse:[ |
|
2208 |
temp1 := self at:b. temp2 := self at:e. |
|
2209 |
self at:b put:temp2. self at:e put:temp1. |
|
2210 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
2211 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
2212 |
]. |
|
2213 |
b := b + 1. |
|
2214 |
e := e - 1 |
|
2215 |
] |
|
2216 |
]. |
|
2217 |
(begin < e) ifTrue:[self quickSortFrom:begin to:e with:aCollection]. |
|
2218 |
(b < end) ifTrue:[self quickSortFrom:b to:end with:aCollection] |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2219 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2220 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2221 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2222 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2223 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2224 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2225 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2226 |
Notice, that this method has a much worse best- and average case |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2227 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2228 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2229 |
element lies at either end of the partition, so that a split |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2230 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2231 |
Since the middle element is choosen, this worst case is very unlikely |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2232 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2233 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2234 |
|begin "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2235 |
end "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2236 |
b "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2237 |
e "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2238 |
rnd "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2239 |
middleElement temp1 temp2 | |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2240 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2241 |
begin := inBegin. "/ this also does a type-check |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2242 |
end := inEnd. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2243 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2244 |
b := begin. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2245 |
e := end. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2246 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2247 |
"/ randomize that partition |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2248 |
"/ by exchanging the first element with any random |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2249 |
"/ element. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2250 |
rnd := Random nextIntegerBetween:b and:e. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2251 |
temp1 := self at:b. temp2 := self at:rnd. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2252 |
self at:b put:temp2. self at:rnd put:temp1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2253 |
aCollection notNil ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2254 |
temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2255 |
aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2256 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2257 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2258 |
"/ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2259 |
"/ now proceed as usual |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2260 |
"/ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2261 |
middleElement := self at:((b + e) // 2). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2262 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2263 |
[b < e] whileTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2264 |
[b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2265 |
[e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2266 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2267 |
(b <= e) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2268 |
(b == e) ifFalse:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2269 |
temp1 := self at:b. temp2 := self at:e. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2270 |
self at:b put:temp2. self at:e put:temp1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2271 |
aCollection notNil ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2272 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2273 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2274 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2275 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2276 |
b := b + 1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2277 |
e := e - 1 |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2278 |
] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2279 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2280 |
(begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2281 |
(b < end) ifTrue:[self randomizedQuickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2282 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2283 |
"Created: 21.8.1997 / 18:13:35 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2284 |
"Modified: 21.8.1997 / 18:34:31 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2285 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2286 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2287 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2288 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2289 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2290 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2291 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2292 |
Notice, that this method has a much worse best- and average case |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2293 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2294 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2295 |
element lies at either end of the partition, so that a split |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2296 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2297 |
Since the middle element is choosen, this worst case is very unlikely |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2298 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2299 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2300 |
|begin "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2301 |
end "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2302 |
b "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2303 |
e "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2304 |
rnd "{ Class: SmallInteger }" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2305 |
middleElement temp1 temp2 | |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2306 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2307 |
begin := inBegin. "/ this also does a type-check |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2308 |
end := inEnd. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2309 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2310 |
b := begin. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2311 |
e := end. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2312 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2313 |
"/ randomize that partition |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2314 |
"/ by exchanging the first element with any random |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2315 |
"/ element. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2316 |
rnd := Random nextIntegerBetween:b and:e. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2317 |
temp1 := self at:b. temp2 := self at:rnd. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2318 |
self at:b put:temp2. self at:rnd put:temp1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2319 |
aCollection notNil ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2320 |
temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2321 |
aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2322 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2323 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2324 |
"/ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2325 |
"/ now proceed as usual |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2326 |
"/ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2327 |
middleElement := self at:((b + e) // 2). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2328 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2329 |
[b < e] whileTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2330 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2331 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2332 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2333 |
(b <= e) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2334 |
(b == e) ifFalse:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2335 |
temp1 := self at:b. temp2 := self at:e. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2336 |
self at:b put:temp2. self at:e put:temp1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2337 |
aCollection notNil ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2338 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2339 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2340 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2341 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2342 |
b := b + 1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2343 |
e := e - 1 |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2344 |
] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2345 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2346 |
(begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e with:aCollection]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2347 |
(b < end) ifTrue:[self randomizedQuickSortFrom:b to:end with:aCollection] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2348 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2349 |
"Modified: 21.8.1997 / 18:30:19 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
2350 |
"Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 2351 |
! ! |
2352 |
||
602 | 2353 |
!SequenceableCollection methodsFor:'queries'! |
2354 |
||
2355 |
firstIndex |
|
2356 |
"return the first elements index" |
|
2357 |
||
2358 |
^ 1 |
|
2359 |
! |
|
2360 |
||
2361 |
isSequenceable |
|
2362 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
2363 |
||
2364 |
^ true |
|
1 | 2365 |
! |
2366 |
||
602 | 2367 |
isSequenceableCollection |
2368 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
2369 |
This method is a historic leftover and will be removed." |
|
2370 |
||
2371 |
^ true |
|
1 | 2372 |
! |
2373 |
||
602 | 2374 |
keys |
2375 |
"return a collection with all keys in the Smalltalk dictionary" |
|
2376 |
||
2377 |
|sz| |
|
2378 |
||
1 | 2379 |
sz := self size. |
602 | 2380 |
sz == 0 ifTrue:[ |
2381 |
^ #() |
|
61 | 2382 |
]. |
602 | 2383 |
^ 1 to:sz |
1 | 2384 |
! |
2385 |
||
602 | 2386 |
lastIndex |
2387 |
"return the last elements index" |
|
2388 |
||
2389 |
^ self size |
|
61 | 2390 |
! |
2391 |
||
602 | 2392 |
size |
2393 |
"return the number of elements in the collection. |
|
2394 |
concrete implementations must define this" |
|
2395 |
||
2396 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2397 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2398 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2399 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2400 |
"return the index value which is returned when nothing |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2401 |
is found in an indexOf* kind of search. |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2402 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2403 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2404 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2405 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
2406 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 2407 |
! ! |
2408 |
||
2409 |
!SequenceableCollection methodsFor:'searching'! |
|
2410 |
||
2411 |
detect:aBlock ifNone:exceptionBlock |
|
2412 |
"find the first element, for which evaluation of the argument, aBlock |
|
2413 |
return true; if none does so, return the evaluation of exceptionBlock |
|
2414 |
||
2415 |
reimplemented here for speed" |
|
2416 |
||
32 | 2417 |
|stop "{ Class: SmallInteger }" |
1 | 2418 |
element| |
2419 |
||
32 | 2420 |
stop := self size. |
2421 |
1 to:stop do:[:index | |
|
159 | 2422 |
element := self at:index. |
2423 |
(aBlock value:element) ifTrue:[ |
|
2424 |
^ element |
|
2425 |
]. |
|
1 | 2426 |
]. |
2427 |
^ exceptionBlock value |
|
159 | 2428 |
|
2429 |
" |
|
2430 |
#(11 12 13 14) detect:[:n | n odd] ifNone:['sorry'] |
|
2431 |
#(12 14 16 18) detect:[:n | n odd] ifNone:['sorry'] |
|
2432 |
" |
|
1 | 2433 |
! |
2434 |
||
602 | 2435 |
findFirst:aBlock |
2436 |
"find the first element, for which evaluation of the argument, aBlock |
|
2437 |
returns true; return its index or 0 if none detected." |
|
2438 |
||
2439 |
|stop "{ Class: SmallInteger }" | |
|
2440 |
||
32 | 2441 |
stop := self size. |
602 | 2442 |
1 to:stop do:[:index | |
2443 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
1 | 2444 |
]. |
2445 |
^ 0 |
|
159 | 2446 |
|
95 | 2447 |
" |
602 | 2448 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
2449 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
2450 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
|
2451 |
'one.two.three' findFirst:[:c | (c == $.)] |
|
95 | 2452 |
" |
1 | 2453 |
! |
2454 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2455 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2456 |
"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
|
2457 |
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
|
2458 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2459 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2460 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2461 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2462 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2463 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2464 |
start to:stop do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2465 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2466 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2467 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2468 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2469 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2470 |
#(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
|
2471 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2472 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2473 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2474 |
|
602 | 2475 |
findLast:aBlock |
2476 |
"find the last element, for which evaluation of the argument, aBlock |
|
2477 |
returns true; return its index or 0 if none detected." |
|
2478 |
||
2479 |
|start "{ Class: SmallInteger }"| |
|
2480 |
||
2481 |
start := self size. |
|
2482 |
start to:1 by:-1 do:[:index | |
|
2483 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
|
252 | 2484 |
]. |
2485 |
^ 0 |
|
2486 |
||
2487 |
" |
|
602 | 2488 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
2489 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 2490 |
" |
2491 |
! |
|
2492 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2493 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2494 |
"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
|
2495 |
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
|
2496 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2497 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2498 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2499 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2500 |
start to:1 by:-1 do:[:index | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2501 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2502 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2503 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2504 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2505 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2506 |
#(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
|
2507 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2508 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2509 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
2510 |
|
1 | 2511 |
identityIndexOf:anElement |
2512 |
"search the collection for anElement using identity compare (i.e. ==); |
|
2513 |
if found, return the index otherwise return 0." |
|
2514 |
||
32 | 2515 |
^ self identityIndexOf:anElement startingAt:1 |
44 | 2516 |
|
95 | 2517 |
" |
159 | 2518 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
2519 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 |
|
2520 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
360 | 2521 |
|
2522 |
be careful: |
|
2523 |
||
2524 |
#(10 20 30 40.0 50 60 70) indexOf:40.0 |
|
2525 |
#(10 20 30 40.0 50 60 70) identityIndexOf:40.0 |
|
2526 |
||
95 | 2527 |
" |
1 | 2528 |
! |
2529 |
||
2530 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
|
2531 |
"search the collection for anElement using identity compare (i.e. ==); |
|
2532 |
if found, return the index otherwise return the value of the |
|
2533 |
exceptionBlock." |
|
2534 |
||
2535 |
|index| |
|
2536 |
||
32 | 2537 |
index := self identityIndexOf:anElement startingAt:1. |
1 | 2538 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
2539 |
^ index |
|
159 | 2540 |
|
2541 |
" |
|
2542 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
2543 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
2544 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
2545 |
#(10 20 30 40 50 60 70) indexOf:40.0 ifAbsent:['none'] |
|
2546 |
" |
|
1 | 2547 |
! |
2548 |
||
2549 |
identityIndexOf:anElement startingAt:start |
|
159 | 2550 |
"search the collection for anElement, starting search at index start |
1 | 2551 |
using identity compare (i.e. ==); |
2552 |
if found, return the index otherwise return 0." |
|
2553 |
||
32 | 2554 |
|startIndex "{ Class: SmallInteger }" |
2555 |
stop "{ Class: SmallInteger }" | |
|
1 | 2556 |
|
32 | 2557 |
startIndex := start. |
2558 |
stop := self size. |
|
2559 |
startIndex to:stop do:[:index | |
|
159 | 2560 |
anElement == (self at:index) ifTrue:[^ index]. |
1 | 2561 |
]. |
2562 |
^ 0 |
|
44 | 2563 |
|
95 | 2564 |
" |
159 | 2565 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40 startingAt:5 |
2566 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
|
2567 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
95 | 2568 |
" |
1 | 2569 |
! |
2570 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2571 |
identityIndexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2572 |
"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
|
2573 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2574 |
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
|
2575 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2576 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2577 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2578 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2579 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2580 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2581 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2582 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2583 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2584 |
anElement == (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2585 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2586 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2587 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2588 |
"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
|
2589 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2590 |
|
1 | 2591 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
159 | 2592 |
"search the collection for anElement, starting search at index start; |
1 | 2593 |
if found, return the index otherwise return the value of the |
2594 |
exceptionBlock. |
|
2595 |
This one searches for identical objects (i.e. ==)." |
|
2596 |
||
2597 |
|index| |
|
2598 |
||
2599 |
index := self identityIndexOf:anElement startingAt:start. |
|
2600 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2601 |
^ index |
|
159 | 2602 |
|
2603 |
" |
|
2604 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
2605 |
#(10 20 30 40 10) identityIndexOf:35 startingAt:3 ifAbsent:['none'] |
|
2606 |
" |
|
2607 |
! |
|
2608 |
||
602 | 2609 |
includes:anElement |
2610 |
"return true if the collection contains anElement; false otherwise. |
|
2611 |
Comparison is done using equality compare (i.e. =). |
|
2612 |
See #includesIdentical: if identity is asked for." |
|
2613 |
||
2614 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
2615 |
^ true |
|
2616 |
||
2617 |
" |
|
2618 |
#(10 20 30 40 50 60 70) includes:99 |
|
2619 |
#(10 20 30 40 50 60 70) includes:40 |
|
2620 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
2621 |
" |
|
2622 |
! |
|
2623 |
||
2624 |
includesIdentical:anElement |
|
2625 |
"return true if the collection contains anElement; false otherwise. |
|
2626 |
Comparison is done using identity compare (i.e. ==). |
|
2627 |
See #includes: if equality is asked for." |
|
2628 |
||
2629 |
((self identityIndexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
|
2630 |
^ true |
|
2631 |
||
2632 |
" |
|
2633 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
2634 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
2635 |
#(10 20 30 40 50 60 70) includes:40 |
|
2636 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
2637 |
||
2638 |
be careful: |
|
2639 |
||
2640 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
|
2641 |
#(10 20 30 40.0 50 60 70) includesIdentical:40.0 |
|
2642 |
" |
|
2643 |
! |
|
2644 |
||
2645 |
indexOf:anElement |
|
2646 |
"search the collection for anElement; |
|
2647 |
if found, return the index otherwise return 0. |
|
2648 |
The comparison is done using = |
|
2649 |
(i.e. equality test - not identity test)." |
|
2650 |
||
2651 |
^ self indexOf:anElement startingAt:1 |
|
2652 |
||
2653 |
" |
|
2654 |
#(10 20 30 40 50 60 70) indexOf:40 |
|
2655 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
|
2656 |
" |
|
2657 |
! |
|
2658 |
||
2659 |
indexOf:anElement ifAbsent:exceptionBlock |
|
2660 |
"search the collection for anElement; |
|
2661 |
if found, return the index otherwise return the value of the |
|
2662 |
exceptionBlock. |
|
2663 |
The comparison is done using = |
|
2664 |
(i.e. equality test - not identity test)." |
|
2665 |
||
2666 |
|index| |
|
2667 |
||
2668 |
index := self indexOf:anElement startingAt:1. |
|
2669 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2670 |
^ index |
|
2671 |
||
2672 |
" |
|
2673 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
2674 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
2675 |
#(10 20 30 40 10 20 30 40) indexOf:35 ifAbsent:['none'] |
|
2676 |
" |
|
2677 |
! |
|
2678 |
||
2679 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
|
2680 |
"search for the first occurence of elementToFind starting at start, |
|
2681 |
stopping the search at stop. If found, replace the element by replacement |
|
2682 |
and return the index. |
|
2683 |
If not found, return 0." |
|
2684 |
||
2685 |
|idx| |
|
2686 |
||
2687 |
idx := self indexOf:elementToFind startingAt:start. |
|
2688 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
|
2689 |
self at:idx put:replacement. |
|
2690 |
^ idx |
|
2691 |
]. |
|
2692 |
^ 0 |
|
2693 |
||
2694 |
" |
|
2695 |
|a| |
|
2696 |
||
2697 |
a := #(10 20 30 40 50 60 70). |
|
2698 |
(a indexOf:30 replaceWith:nil startingAt:1 stoppingAt:7) printNL. |
|
2699 |
a printNL. |
|
2700 |
" |
|
2701 |
! |
|
2702 |
||
2703 |
indexOf:anElement startingAt:start |
|
2704 |
"search the collection for anElement, starting the search at index start; |
|
2705 |
if found, return the index otherwise return 0. |
|
2706 |
The comparison is done using = |
|
2707 |
(i.e. equality test - not identity test)." |
|
2708 |
||
2709 |
|startIndex "{ Class: SmallInteger }" |
|
2710 |
stop "{ Class: SmallInteger }" | |
|
2711 |
||
2712 |
startIndex := start. |
|
2713 |
stop := self size. |
|
2714 |
startIndex to:stop do:[:index | |
|
2715 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2716 |
]. |
|
2717 |
^ 0 |
|
2718 |
||
2719 |
" |
|
2720 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
2721 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
2722 |
" |
|
2723 |
! |
|
2724 |
||
1159
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2725 |
indexOf:anElement startingAt:start endingAt:stop |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2726 |
"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
|
2727 |
ending at stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2728 |
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
|
2729 |
The comparison is done using = |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2730 |
(i.e. equality test - not identity test)." |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2731 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2732 |
|startIndex "{ Class: SmallInteger }" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2733 |
stopIndex "{ Class: SmallInteger }" | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2734 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2735 |
startIndex := start. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2736 |
stopIndex := self size min:stop. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2737 |
startIndex to:stopIndex do:[:index | |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2738 |
anElement = (self at:index) ifTrue:[^ index]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2739 |
]. |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2740 |
^ 0 |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2741 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2742 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2743 |
#(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
|
2744 |
#(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
|
2745 |
#(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
|
2746 |
#(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
|
2747 |
" |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2748 |
|
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2749 |
"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
|
2750 |
! |
dcbb3e070a05
added fallback methods #indexOf:startingAt:endingAt: / #identity...
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
2751 |
|
602 | 2752 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
2753 |
"search the collection for anElement starting the search at index start; |
|
2754 |
if found, return the index otherwise return the value of the |
|
2755 |
exceptionBlock. |
|
2756 |
The comparison is done using = |
|
2757 |
(i.e. equality test - not identity test)." |
|
2758 |
||
2759 |
|index| |
|
2760 |
||
2761 |
index := self indexOf:anElement startingAt:start. |
|
2762 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2763 |
^ index |
|
2764 |
||
2765 |
" |
|
2766 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
2767 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
2768 |
#(10 20 30 40 10 20 30 40) indexOf:35 startingAt:5 ifAbsent:['none'] |
|
2769 |
" |
|
2770 |
! |
|
2771 |
||
159 | 2772 |
indexOfAny:aCollection |
2773 |
"search the collection for an element in aCollection. |
|
2774 |
if found, return the index otherwise return 0. |
|
2775 |
The comparison is done using = |
|
2776 |
(i.e. equality test - not identity test). |
|
2777 |
||
2778 |
Notice, that for big collections, the runtime of this search |
|
2779 |
grows proportional to size(receiver) * size(aCollection). |
|
2780 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
2781 |
||
2782 |
^ self indexOfAny:aCollection startingAt:1 |
|
2783 |
||
2784 |
" |
|
2785 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
2786 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
2787 |
" |
|
2788 |
! |
|
2789 |
||
2790 |
indexOfAny:aCollection startingAt:start |
|
2791 |
"search the collection for an element in aCollection, |
|
2792 |
starting the search at index start; |
|
2793 |
if found, return the index otherwise return 0. |
|
2794 |
The comparison is done using = |
|
2795 |
(i.e. equality test - not identity test). |
|
2796 |
||
2797 |
Notice, that for big collections, the runtime of this search |
|
2798 |
grows proportional to size(receiver) * size(aCollection). |
|
2799 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
2800 |
||
2801 |
||
2802 |
|startIndex "{ Class: SmallInteger }" |
|
2803 |
stop "{ Class: SmallInteger }" | |
|
2804 |
||
2805 |
startIndex := start. |
|
2806 |
stop := self size. |
|
2807 |
startIndex to:stop do:[:index | |
|
2808 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
|
2809 |
]. |
|
2810 |
^ 0 |
|
2811 |
" |
|
2812 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
|
2813 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
2814 |
" |
|
1 | 2815 |
! |
2816 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2817 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2818 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2819 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2820 |
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
|
2821 |
The comparison is done using = |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2822 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2823 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2824 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2825 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2826 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2827 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2828 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2829 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2830 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2831 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2832 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2833 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2834 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2835 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2836 |
#(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
|
2837 |
#(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
|
2838 |
#(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
|
2839 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2840 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
2841 |
|
368 | 2842 |
indexOfSubCollection:aCollection |
2843 |
"find a subcollection. |
|
2844 |
If found, return the index; if not found, return 0." |
|
2845 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2846 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 2847 |
|
2848 |
" |
|
2849 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
|
2850 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
2851 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
2852 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
2853 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
2854 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
2855 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
2856 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
2857 |
" |
|
2858 |
! |
|
2859 |
||
602 | 2860 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
2861 |
"find a subcollection. If found, return the index; |
|
2862 |
if not found, return the result of evaluating exceptionBlock." |
|
2863 |
||
2864 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
2865 |
! |
|
2866 |
||
368 | 2867 |
indexOfSubCollection:aCollection startingAt:startIndex |
2868 |
"find a subcollection starting at index. |
|
2869 |
If found, return the index; if not found, return 0." |
|
2870 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
2871 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 2872 |
! |
2873 |
||
2874 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
2875 |
"find a subcollection, starting at index. If found, return the index; |
|
2876 |
if not found, return the result of evaluating exceptionBlock. |
|
2877 |
This is a q&d hack - not very efficient" |
|
2878 |
||
2879 |
|same first |
|
2880 |
mySize "{Class: SmallInteger }" |
|
2881 |
checkIndex "{Class: SmallInteger }" |
|
2882 |
cmpIndex "{Class: SmallInteger }" |
|
2883 |
sz "{Class: SmallInteger }"| |
|
2884 |
||
2885 |
mySize := self size. |
|
2886 |
sz := aCollection size. |
|
2887 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
2888 |
first := aCollection at:1. |
|
2889 |
||
2890 |
checkIndex := startIndex - 1. |
|
2891 |
[true] whileTrue:[ |
|
2892 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
|
2893 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
2894 |
||
2895 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
2896 |
^ 0 |
|
2897 |
]. |
|
2898 |
same := true. |
|
2899 |
cmpIndex := 1. |
|
2900 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
2901 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
2902 |
same := false |
|
2903 |
] ifTrue:[ |
|
2904 |
cmpIndex := cmpIndex + 1 |
|
2905 |
] |
|
2906 |
]. |
|
2907 |
same ifTrue:[^ checkIndex]. |
|
2908 |
] |
|
2909 |
||
2910 |
" |
|
2911 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
2912 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
2913 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
2914 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
2915 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
2916 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
2917 |
" |
|
602 | 2918 |
! |
2919 |
||
2920 |
lastIndexOf:anElement |
|
2921 |
"search the collection backwards for anElement; |
|
2922 |
if found, return the index otherwise return 0. |
|
2923 |
The comparison is done using = |
|
2924 |
(i.e. equality test - not identity test)." |
|
2925 |
||
2926 |
^ self lastIndexOf:anElement startingAt:self size |
|
2927 |
||
2928 |
" |
|
2929 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
2930 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
2931 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
2932 |
#(10 20 30 40 50 60 70) lastIndexOf:10 |
|
2933 |
" |
|
2934 |
! |
|
2935 |
||
2936 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
|
2937 |
"search the collection backwards for anElement; |
|
2938 |
if found, return the index otherwise return the value of the |
|
2939 |
exceptionBlock. |
|
2940 |
The comparison is done using = |
|
2941 |
(i.e. equality test - not identity test)." |
|
2942 |
||
2943 |
|index| |
|
2944 |
||
2945 |
index := self lastIndexOf:anElement startingAt:self size. |
|
2946 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2947 |
^ index |
|
2948 |
||
2949 |
" |
|
2950 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
|
2951 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
2952 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
2953 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 ifAbsent:['none'] |
|
2954 |
" |
|
2955 |
! |
|
2956 |
||
2957 |
lastIndexOf:anElement startingAt:start |
|
2958 |
"search the collection backwards for anElement, starting the search at index start; |
|
2959 |
if found, return the index otherwise return 0. |
|
2960 |
The comparison is done using = |
|
2961 |
(i.e. equality test - not identity test)." |
|
2962 |
||
2963 |
|startIndex "{ Class: SmallInteger }"| |
|
2964 |
||
2965 |
startIndex := start. |
|
2966 |
startIndex to:1 by:-1 do:[:index | |
|
2967 |
anElement = (self at:index) ifTrue:[^ index]. |
|
2968 |
]. |
|
2969 |
^ 0 |
|
2970 |
||
2971 |
" |
|
2972 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
|
2973 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
2974 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
2975 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
2976 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:4 |
|
2977 |
" |
|
2978 |
! |
|
2979 |
||
2980 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
|
2981 |
"search the collection backwards for anElement starting the search at |
|
2982 |
index start; if found, return the index |
|
2983 |
otherwise return the value of the exceptionBlock. |
|
2984 |
The comparison is done using = |
|
2985 |
(i.e. equality test - not identity test)." |
|
2986 |
||
2987 |
|index| |
|
2988 |
||
2989 |
index := self lastIndexOf:anElement startingAt:start. |
|
2990 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
2991 |
^ index |
|
2992 |
||
2993 |
" |
|
2994 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
2995 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
2996 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
2997 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 ifAbsent:['none'] |
|
2998 |
" |
|
2999 |
! |
|
3000 |
||
3001 |
nextIndexOf:anElement from:start to:stop |
|
3002 |
"search the collection for anElement, starting the search at index start, |
|
3003 |
stopping at:stop; |
|
3004 |
if found, return the index otherwise return 0. |
|
3005 |
The comparison is done using = |
|
3006 |
(i.e. equality test - not identity test)." |
|
3007 |
||
3008 |
|startIndex "{ Class: SmallInteger }" |
|
3009 |
stopIndex "{ Class: SmallInteger }" | |
|
3010 |
||
3011 |
startIndex := start. |
|
3012 |
stopIndex := stop. |
|
3013 |
startIndex to:stop do:[:index | |
|
3014 |
anElement = (self at:index) ifTrue:[^ index]. |
|
3015 |
]. |
|
3016 |
^ 0 |
|
3017 |
||
3018 |
" |
|
3019 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
3020 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
3021 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
3022 |
" |
|
3023 |
! |
|
3024 |
||
3025 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
3026 |
"search the collection for anElement, starting the search at index start |
|
3027 |
and stopping at stop; |
|
3028 |
if found, return the index otherwise return the value of the |
|
3029 |
exceptionBlock. |
|
3030 |
The comparison is done using = |
|
3031 |
(i.e. equality test - not identity test)." |
|
3032 |
||
3033 |
|index| |
|
3034 |
||
3035 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3036 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
3037 |
^ index |
|
3038 |
||
3039 |
" |
|
3040 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
3041 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
3042 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
3043 |
" |
|
2275 | 3044 |
! |
3045 |
||
3046 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
3047 |
"search the collection for anElement, starting the search at index start; |
|
3048 |
ending at stop, going in reverse direction. |
|
3049 |
If found (within the range), return the index, otherwise return 0. |
|
3050 |
The comparison is done using = |
|
3051 |
(i.e. equality test - not identity test)." |
|
3052 |
||
3053 |
|startIndex "{ Class: SmallInteger }" |
|
3054 |
stopIndex "{ Class: SmallInteger }" | |
|
3055 |
||
3056 |
startIndex := startSearchIndex. |
|
3057 |
stopIndex := endSearchIndex max:1. |
|
3058 |
startIndex to:stopIndex by:-1 do:[:index | |
|
3059 |
anElement = (self at:index) ifTrue:[^ index]. |
|
3060 |
]. |
|
3061 |
^ 0 |
|
3062 |
||
3063 |
" |
|
3064 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
3065 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
3066 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
3067 |
" |
|
3068 |
||
3069 |
"Modified: 25.1.1997 / 18:19:50 / cg" |
|
3070 |
! |
|
3071 |
||
3072 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
3073 |
"search the collection for anElement, starting the search at index start |
|
3074 |
and stopping at stop, doing a reverse search; |
|
3075 |
if found, return the index otherwise return the value of the |
|
3076 |
exceptionBlock. |
|
3077 |
The comparison is done using = |
|
3078 |
(i.e. equality test - not identity test)." |
|
3079 |
||
3080 |
|index| |
|
3081 |
||
3082 |
index := self prevIndexOf:anElement from:start to:stop. |
|
3083 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
3084 |
^ index |
|
3085 |
||
3086 |
" |
|
3087 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
3088 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
3089 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
3090 |
" |
|
3091 |
||
3092 |
"Modified: 25.1.1997 / 18:18:18 / cg" |
|
1 | 3093 |
! ! |
3094 |
||
3095 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
3096 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3097 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3098 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3099 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3100 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3101 |
This uses the randomized quicksort algorithm, |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3102 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3103 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3104 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3105 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3106 |
|stop| |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3107 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3108 |
stop := self size. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3109 |
(stop > 1) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3110 |
self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:nil |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3111 |
] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3112 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3113 |
"Modified: 21.8.1997 / 18:31:21 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3114 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3115 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3116 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3117 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3118 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3119 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3120 |
This uses the randomized quicksort algorithm, |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3121 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3122 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3123 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3124 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3125 |
|stop| |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3126 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3127 |
stop := self size. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3128 |
(stop > 1) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3129 |
self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:nil |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3130 |
] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3131 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3132 |
"Created: 21.8.1997 / 18:16:50 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3133 |
"Modified: 21.8.1997 / 18:31:24 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3134 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3135 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3136 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3137 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3138 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3139 |
Use this, when you have a key collection to sort some other collection with. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3140 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3141 |
This uses the randomized quicksort algorithm, |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3142 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3143 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3144 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3145 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3146 |
|stop| |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3147 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3148 |
stop := self size. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3149 |
(stop > 1) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3150 |
self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3151 |
] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3152 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3153 |
"Created: 21.8.1997 / 18:20:53 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3154 |
"Modified: 21.8.1997 / 18:31:27 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3155 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3156 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3157 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3158 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3159 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3160 |
Use this, when you have a key collection to sort another collection with. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3161 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3162 |
This uses the randomized quicksort algorithm, |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3163 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3164 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3165 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3166 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3167 |
|stop| |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3168 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3169 |
stop := self size. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3170 |
(stop > 1) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3171 |
self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3172 |
] |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3173 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3174 |
"Created: 21.8.1997 / 18:22:25 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3175 |
"Modified: 21.8.1997 / 18:31:31 / cg" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3176 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3177 |
|
602 | 3178 |
reverse |
2610 | 3179 |
"destructively reverse the order of the elements inplace" |
602 | 3180 |
|
3181 |
|lowIndex "{ Class: SmallInteger }" |
|
3182 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
3183 |
t1 t2| |
602 | 3184 |
|
3185 |
hiIndex := self size. |
|
3186 |
lowIndex := 1. |
|
3187 |
[lowIndex < hiIndex] whileTrue:[ |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
3188 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
3189 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
3190 |
|
602 | 3191 |
lowIndex := lowIndex + 1. |
3192 |
hiIndex := hiIndex - 1 |
|
3193 |
] |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
3194 |
|
69 | 3195 |
" |
602 | 3196 |
#(4 5 6 7 7) reverse |
3197 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 3198 |
" |
1 | 3199 |
! |
3200 |
||
3201 |
sort |
|
3202 |
"sort the collection inplace. The elements are compared using |
|
2069 | 3203 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3204 |
|
159 | 3205 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3206 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3207 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3208 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 3209 |
|
32 | 3210 |
|stop| |
1 | 3211 |
|
32 | 3212 |
stop := self size. |
3213 |
(stop > 1) ifTrue:[ |
|
2069 | 3214 |
self quickSortFrom:1 to:stop |
1 | 3215 |
] |
3216 |
||
95 | 3217 |
" |
3218 |
#(1 16 7 98 3 19 4 0) sort |
|
3219 |
||
3220 |
|data| |
|
3221 |
data := Random new next:100000. |
|
3222 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
3223 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
3224 |
data reverse. |
|
3225 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
3226 |
" |
|
2069 | 3227 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3228 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 3229 |
! |
3230 |
||
3231 |
sort:sortBlock |
|
3232 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3233 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3234 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3235 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3236 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3237 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3238 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 3239 |
|
32 | 3240 |
|stop| |
1 | 3241 |
|
32 | 3242 |
stop := self size. |
3243 |
(stop > 1) ifTrue:[ |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3244 |
sortBlock numArgs == 3 ifTrue:[ |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3245 |
"/ TODO: pass a collating policy to aBlock |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3246 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3247 |
] ifFalse:[ |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3248 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3249 |
] |
1 | 3250 |
] |
3251 |
||
95 | 3252 |
" |
2069 | 3253 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
3254 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
3255 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
3256 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 3257 |
" |
2069 | 3258 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3259 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 3260 |
! |
3261 |
||
3262 |
sort:sortBlock with:aCollection |
|
3263 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 3264 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3265 |
Use this, when you have a key collection to sort some other collection with. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3266 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3267 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3268 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3269 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3270 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 3271 |
|
32 | 3272 |
|stop| |
1 | 3273 |
|
32 | 3274 |
stop := self size. |
3275 |
(stop > 1) ifTrue:[ |
|
2069 | 3276 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
1 | 3277 |
] |
3278 |
||
69 | 3279 |
" |
3280 |
|c1 c2| |
|
1 | 3281 |
c1 := #(1 16 7 9). |
3282 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
3283 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 3284 |
c1 printNL. |
3285 |
c2 printNL |
|
69 | 3286 |
" |
2069 | 3287 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3288 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 3289 |
! |
3290 |
||
602 | 3291 |
sortWith:aCollection |
2069 | 3292 |
"sort the receiver collection inplace, using '<' to compare elements. |
3293 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3294 |
Use this, when you have a key collection to sort another collection with. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3295 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3296 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3297 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3298 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3299 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 3300 |
|
3301 |
|stop| |
|
356 | 3302 |
|
3303 |
stop := self size. |
|
602 | 3304 |
(stop > 1) ifTrue:[ |
2069 | 3305 |
self quickSortFrom:1 to:stop with:aCollection |
1 | 3306 |
] |
77 | 3307 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3308 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
424 | 3309 |
! |
3310 |
||
602 | 3311 |
topologicalSort:sortBlock |
3312 |
"sort the collection inplace using a sloooow sort algorithm. |
|
3313 |
This algorithm has O-square runtime behavior and should be used only |
|
3314 |
in special situations. |
|
3315 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
3316 |
a>c (for example, to sort classes by inheritance) |
|
3317 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3318 |
In all other situations, use #sort; which implements the quicksort algorithm. |
77 | 3319 |
" |
602 | 3320 |
|
3321 |
|smallestIndex "{ Class: SmallInteger }" |
|
3322 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
3323 |
index2 "{ Class: SmallInteger }" |
602 | 3324 |
smallest thisOne| |
3325 |
||
3326 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
3327 |
||
3328 |
end := self size. |
|
862 | 3329 |
1 to:end-1 do:[:index | |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3330 |
smallest := self at:index. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3331 |
smallestIndex := index. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3332 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3333 |
index2 := index + 1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3334 |
[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3335 |
thisOne := self at:index2. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3336 |
(index2 ~~ smallestIndex |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3337 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3338 |
smallestIndex := index2. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3339 |
smallest := thisOne. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3340 |
index2 := index + 1. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3341 |
] ifFalse:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3342 |
index2 := index2 + 1.. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3343 |
]. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3344 |
] doWhile:[index2 <= end]. |
862 | 3345 |
|
3346 |
"/ (index + 1) to:end do:[:index2 | |
|
3347 |
"/ thisOne := self at:index2. |
|
3348 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
3349 |
"/ smallestIndex := index2. |
|
3350 |
"/ smallest := thisOne |
|
3351 |
"/ ]. |
|
3352 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
3353 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3354 |
(smallestIndex ~~ index) ifTrue:[ |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3355 |
thisOne := self at:index. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3356 |
self at:index put:smallest. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3357 |
self at:smallestIndex put:thisOne |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3358 |
]. |
1 | 3359 |
] |
95 | 3360 |
|
77 | 3361 |
" |
602 | 3362 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
3363 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
3364 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
3365 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 3366 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3367 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
3368 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 3369 |
! ! |
602 | 3370 |
|
2069 | 3371 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 3372 |
|
3373 |
version |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
3374 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/SeqColl.st,v 1.89 1997-10-28 18:57:00 cg Exp $' |
628 | 3375 |
! ! |