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