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