author | Claus Gittinger <cg@exept.de> |
Thu, 20 Jul 2006 22:43:19 +0200 | |
changeset 9452 | 921f2690dfe6 |
parent 9414 | 33636257cb26 |
child 9454 | 7a9116db27b6 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
8874 | 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 |
||
5358 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
Collection subclass:#SequenceableCollection |
6183 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'MissingClassInLiteralArrayErrorSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Abstract' |
|
1 | 20 |
! |
21 |
||
2069 | 22 |
!SequenceableCollection class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
8874 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
SequenceableCollections have ordered elements which can be accessed via |
|
8874 | 41 |
a numeric index. |
42 |
SequenceableCollection is an abstract class - there are no instances of |
|
43 |
it in the system. |
|
1385 | 44 |
|
45 |
The methods found here assume that implementations of (at least) |
|
46 |
#at:/#at:put: are implemented (which are always found in Object for |
|
47 |
indexable objects). |
|
48 |
||
49 |
For performance, some subclasses redefine more methods, knowing the details |
|
8874 | 50 |
of how elements are stored. |
51 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
|
1385 | 52 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this |
53 |
kind of tuneup. |
|
54 |
||
55 |
See concrete subclasses (such as OrderedCollection, Array or String). |
|
1289 | 56 |
|
57 |
[author:] |
|
8874 | 58 |
Claus Gittinger |
1385 | 59 |
|
60 |
[see also:] |
|
8874 | 61 |
OrderedCollection Array |
62 |
CharacterArray String |
|
88 | 63 |
" |
64 |
! ! |
|
1 | 65 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
66 |
!SequenceableCollection class methodsFor:'initialization'! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
67 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
68 |
initialize |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
69 |
MissingClassInLiteralArrayErrorSignal isNil ifTrue:[ |
8874 | 70 |
MissingClassInLiteralArrayErrorSignal := Error newSignalMayProceed:true. |
71 |
MissingClassInLiteralArrayErrorSignal nameClass:self message:#missingClassInLiteralArrayErrorSignal. |
|
72 |
MissingClassInLiteralArrayErrorSignal notifierString:'Missing class in literal encoding'. |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
73 |
] |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
74 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
75 |
"Created: / 18.5.1999 / 14:49:51 / cg" |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
76 |
! ! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
77 |
|
2069 | 78 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 79 |
|
4807 | 80 |
decodeFromLiteralArray:anArray |
81 |
"create & return a new instance from information encoded in anArray. |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
82 |
Redefined for faster creation." |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
83 |
|
8874 | 84 |
|collection |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
85 |
sz "{ Class: SmallInteger }"| |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
86 |
|
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
87 |
sz := anArray size. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
88 |
collection := self withSize:sz-1. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
89 |
2 to:sz do:[:idx| collection at:idx-1 put:(anArray at:idx) decodeAsLiteralArray]. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
90 |
^ collection |
4807 | 91 |
|
8797 | 92 |
" |
93 |
Array |
|
8874 | 94 |
decodeFromLiteralArray:#(Array 1 2 3) |
8797 | 95 |
" |
4807 | 96 |
! |
97 |
||
1 | 98 |
new:size withAll:element |
360 | 99 |
"return a new collection of size, where all elements are |
100 |
initialized to element." |
|
1 | 101 |
|
102 |
|newCollection| |
|
103 |
||
104 |
newCollection := self new:size. |
|
105 |
newCollection atAllPut:element. |
|
106 |
^ newCollection |
|
360 | 107 |
! |
108 |
||
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
109 |
newWithConcatenationOfAll:aCollectionOfCollections |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
110 |
"this creates and returns a new collection consisting |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
111 |
of the concatenation of all elements of the argument. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
112 |
I.e. it has the same effect as concatenating all elements |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
113 |
of the argument using #, ,but is implemented more efficiently, |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
114 |
by avoiding repeated garbage allocations. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
115 |
This is an O runtime algorithm, in contrast to the #, loop, which is O*O" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
116 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
117 |
|totalSize newColl idx| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
118 |
|
8874 | 119 |
totalSize := aCollectionOfCollections |
120 |
inject:0 |
|
121 |
into:[:sumSoFar :el | sumSoFar + el size]. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
122 |
newColl := self new:totalSize. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
123 |
idx := 1. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
124 |
aCollectionOfCollections do:[:el | |
8874 | 125 |
|sz| |
126 |
||
127 |
sz := el size. |
|
128 |
newColl replaceFrom:idx to:(idx+sz-1) with:el startingAt:1. |
|
129 |
idx := idx + sz |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
130 |
]. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
131 |
^ newColl |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
132 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
133 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
134 |
this method optimizes the following (common) operation: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
135 |
|
8874 | 136 |
|s| |
137 |
||
138 |
s := ''. |
|
139 |
#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') do:[:e| |
|
140 |
s := s , e. |
|
141 |
]. |
|
142 |
s |
|
143 |
||
144 |
String |
|
145 |
newWithConcatenationOfAll:#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') |
|
146 |
||
147 |
String |
|
148 |
newWithConcatenationOfAll:#() |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
149 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
150 |
Array |
8874 | 151 |
newWithConcatenationOfAll:#( (1 2 3 4) (5 6 7 8) (9 10 11 12) ) |
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
152 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
153 |
timing: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
154 |
------- |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
155 |
speed-break-even is at around 5 elements for strings; |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
156 |
for more elements, #newWithConcatenationOfAll: is much faster. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
157 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
158 |
|arr1 arr2 arr3 t| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
159 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
160 |
arr1 := #('hello' ' ' 'world' ' ' 'this is ' 'ST/X'). |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
161 |
arr2 := Array new:1000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
162 |
arr3 := Array new:10000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
163 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
164 |
(Array with:arr1 with:arr2 with:arr3) with:#(10000 100 10) do:[:arr :cnt | |
8874 | 165 |
t := Time millisecondsToRun:[ |
166 |
cnt timesRepeat:[ |
|
167 |
String |
|
168 |
newWithConcatenationOfAll:arr |
|
169 |
] |
|
170 |
]. |
|
171 |
Transcript showCR:(arr size printString , ' elements - time for #newWithConcatenationOfAll :' , (t/cnt) asFloat printString , 'mS'). |
|
172 |
Transcript endEntry. |
|
173 |
||
174 |
t := Time millisecondsToRun:[ |
|
175 |
cnt timesRepeat:[ |
|
176 |
|s| |
|
177 |
||
178 |
s := ''. |
|
179 |
arr do:[:e| |
|
180 |
s := s , e. |
|
181 |
]. |
|
182 |
s |
|
183 |
] |
|
184 |
]. |
|
185 |
Transcript showCR:(arr size printString , ' elements - time for loop over #, :' , (t/cnt) asFloat printString , 'mS'). |
|
186 |
Transcript endEntry. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
187 |
] |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
188 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
189 |
! |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
190 |
|
360 | 191 |
withSize:size |
192 |
"return a new collection of size. |
|
193 |
For variable size collections, this is different from #new:, |
|
194 |
in that #new: creates an empty collection with preallocated size, |
|
195 |
while #withSize: creates a non empty one." |
|
196 |
||
9162 | 197 |
^ (self new:size) grow:size. |
360 | 198 |
|
199 |
" |
|
200 |
(OrderedCollection new:10) inspect. |
|
201 |
(OrderedCollection withSize:10) inspect. |
|
202 |
(Array new:10) inspect. |
|
203 |
(Array withSize:10) inspect. |
|
204 |
" |
|
1 | 205 |
! ! |
206 |
||
7304 | 207 |
!SequenceableCollection class methodsFor:'Compatibility-Squeak'! |
4370 | 208 |
|
209 |
streamContents:blockWithArg |
|
6126 | 210 |
"create a write-stream on an instance of the receiver-class, |
211 |
evaluate blockWithArg, passing that stream, |
|
212 |
extract and return the streams contents." |
|
213 |
||
4370 | 214 |
|stream| |
215 |
||
216 |
stream := WriteStream on:(self new:100). |
|
217 |
blockWithArg value:stream. |
|
218 |
^ stream contents |
|
219 |
||
6126 | 220 |
" |
221 |
|rslt| |
|
222 |
||
223 |
rslt := String streamContents:[:s | s nextPutAll:'hello'; space; nextPutAll:'world'] |
|
224 |
" |
|
225 |
||
226 |
"Modified: / 31.10.2001 / 09:25:45 / cg" |
|
6820 | 227 |
! |
228 |
||
229 |
writeStream |
|
230 |
"create a write-stream on an instance of the receiver-class" |
|
231 |
||
232 |
^ WriteStream on:(self new:100). |
|
8220 | 233 |
|
234 |
" |
|
235 |
String writeStream |
|
236 |
" |
|
4370 | 237 |
! ! |
238 |
||
5188 | 239 |
!SequenceableCollection class methodsFor:'Signal constants'! |
240 |
||
241 |
missingClassInLiteralArrayErrorSignal |
|
242 |
^ MissingClassInLiteralArrayErrorSignal |
|
243 |
||
244 |
"Created: / 18.5.1999 / 14:50:04 / cg" |
|
245 |
! ! |
|
246 |
||
7304 | 247 |
!SequenceableCollection class methodsFor:'instance creation-multiDimensional'! |
7255 | 248 |
|
249 |
_at:nIndices |
|
250 |
"this is a synthetic selector, generated by the compiler, |
|
251 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 252 |
I.e. |
253 |
Array[n] |
|
7255 | 254 |
generates |
8874 | 255 |
Array _at: n |
7255 | 256 |
" |
257 |
||
258 |
^ self new:nIndices |
|
259 |
! |
|
260 |
||
261 |
_at:dim1 at:dim2 |
|
262 |
"this is a synthetic selector, generated by the compiler, |
|
263 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 264 |
I.e. |
265 |
Array[n,m] |
|
7255 | 266 |
generates |
8874 | 267 |
Array _at:n at:m |
7255 | 268 |
" |
269 |
||
270 |
|data| |
|
271 |
||
272 |
data := self withSize:(dim1 * dim2). |
|
273 |
^ MultiDimensionalArrayAccessor |
|
8874 | 274 |
collection:data |
275 |
dimensions:(Array with:dim1 with:dim2) |
|
7255 | 276 |
! |
277 |
||
278 |
_at:dim1 at:dim2 at:dim3 |
|
279 |
"this is a synthetic selector, generated by the compiler, |
|
280 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 281 |
I.e. |
282 |
Array[n,m,o] |
|
7255 | 283 |
generates |
8874 | 284 |
Array _at:n at:m at:o |
7255 | 285 |
" |
286 |
||
287 |
|data| |
|
288 |
||
289 |
data := self withSize:(dim1 * dim2 * dim3). |
|
8874 | 290 |
^ MultiDimensionalArrayAccessor |
291 |
collection:data |
|
292 |
dimensions:(Array with:dim1 with:dim2 with:dim3) |
|
7255 | 293 |
! ! |
294 |
||
8893 | 295 |
!SequenceableCollection class methodsFor:'queries'! |
296 |
||
297 |
isAbstract |
|
298 |
^ self == SequenceableCollection |
|
299 |
! ! |
|
300 |
||
7261 | 301 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 302 |
|
7796 | 303 |
allButFirst |
8874 | 304 |
"Return a copy of the receiver containing all but the first element. |
7796 | 305 |
Raise an error if there are not enough elements." |
306 |
||
307 |
^ self copyFrom:2 |
|
8571 | 308 |
|
309 |
" |
|
310 |
'1234567890' allButFirst |
|
311 |
" |
|
312 |
! |
|
313 |
||
314 |
allButFirst:n |
|
8874 | 315 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 316 |
Raise an error if there are not enough elements." |
317 |
||
318 |
^ self copyFrom:n+1 |
|
319 |
||
320 |
" |
|
321 |
'1234567890' allButFirst:5 |
|
322 |
" |
|
7796 | 323 |
! |
324 |
||
6149 | 325 |
allButLast |
8874 | 326 |
"Return a copy of the receiver containing all but the last element. |
7796 | 327 |
Raise an error if there are not enough elements." |
6183 | 328 |
|
329 |
^ self allButLast:1 |
|
330 |
||
8571 | 331 |
" |
332 |
'1234567890' allButLast |
|
333 |
" |
|
334 |
||
6183 | 335 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
336 |
! |
|
337 |
||
338 |
allButLast:n |
|
8874 | 339 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 340 |
Raise an error if there are not enough elements." |
6183 | 341 |
|
342 |
^ self copyFrom: 1 to: self size - n |
|
343 |
||
8571 | 344 |
" |
345 |
'1234567890' allButLast:5 |
|
346 |
" |
|
347 |
||
6183 | 348 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 349 |
! |
350 |
||
8874 | 351 |
atPin: index |
4414 | 352 |
"Return the index'th element of me if possible. |
353 |
Return the first or last element if index is out of bounds." |
|
354 |
||
355 |
index < 1 ifTrue:[^ self first]. |
|
356 |
index > self size ifTrue:[^ self last]. |
|
357 |
^ self at:index |
|
358 |
||
359 |
" |
|
8874 | 360 |
#(1 2 3) atPin:4 |
361 |
#(1 2 3) atPin:0 |
|
362 |
#(1 2 3) atPin:1 |
|
363 |
#(1 2 3) atPin:3 |
|
364 |
" |
|
365 |
! |
|
366 |
||
367 |
atRandom |
|
4459 | 368 |
"Return any random element from the receiver" |
369 |
||
8573 | 370 |
^ self atRandom:Random |
371 |
||
372 |
" |
|
8874 | 373 |
#(1 2 3) atRandom |
374 |
" |
|
375 |
! |
|
376 |
||
377 |
atRandom:aRandomGenerator |
|
8573 | 378 |
"Return any random element from the receiver" |
379 |
||
4459 | 380 |
|max idx| |
381 |
||
382 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 383 |
self emptyCollectionError |
4459 | 384 |
]. |
8573 | 385 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 386 |
^ self at:idx |
387 |
||
388 |
" |
|
8874 | 389 |
#(1 2 3) atRandom:(Random new) |
390 |
" |
|
391 |
! |
|
392 |
||
393 |
atWrap:index |
|
4975 | 394 |
"Return the index'th element of me if possible. |
395 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
396 |
||
397 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
398 |
||
399 |
" |
|
8874 | 400 |
#(1 2 3) atWrap:1 |
401 |
#(1 2 3) atWrap:2 |
|
402 |
#(1 2 3) atWrap:3 |
|
403 |
#(1 2 3) atWrap:4 |
|
404 |
#(1 2 3) atWrap:5 |
|
405 |
#(1 2 3) atWrap:6 |
|
406 |
#(1 2 3) atWrap:7 |
|
407 |
#(1 2 3) atWrap:0 |
|
408 |
#(1 2 3) atWrap:-1 |
|
4975 | 409 |
" |
5372 | 410 |
! |
411 |
||
412 |
beginsWith:aCollection |
|
6183 | 413 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 414 |
|
415 |
^ self startsWith:aCollection |
|
6183 | 416 |
|
417 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 418 |
! |
419 |
||
8611 | 420 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 421 |
"squeak uses a different naming here - sigh. |
422 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
423 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
424 |
this method first takes a slice (from:to:) and then collects over that. |
|
425 |
I.e. it is equivalent to: |
|
8874 | 426 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 427 |
|
428 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
429 |
! |
|
430 |
||
7012 | 431 |
copyAfter: anElement |
8874 | 432 |
"Answer a copy of the receiver from after the first occurrence |
433 |
of anElement up to the end. If no such element exists, answer |
|
434 |
an empty copy." |
|
7012 | 435 |
|idx| |
436 |
||
437 |
idx := self indexOf:anElement. |
|
438 |
idx == 0 ifTrue:[idx := self size]. |
|
439 |
^ self copyFrom:idx + 1 |
|
440 |
||
441 |
" |
|
8874 | 442 |
'hello world' copyAfter:$l |
443 |
'123456123456' copyAfter:$2 |
|
444 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
445 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
446 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 447 |
" |
448 |
! |
|
449 |
||
6982 | 450 |
copyAfterLast:element |
8874 | 451 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 452 |
of element to the end; uses = for comparison" |
453 |
||
454 |
|idx| |
|
455 |
||
456 |
idx := self lastIndexOf:element. |
|
457 |
idx == 0 ifTrue:[idx := self size]. |
|
458 |
^ self copyFrom:idx + 1 |
|
459 |
||
460 |
" |
|
8874 | 461 |
'hello world' copyAfterLast:$l |
462 |
'123456123456' copyAfterLast:$2 |
|
463 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
464 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
465 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 466 |
" |
467 |
! |
|
468 |
||
6980 | 469 |
copyUpToLast:element |
8874 | 470 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 471 |
of element; uses = for comparison" |
472 |
||
473 |
|idx| |
|
474 |
||
475 |
idx := self lastIndexOf:element. |
|
476 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
477 |
^ self copyTo:idx - 1 |
|
478 |
||
479 |
" |
|
8874 | 480 |
'hello world' copyUpToLast:$l |
481 |
'123456123456' copyUpToLast:$2 |
|
482 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
483 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
484 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 485 |
" |
486 |
! |
|
487 |
||
8276 | 488 |
copyWithoutAll:elementsToSkip |
489 |
"return a new collection consisting of a copy of the receiver, with |
|
490 |
ALL elements equal to any in elementsToSkip are left out. |
|
491 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
492 |
||
493 |
|n "{ Class: SmallInteger }" |
|
494 |
sz "{ Class: SmallInteger }" |
|
495 |
srcIndex "{ Class: SmallInteger }" |
|
496 |
dstIndex "{ Class: SmallInteger }" |
|
497 |
skipIndex "{ Class: SmallInteger }" |
|
498 |
copy l| |
|
499 |
||
8874 | 500 |
"the code below may look like overkill, |
8276 | 501 |
however, for big collections its better to move data |
502 |
around in big chunks" |
|
503 |
||
504 |
n := self occurrencesOfAny:elementsToSkip. |
|
505 |
n == 0 ifTrue:[^ self copy]. |
|
506 |
||
507 |
sz := self size. |
|
508 |
copy := self copyEmptyAndGrow:(sz - n). |
|
509 |
||
510 |
srcIndex := 1. |
|
511 |
dstIndex := 1. |
|
512 |
||
513 |
n timesRepeat:[ |
|
8874 | 514 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
515 |
l := skipIndex - srcIndex. |
|
516 |
l ~~ 0 ifTrue:[ |
|
517 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
518 |
with:self startingAt:srcIndex. |
|
519 |
dstIndex := dstIndex + l |
|
520 |
]. |
|
521 |
srcIndex := skipIndex + 1 |
|
8276 | 522 |
]. |
523 |
l := sz - srcIndex. |
|
524 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 525 |
with:self startingAt:srcIndex. |
8276 | 526 |
^ copy |
527 |
||
528 |
" |
|
529 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 530 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 531 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
532 |
" |
|
533 |
! |
|
534 |
||
7304 | 535 |
copyWithoutFirst |
536 |
^ self copyFrom:2 |
|
537 |
! |
|
538 |
||
6149 | 539 |
permutationsDo: aBlock |
6183 | 540 |
"Repeatly value aBlock with a single copy of the receiver. Reorder the copy |
541 |
so that aBlock is presented all (self size factorial) possible permutations." |
|
542 |
||
543 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
544 |
||
545 |
" |
|
546 |
(1 to: 4) permutationsDo:[:each | Transcript cr; show: each printString] |
|
547 |
" |
|
548 |
||
549 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
6149 | 550 |
! |
551 |
||
552 |
permutationsStartingAt: anInteger do: aBlock |
|
6183 | 553 |
"#(1 2 3 4) permutationsDo: [:each | Transcript cr; show: each printString]" |
554 |
||
555 |
|mySize| |
|
556 |
||
557 |
mySize := self size. |
|
558 |
anInteger > mySize ifTrue: [^self]. |
|
559 |
anInteger = mySize ifTrue: [^aBlock value: self]. |
|
8874 | 560 |
anInteger to:mySize do:[:i | |
561 |
self swap: anInteger with: i. |
|
562 |
self permutationsStartingAt: anInteger + 1 do: aBlock. |
|
563 |
self swap: anInteger with: i |
|
6183 | 564 |
] |
565 |
||
566 |
"Modified: / 13.11.2001 / 13:48:49 / cg" |
|
4414 | 567 |
! ! |
568 |
||
7261 | 569 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 570 |
|
571 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
572 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
573 |
! ! |
|
574 |
||
1 | 575 |
!SequenceableCollection methodsFor:'accessing'! |
576 |
||
1366 | 577 |
after:anObject |
578 |
"return the element, after anObject. |
|
5231 | 579 |
If anObject is not in the receiver, report an error; |
580 |
if anObject is the last in the receiver, return nil. |
|
1366 | 581 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
582 |
||
583 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
584 |
||
585 |
" |
|
8874 | 586 |
#(4 3 2 1) asOrderedCollection after:3. |
587 |
#(4 3 2 1) asOrderedCollection after:5 |
|
588 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 589 |
" |
590 |
||
591 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
592 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
593 |
! |
|
594 |
||
595 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 596 |
"return the element, after anObject. |
5231 | 597 |
If anObject is the last in the receiver, return nil. |
598 |
If there is no such element (anObject is not found), |
|
1366 | 599 |
return the value from exceptionBlock." |
600 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
601 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
602 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
603 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
604 |
idx ~~ 0 ifTrue:[ |
8874 | 605 |
idx == self size ifTrue:[^ nil]. |
606 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
607 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
608 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
609 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
610 |
" |
8874 | 611 |
#(4 3 2 1) after:3 ifAbsent:nil. |
612 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
613 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
614 |
(2 to:10 by:2) after:5 ifAbsent:nil. |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
615 |
" |
1366 | 616 |
! |
617 |
||
6967 | 618 |
anElement |
8874 | 619 |
"return any element from the collection, |
6967 | 620 |
report an error if there is none" |
621 |
||
622 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
623 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
624 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
625 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
626 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
627 |
" |
6967 | 628 |
! |
629 |
||
32 | 630 |
at:index ifAbsent:exceptionBlock |
8874 | 631 |
"return the element at index if valid. |
632 |
If the index is invalid, return the result of evaluating |
|
302 | 633 |
the exceptionblock. |
8874 | 634 |
NOTICE: |
635 |
in ST-80, this message is only defined for Dictionaries, |
|
636 |
however, having a common protocol with indexed collections |
|
637 |
often simplifies things." |
|
32 | 638 |
|
8579 | 639 |
(index between:1 and:self size) ifFalse:[ |
8874 | 640 |
^ exceptionBlock value |
32 | 641 |
]. |
642 |
^ self at:index |
|
643 |
||
95 | 644 |
" |
645 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 646 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
647 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 648 |
|
95 | 649 |
(Dictionary with:(#foo -> #bar) |
8874 | 650 |
with:(#frob -> #baz)) |
651 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 652 |
" |
362 | 653 |
! |
654 |
||
1366 | 655 |
before:anObject |
656 |
"return the element before the argument, anObject. |
|
5231 | 657 |
If anObject is not in the receiver, report an error; |
658 |
if anObject is the first in the receiver, return nil. |
|
1366 | 659 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
660 |
||
661 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
662 |
||
663 |
" |
|
8874 | 664 |
#(4 3 2 1) asOrderedCollection before:3. |
665 |
#(4 3 2 1) asOrderedCollection before:4 |
|
666 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 667 |
" |
668 |
||
669 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
670 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
671 |
! |
|
672 |
||
673 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 674 |
"return the element, before anObject. |
5231 | 675 |
If anObject is the first in the receiver, return nil. |
676 |
If there is no such element (anObject is not found), |
|
1366 | 677 |
return the value from exceptionBlock." |
678 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
679 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
680 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
681 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
682 |
idx ~~ 0 ifTrue:[ |
8874 | 683 |
idx == 1 ifTrue:[^ nil]. |
684 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
685 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
686 |
^ exceptionBlock value |
1366 | 687 |
|
5231 | 688 |
" |
8874 | 689 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
690 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
691 |
#(4 3 2 1) asOrderedCollection before:5. |
|
692 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
693 |
#(4 3 2 1) asOrderedCollection before:4. |
|
694 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
695 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 696 |
" |
697 |
||
1366 | 698 |
! |
699 |
||
602 | 700 |
first |
8874 | 701 |
"return the first element; |
5231 | 702 |
report an error, if the collection is empty." |
602 | 703 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
704 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 705 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
706 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
707 |
" |
8874 | 708 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
709 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
710 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
711 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
712 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 713 |
! |
714 |
||
5887 | 715 |
first:n |
716 |
"return the n first elements of the collection. |
|
717 |
Raises an error if there are not enough elements in the receiver." |
|
718 |
||
719 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
720 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
721 |
||
722 |
^ self copyFirst:n |
|
723 |
||
724 |
" |
|
8874 | 725 |
#(1 2 3 4 5) first:3 |
726 |
#(1 2 3 4 5) first:6 |
|
727 |
#(1 2 3 4 5) asSet first:3 |
|
728 |
'hello world' first:5 |
|
5887 | 729 |
" |
730 |
! |
|
731 |
||
362 | 732 |
keyAtEqualValue:value |
733 |
"return the index of a value. |
|
734 |
This is normally not used (use indexOf:), but makes the |
|
735 |
protocol more compatible with dictionaries." |
|
736 |
||
737 |
^ self indexOf:value |
|
738 |
! |
|
739 |
||
740 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
741 |
"return the index of a value. |
|
742 |
This is normally not used (use indexOf:), but makes the |
|
743 |
protocol more compatible with dictionaries." |
|
744 |
||
745 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 746 |
! |
747 |
||
748 |
keyAtValue:value |
|
749 |
"return the index of a value. |
|
750 |
This is normally not used (use indexOf:), but makes the |
|
751 |
protocol more compatible with dictionaries." |
|
752 |
||
753 |
^ self identityIndexOf:value |
|
754 |
! |
|
755 |
||
756 |
keyAtValue:value ifAbsent:exceptionBlock |
|
757 |
"return the index of a value. |
|
758 |
This is normally not used (use indexOf:), but makes the |
|
759 |
protocol more compatible with dictionaries." |
|
760 |
||
761 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
762 |
! |
|
763 |
||
764 |
last |
|
5231 | 765 |
"return the last element; |
766 |
report an error, if the collection is empty." |
|
602 | 767 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
768 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
769 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
770 |
(sz := self size) == 0 ifFalse:[ |
8874 | 771 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
772 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
773 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
774 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
775 |
|
5231 | 776 |
" |
777 |
#(1 2 3 4 5) last |
|
778 |
#() last |
|
779 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
780 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
781 |
|
5887 | 782 |
last:n |
783 |
"return the n last elements of the collection. |
|
784 |
Raises an error if there are not enough elements in the receiver." |
|
785 |
||
786 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
787 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
788 |
||
789 |
^ self copyLast:n |
|
790 |
||
791 |
" |
|
8874 | 792 |
#(1 2 3 4 5) last:3 |
793 |
#(1 2 3 4 5) last:6 |
|
794 |
#(1 2 3 4 5) asSet last:3 |
|
795 |
'hello world' last:5 |
|
5887 | 796 |
" |
797 |
! |
|
798 |
||
4986 | 799 |
nth:n |
5231 | 800 |
"return the nth element; |
801 |
report an error, if the collections size is smaller than n." |
|
4986 | 802 |
|
803 |
^ self at:n |
|
804 |
||
805 |
" |
|
8874 | 806 |
args: |
4986 | 807 |
returns: nth element <object> |
808 |
" |
|
809 |
||
5231 | 810 |
" |
8874 | 811 |
#(10 20 30 40 50) nth:4 |
5231 | 812 |
#(10 20 30 40 50) nth:6 |
813 |
#() nth:1 |
|
814 |
" |
|
815 |
||
4986 | 816 |
! |
817 |
||
4855 | 818 |
second |
5231 | 819 |
"return the second element; |
820 |
report an error, if the collections size is smaller than 2." |
|
4855 | 821 |
|
822 |
^ self at:2 |
|
823 |
||
824 |
" |
|
8874 | 825 |
args: |
4855 | 826 |
returns: secondElement <object> |
827 |
" |
|
828 |
||
829 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
830 |
! |
|
831 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
832 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
833 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
834 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
835 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
836 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
837 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
838 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
839 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
840 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
841 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 842 |
! ! |
843 |
||
602 | 844 |
!SequenceableCollection methodsFor:'adding & removing'! |
845 |
||
846 |
add:anObject |
|
847 |
"append the argument, anObject to the collection. |
|
848 |
Return the argument, anObject. |
|
849 |
||
850 |
Notice, that this is modifies the receiver NOT a copy. |
|
851 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 852 |
due to the grow:-message, which is inefficient for fixed size |
602 | 853 |
collections (i.e. for Strings and Arrays it is not recommened)." |
854 |
||
855 |
|newSize| |
|
856 |
||
857 |
newSize := self size + 1. |
|
858 |
self grow:newSize. |
|
859 |
self at:newSize put:anObject. |
|
860 |
^ anObject |
|
861 |
||
862 |
" |
|
863 |
|a| |
|
864 |
a := #(1 2 3 4 5 6 7 8). |
|
865 |
a add:'hello'. |
|
866 |
a |
|
867 |
" |
|
868 |
" |
|
869 |
|c| |
|
870 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
871 |
c add:'hello'. |
|
872 |
c |
|
873 |
" |
|
874 |
! |
|
875 |
||
876 |
add:anElement beforeIndex:index |
|
877 |
"insert the first argument, anObject into the collection before slot index. |
|
878 |
Return the receiver (sigh - ST-80 compatibility). |
|
879 |
||
880 |
Notice, that this is modifies the receiver NOT a copy. |
|
881 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 882 |
due to the grow:-message, which is inefficient for fixed size |
602 | 883 |
collections (i.e. for Strings and Arrays it is not recommened)." |
884 |
||
885 |
|newSize| |
|
886 |
||
887 |
newSize := self size + 1. |
|
888 |
self grow:newSize. |
|
889 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
890 |
self at:index put:anElement |
|
891 |
||
892 |
" |
|
893 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
894 |
" |
|
202 | 895 |
! |
896 |
||
602 | 897 |
addFirst:anObject |
898 |
"prepend the argument, anObject to the collection. |
|
899 |
Return the argument, anObject. |
|
900 |
||
901 |
Notice, that this is modifies the receiver NOT a copy. |
|
902 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 903 |
due to the grow:-message, which is inefficient for fixed size |
602 | 904 |
collections (i.e. for Strings and Arrays it is not recommened)." |
905 |
||
906 |
|newSize| |
|
907 |
||
908 |
newSize := self size + 1. |
|
909 |
self grow:newSize. |
|
910 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
911 |
self at:1 put:anObject. |
|
912 |
^ anObject |
|
913 |
||
914 |
" |
|
8874 | 915 |
|a| |
916 |
a:= #(1 2 3 4 5 6 7 8). |
|
917 |
a addFirst:'hello'. |
|
918 |
a |
|
602 | 919 |
" |
920 |
" |
|
921 |
|c| |
|
922 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
923 |
c addFirst:'hello'. |
|
924 |
c |
|
925 |
" |
|
69 | 926 |
! |
927 |
||
602 | 928 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
929 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
930 |
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
|
931 |
Use equality compare (=) to search for an occurrence. |
602 | 932 |
|
933 |
Notice, that this is modifies the receiver NOT a copy. |
|
934 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 935 |
due to the grow:-message, which is inefficient for fixed size |
602 | 936 |
collections (i.e. for Strings and Arrays it is not recommened)." |
937 |
||
8874 | 938 |
|any |
602 | 939 |
dstIndex "{ Class: SmallInteger }" |
940 |
sz "{ Class: SmallInteger }"| |
|
941 |
||
942 |
dstIndex := 1. |
|
943 |
any := false. |
|
944 |
sz := self size. |
|
945 |
1 to:sz do:[:srcIndex | |
|
8874 | 946 |
(anElement = (self at:srcIndex)) ifTrue:[ |
947 |
any := true |
|
948 |
] ifFalse:[ |
|
949 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
950 |
self at:dstIndex put:(self at:srcIndex) |
|
951 |
]. |
|
952 |
dstIndex := dstIndex + 1 |
|
953 |
] |
|
602 | 954 |
]. |
955 |
any ifTrue:[ |
|
8874 | 956 |
self grow:dstIndex - 1. |
957 |
^ anElement |
|
602 | 958 |
]. |
959 |
^ aBlock value |
|
960 |
||
961 |
" |
|
1422 | 962 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 963 |
#(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
|
964 |
#(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
|
965 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 966 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
967 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
968 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 969 |
! |
970 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
971 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
972 |
"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
|
973 |
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
|
974 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
975 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
976 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
977 |
"/ 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
|
978 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
979 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
980 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
981 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
982 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
983 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
984 |
[runIndex <= self size] whileTrue:[ |
8874 | 985 |
element := self at:runIndex. |
986 |
(aBlock value:element) ifTrue:[ |
|
987 |
removed add:element. |
|
988 |
self removeAtIndex:runIndex |
|
989 |
] ifFalse:[ |
|
990 |
runIndex := runIndex + 1 |
|
991 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
992 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
993 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
994 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
995 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
996 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
997 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
998 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 999 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1000 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1001 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1002 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1003 |
"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
|
1004 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1005 |
|
602 | 1006 |
removeAtIndex:anIndex |
1007 |
"remove the element stored at anIndex. Return the removed object. |
|
1008 |
||
1009 |
Notice, that this is modifies the receiver NOT a copy. |
|
1010 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1011 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1012 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1013 |
||
1014 |
|element| |
|
1015 |
||
1016 |
element := self at:anIndex. |
|
1017 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1018 |
^ element |
|
1019 |
||
1020 |
" |
|
1021 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1022 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1023 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1024 |
" |
|
13 | 1025 |
! |
1026 |
||
602 | 1027 |
removeFirst |
1028 |
"remove the first element of the receiver and return it. |
|
1029 |
||
1030 |
Notice, that this is modifies the receiver NOT a copy. |
|
1031 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1032 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1033 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1034 |
||
1035 |
^ self removeAtIndex:1 |
|
1036 |
||
1037 |
" |
|
1038 |
|a| |
|
1039 |
a := #(1 2 3 4 5 6). |
|
1040 |
a removeFirst. |
|
8874 | 1041 |
a |
602 | 1042 |
" |
13 | 1043 |
! |
1044 |
||
2294 | 1045 |
removeFromIndex:startIndex |
1046 |
"remove the elements stored at indexes from startIndex to the end. |
|
1047 |
Return the receiver. |
|
1048 |
Returning the receiver is a historic leftover - it may at one |
|
1049 |
time return a collection of the removed elements. |
|
1050 |
||
8874 | 1051 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1052 |
therefore any other users of the receiver will also see this change. |
1053 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1054 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1055 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1056 |
||
1057 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1058 |
||
1059 |
" |
|
8874 | 1060 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1061 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1062 |
" |
|
1063 |
||
1064 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1065 |
! |
|
1066 |
||
602 | 1067 |
removeFromIndex:startIndex toIndex:endIndex |
1068 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1069 |
Return the receiver. |
|
1070 |
Returning the receiver is a historic leftover - it may at one |
|
1071 |
time return a collection of the removed elements. |
|
1072 |
||
8874 | 1073 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1074 |
therefore any other users of the receiver will also see this change. |
1075 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1076 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1077 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1078 |
||
1079 |
|size newSize| |
|
1080 |
||
1081 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1082 |
self grow:(startIndex - 1) |
602 | 1083 |
] ifFalse:[ |
8874 | 1084 |
newSize := size - endIndex + startIndex - 1. |
1085 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1086 |
self grow:newSize |
|
602 | 1087 |
] |
1088 |
||
1089 |
"/ |newSize| |
|
1090 |
"/ |
|
1091 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1092 |
"/ newSize <= 0 ifTrue:[ |
|
1093 |
"/ self grow:0 |
|
1094 |
"/ ] ifFalse:[ |
|
1095 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1096 |
"/ self grow:newSize |
|
1097 |
"/ ] |
|
1098 |
||
1099 |
" |
|
8874 | 1100 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1101 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1102 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1103 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1104 |
" |
1105 |
! |
|
1106 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1107 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1108 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1109 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1110 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1111 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1112 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1113 |
Also note, that it may be a slow operation for some collections, |
8874 | 1114 |
due to the grow:-message, which is inefficient for fixed size |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1115 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1116 |
|
2363 | 1117 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1118 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1119 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1120 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1121 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1122 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1123 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1124 |
1 to:sz do:[:srcIndex | |
8874 | 1125 |
el := self at:srcIndex. |
1126 |
(anElement == el) ifTrue:[ |
|
1127 |
any := true |
|
1128 |
] ifFalse:[ |
|
1129 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1130 |
self at:dstIndex put:el |
|
1131 |
]. |
|
1132 |
dstIndex := dstIndex + 1 |
|
1133 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1134 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1135 |
any ifTrue:[ |
8874 | 1136 |
self grow:dstIndex - 1. |
1137 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1138 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1139 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1140 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1141 |
" |
8874 | 1142 |
#(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
|
1143 |
#(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
|
1144 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1145 |
|
2363 | 1146 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1147 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1148 |
|
602 | 1149 |
removeIndex:index |
1150 |
"remove the argument stored at index. Return the receiver. |
|
1151 |
||
1152 |
Notice, that this is modifies the receiver NOT a copy. |
|
1153 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1154 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1155 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1156 |
||
1157 |
self removeFromIndex:index toIndex:index |
|
1158 |
||
1159 |
" |
|
1160 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1161 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1162 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1163 |
" |
|
77 | 1164 |
! |
1165 |
||
602 | 1166 |
removeLast |
1167 |
"remove the last element of the receiver and return it. |
|
1168 |
||
1169 |
Notice, that this is modifies the receiver NOT a copy. |
|
1170 |
Also note, that it may be a slow operation for some collections, |
|
1171 |
due to the grow:-message, which is inefficient for fixed size |
|
1172 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1173 |
||
8874 | 1174 |
^ self removeAtIndex:(self size) |
602 | 1175 |
|
1176 |
" |
|
1177 |
|a| |
|
1178 |
a := #(1 2 3 4 5 6). |
|
1179 |
a removeLast. |
|
8874 | 1180 |
a |
602 | 1181 |
" |
1182 |
! ! |
|
1183 |
||
1184 |
!SequenceableCollection methodsFor:'comparing'! |
|
1185 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1186 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1187 |
"compare two sequenceable collections" |
8874 | 1188 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1189 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1190 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1191 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1192 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1193 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1194 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1195 |
aCollectionSize := aCollection size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1196 |
size < aCollectionSize ifTrue:[ |
8874 | 1197 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1198 |
] ifFalse:[ |
8874 | 1199 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1200 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1201 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1202 |
1 to: min do: [:i| |
8874 | 1203 |
v1 := self at: i. |
1204 |
v2 := aCollection at: i. |
|
1205 |
(v1 == v2 or:[v1 = v2]) ifFalse:[^ v1 < v2] |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1206 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1207 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1208 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1209 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1210 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1211 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1212 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1213 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1214 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1215 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1216 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1217 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1218 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1219 |
Time millisecondsToRun:[ |
8874 | 1220 |
1000000 timesRepeat:[ |
1221 |
a < b |
|
1222 |
] |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1223 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1224 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1225 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1226 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1227 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1228 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1229 |
Time millisecondsToRun:[ |
8874 | 1230 |
1000000 timesRepeat:[ |
1231 |
a < b |
|
1232 |
] |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1233 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1234 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1235 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1236 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1237 |
a := #[1 2 3 4 5 6 7 8 9 10 11]. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1238 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1239 |
Time millisecondsToRun:[ |
8874 | 1240 |
1000000 timesRepeat:[ |
1241 |
a < b |
|
1242 |
] |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1243 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1244 |
" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1245 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1246 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1247 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1248 |
"Compare the receiver with the argument and return true if the |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1249 |
receiver is less than or equal to the argument. Otherwise return false" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1250 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1251 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1252 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1253 |
|
602 | 1254 |
= aCollection |
1255 |
"return true if the receiver and aCollection represent collections |
|
1256 |
with equal contents." |
|
1257 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1258 |
| stop "{ Class: SmallInteger }" | |
602 | 1259 |
|
1260 |
(aCollection == self) ifTrue:[^true]. |
|
1261 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1262 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1263 |
|
1264 |
stop := self size. |
|
1265 |
stop == (aCollection size) ifFalse:[^false]. |
|
1266 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1267 |
1 to:stop do:[:index| |
9366 | 1268 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1269 |
]. |
1270 |
^ true |
|
1271 |
||
1272 |
" |
|
8874 | 1273 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1274 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1275 |
#($1 $2 $3 $4 $5) = '12345' |
|
1276 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1277 |
" |
1278 |
! |
|
1279 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1280 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1281 |
"Compare the receiver with the argument and return true if the |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1282 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1283 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1284 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1285 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1286 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1287 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1288 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1289 |
"Compare the receiver with the argument and return true if the |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1290 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1291 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1292 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1293 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1294 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1295 |
|
8874 | 1296 |
commonPrefixWith:aCollection |
6507 | 1297 |
"return the common prefix of myself and the argument, aCollection. |
1298 |
If there is none, an empty collection is returned." |
|
1299 |
||
1300 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1301 |
||
1302 |
" |
|
8874 | 1303 |
'hello' commonPrefixWith:'hello' |
1304 |
'hello' commonPrefixWith:'hElLo' |
|
1305 |
'hello' commonPrefixWith:'hello world' |
|
1306 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1307 |
||
1308 |
'hello world' commonPrefixWith:'hello' |
|
1309 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1310 |
||
1311 |
'abcd' commonPrefixWith:'bcde' |
|
1312 |
||
1313 |
'abcd' commonPrefixWith:'abab' |
|
1314 |
'abcd' commonPrefixWith:'ab' |
|
1315 |
'abcd' commonPrefixWith:'ababab' |
|
1316 |
'abcd' commonPrefixWith:'abcdef' |
|
1317 |
||
1318 |
'abab' commonPrefixWith:'abcd' |
|
1319 |
'ab' commonPrefixWith:'abcd' |
|
1320 |
'ababab' commonPrefixWith:'abcd' |
|
1321 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1322 |
" |
1323 |
! |
|
1324 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1325 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1326 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1327 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1328 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1329 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1330 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1331 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1332 |
1 to:matchLen do:[:idx | |
8874 | 1333 |
|elHere elThere same| |
1334 |
||
1335 |
elHere := self at:idx. |
|
1336 |
elThere := aCollection at:idx. |
|
1337 |
||
1338 |
ignoreCase ifTrue:[ |
|
1339 |
same := elHere sameAs:elThere |
|
1340 |
] ifFalse:[ |
|
1341 |
same := elHere = elThere |
|
1342 |
]. |
|
1343 |
same ifFalse:[ |
|
1344 |
^ self copyTo:(idx - 1). |
|
1345 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1346 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1347 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1348 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1349 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1350 |
" |
8874 | 1351 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1352 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1353 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1354 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1355 |
||
1356 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1357 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1358 |
||
1359 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1360 |
||
1361 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1362 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1363 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1364 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1365 |
||
1366 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1367 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1368 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1369 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1370 |
" |
|
1371 |
! |
|
1372 |
||
1373 |
commonSuffixWith:aCollection |
|
6507 | 1374 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1375 |
If there is none, an empty collection is returned." |
|
1376 |
||
1377 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1378 |
||
1379 |
" |
|
8874 | 1380 |
'hello' commonSuffixWith:'hello' |
1381 |
'hello' commonSuffixWith:'hElLo' |
|
1382 |
'hello' commonSuffixWith:'hello world' |
|
1383 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1384 |
'hello2 world' commonSuffixWith:'hello world' |
|
1385 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1386 |
||
1387 |
'hello world' commonSuffixWith:'world' |
|
1388 |
'hello WoRlD' commonSuffixWith:'world' |
|
1389 |
||
1390 |
'dcba' commonSuffixWith:'edcb' |
|
1391 |
||
1392 |
'dcba' commonSuffixWith:'baba' |
|
1393 |
'dcba' commonSuffixWith:'ba' |
|
1394 |
'dcba' commonSuffixWith:'bababa' |
|
1395 |
'dcba' commonSuffixWith:'fdcba' |
|
1396 |
||
1397 |
'baba' commonSuffixWith:'dcba' |
|
1398 |
'ba' commonSuffixWith:'dcba' |
|
1399 |
'bababa' commonSuffixWith:'dcba' |
|
1400 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1401 |
" |
1402 |
! |
|
1403 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1404 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1405 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1406 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1407 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1408 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1409 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1410 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1411 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1412 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1413 |
1 to:matchLen do:[:idx | |
8874 | 1414 |
|elHere elThere same| |
1415 |
||
1416 |
elHere := self at:(l1 - idx + 1). |
|
1417 |
elThere := aCollection at:(l2 - idx + 1). |
|
1418 |
||
1419 |
ignoreCase ifTrue:[ |
|
1420 |
same := elHere sameAs:elThere |
|
1421 |
] ifFalse:[ |
|
1422 |
same := elHere = elThere |
|
1423 |
]. |
|
1424 |
same ifFalse:[ |
|
1425 |
^ self copyFrom:(l1 - idx + 2). |
|
1426 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1427 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1428 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1429 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1430 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1431 |
" |
8874 | 1432 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1433 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1434 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1435 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1436 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1437 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1438 |
||
1439 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1440 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1441 |
||
1442 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1443 |
||
1444 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1445 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1446 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1447 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1448 |
||
1449 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1450 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1451 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1452 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1453 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1454 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1455 |
|
5813 | 1456 |
deepSameContentsAs:aCollection |
1457 |
"return true, if the receiver and the arg have the same contents |
|
1458 |
in both the named instance vars and any indexed instVars. |
|
1459 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1460 |
||
1461 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1462 |
unused space" |
|
1463 |
||
1464 |
|index "{ Class: SmallInteger }" |
|
1465 |
stop "{ Class: SmallInteger }" | |
|
1466 |
||
1467 |
(aCollection == self) ifTrue:[^true]. |
|
1468 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1469 |
||
1470 |
stop := self size. |
|
1471 |
stop == (aCollection size) ifFalse:[^false]. |
|
1472 |
||
1473 |
index := 1. |
|
1474 |
[index <= stop] whileTrue:[ |
|
8874 | 1475 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1476 |
index := index + 1 |
|
5813 | 1477 |
]. |
1478 |
^ true |
|
1479 |
||
1480 |
" |
|
8874 | 1481 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1482 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1483 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1484 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1485 |
" |
1486 |
! |
|
1487 |
||
602 | 1488 |
endsWith:aCollection |
1489 |
"return true, if the receivers last elements match those |
|
1490 |
of aCollection" |
|
1491 |
||
1492 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1493 |
index2 "{ Class: SmallInteger }" |
1494 |
stop "{ Class: SmallInteger }" |
|
602 | 1495 |
sz "{ Class: SmallInteger }"| |
1496 |
||
1497 |
(aCollection == self) ifTrue:[^ true]. |
|
1498 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1499 |
||
1500 |
stop := aCollection size. |
|
77 | 1501 |
sz := self size. |
602 | 1502 |
stop > sz ifTrue:[^false]. |
1503 |
||
1504 |
index1 := sz. |
|
1505 |
index2 := stop. |
|
1506 |
[index2 > 0] whileTrue:[ |
|
8874 | 1507 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1508 |
index1 := index1 - 1. |
|
1509 |
index2 := index2 - 1 |
|
77 | 1510 |
]. |
602 | 1511 |
^ true |
1512 |
||
1513 |
" |
|
1514 |
'abcde' endsWith:#($d $e) |
|
8874 | 1515 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1516 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1517 |
" |
|
1518 |
! |
|
1519 |
||
7278 | 1520 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1521 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1522 |
|
1523 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1524 |
||
1525 |
" |
|
1526 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1527 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1528 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1529 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1530 |
" |
1531 |
! |
|
1532 |
||
602 | 1533 |
hash |
1534 |
"return a hash key for the receiver" |
|
1535 |
||
1536 |
"this hash is stupid - but for larger collections, the hashing |
|
1537 |
time can become much bigger than the time lost in added probing. |
|
1538 |
Time will show ..." |
|
1539 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1540 |
|mySize h| |
3653 | 1541 |
|
1542 |
mySize := self size. |
|
1543 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1544 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1545 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1546 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1547 |
h := (h bitShift:5) + (self at:mySize) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1548 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1549 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1550 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1551 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1552 |
"/ cg: the code below may lead to largeInteger arithmetic, which was slow... |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1553 |
"/ ^ ((((self first hash bitShift:5) + self last hash) bitShift:5) + self size hash) bitAnd:16r3FFFFFFF. |
6749
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1554 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1555 |
" |
8874 | 1556 |
#(1 2 3 4 5 6) hash |
1557 |
(1 to:6) hash |
|
1558 |
||
1559 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1560 |
#(1 2 3 4 5) hash |
|
602 | 1561 |
" |
3653 | 1562 |
|
1563 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1564 |
! |
1565 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1566 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1567 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1568 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1569 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1570 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1571 |
^ self |
8874 | 1572 |
sameContentsAs:aCollection |
1573 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1574 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1575 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1576 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1577 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1578 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1579 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1580 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1581 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1582 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1583 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1584 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1585 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1586 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1587 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1588 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1589 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1590 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1591 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1592 |
" |
8874 | 1593 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1594 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1595 |
#($1 $2 $3 $4 $5) = '12345' |
|
1596 |
||
1597 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1598 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1599 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1600 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1601 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1602 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1603 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1604 |
|
6118 | 1605 |
isSameSequenceAs: otherCollection |
8874 | 1606 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1607 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1608 |
" |
|
1609 |
||
1610 |
| size | |
|
1611 |
||
1612 |
(size := self size) = otherCollection size ifFalse: [^false]. |
|
1613 |
||
1614 |
1 to: size do:[:index | |
|
8874 | 1615 |
(self at: index) = (otherCollection at: index) ifFalse: [^false] |
6118 | 1616 |
]. |
1617 |
^ true |
|
1618 |
! |
|
1619 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1620 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1621 |
"return true if the receiver and aCollection represent collections |
8874 | 1622 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1623 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1624 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1625 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1626 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1627 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1628 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1629 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1630 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1631 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1632 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1633 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1634 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1635 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1636 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1637 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1638 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1639 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1640 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1641 |
[index <= stop] whileTrue:[ |
8874 | 1642 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1643 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1644 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1645 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1646 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1647 |
" |
8874 | 1648 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
1649 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1650 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1651 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1652 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1653 |
#('Hello' 'ABC' 'worlD') sameContentsAs: #('Hello' 'ABC' 'worlD') whenComparedWith:[:a :b | a sameAs:b] |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1654 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1655 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1656 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1657 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1658 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1659 |
|
602 | 1660 |
startsWith:aCollection |
1661 |
"return true, if the receivers first elements match those |
|
4389
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1662 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1663 |
If the argument is empty, true is returned." |
602 | 1664 |
|
1665 |
|index "{ Class: SmallInteger }" |
|
1666 |
stop "{ Class: SmallInteger }" | |
|
1667 |
||
1668 |
(aCollection == self) ifTrue:[^true]. |
|
1669 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1670 |
||
1671 |
stop := aCollection size. |
|
1672 |
stop > self size ifTrue:[^false]. |
|
1673 |
||
1674 |
index := 1. |
|
1675 |
[index <= stop] whileTrue:[ |
|
8874 | 1676 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1677 |
index := index + 1 |
|
602 | 1678 |
]. |
1679 |
^ true |
|
1680 |
||
1681 |
" |
|
1682 |
'abcde' startsWith:#($a $b $c) |
|
1683 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1684 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1685 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 1686 |
" |
7279 | 1687 |
! |
1688 |
||
1689 |
startsWithAnyOf:aCollectionOfCollections |
|
1690 |
"return true, if the receiver starts with any in aCollection" |
|
1691 |
||
1692 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
1693 |
||
1694 |
" |
|
1695 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
1696 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 1697 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
1698 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 1699 |
" |
13 | 1700 |
! ! |
1701 |
||
260 | 1702 |
!SequenceableCollection methodsFor:'converting'! |
1703 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1704 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 1705 |
"return a collection containing the subcollections (separated by anElement) |
1706 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1707 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1708 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1709 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1710 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1711 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1712 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1713 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1714 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1715 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1716 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1717 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1718 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1719 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1720 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1721 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1722 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1723 |
1 to:numberOfCols do:[:lineNr | |
8874 | 1724 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
1725 |
stopIndex == 0 ifTrue:[ |
|
1726 |
stopIndex := self size |
|
1727 |
] ifFalse: [ |
|
1728 |
stopIndex := stopIndex - 1. |
|
1729 |
]. |
|
1730 |
||
1731 |
(stopIndex < startIndex) ifTrue: [ |
|
1732 |
cols add:(myClass new:0) |
|
1733 |
] ifFalse: [ |
|
1734 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
1735 |
]. |
|
1736 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1737 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1738 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1739 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1740 |
" |
8874 | 1741 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1742 |
'1 one 2 two 3 three 4 four 5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:Character space |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1743 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1744 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1745 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1746 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1747 |
|
9234 | 1748 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
1749 |
"evaluate aBlock for each subcollection generated by separating elements |
|
1750 |
of the receiver by anElement. |
|
1751 |
If anElement occurs multiple times in a row, |
|
1752 |
the block will be invoked with empty collections as argument. |
|
1753 |
This algorithm uses equality-compare to detect the element." |
|
1754 |
||
1755 |
|subCollection |
|
1756 |
endIndex "{ Class:SmallInteger }" |
|
1757 |
startIndex "{ Class:SmallInteger }" |
|
1758 |
stopIndex "{ Class:SmallInteger }" | |
|
1759 |
||
1760 |
startIndex := 0. |
|
1761 |
endIndex := self size. |
|
1762 |
||
1763 |
[startIndex <= endIndex] whileTrue:[ |
|
1764 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
1765 |
stopIndex == 0 ifTrue:[ |
|
1766 |
stopIndex := self size |
|
1767 |
] ifFalse: [ |
|
1768 |
stopIndex := stopIndex - 1. |
|
1769 |
]. |
|
1770 |
||
1771 |
(stopIndex < startIndex) ifTrue: [ |
|
1772 |
subCollection := self species new:0 |
|
1773 |
] ifFalse: [ |
|
1774 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
1775 |
]. |
|
1776 |
aBlock value:subCollection. |
|
1777 |
startIndex := stopIndex + 1 |
|
1778 |
]. |
|
1779 |
||
1780 |
" |
|
1781 |
'' |
|
1782 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1783 |
||
1784 |
'1 one' |
|
1785 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1786 |
||
1787 |
'1 one:2 two:3 three:4 four:5 five' |
|
1788 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1789 |
||
1790 |
'a::b' |
|
1791 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1792 |
||
1793 |
':' |
|
1794 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1795 |
||
1796 |
':a' |
|
1797 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1798 |
||
1799 |
'a:' |
|
1800 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1801 |
" |
|
1802 |
! |
|
1803 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1804 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 1805 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
1806 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1807 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1808 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1809 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1810 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1811 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1812 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1813 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1814 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1815 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1816 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1817 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1818 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1819 |
[done] whileFalse:[ |
8874 | 1820 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
1821 |
stopIndex == 0 ifTrue:[ |
|
1822 |
stopIndex := self size. |
|
1823 |
done := true. |
|
1824 |
] ifFalse: [ |
|
1825 |
stopIndex := stopIndex - 1. |
|
1826 |
]. |
|
1827 |
||
1828 |
(stopIndex < startIndex) ifTrue: [ |
|
1829 |
items add:(myClass new:0) |
|
1830 |
] ifFalse: [ |
|
1831 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
1832 |
]. |
|
1833 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1834 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1835 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1836 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1837 |
" |
8874 | 1838 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
1839 |
#(1 2 3 1 2 3 4 3 1 1 2 3 1 4 5) asCollectionOfSubCollectionsSeparatedByAll:#(3 1) |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1840 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1841 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1842 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1843 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 1844 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1845 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1846 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1847 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1848 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1849 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1850 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1851 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1852 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 1853 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
1854 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
1855 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
1856 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
1857 |
#(1 9 2 8 3 7 4 6 5 5) asCollectionOfSubCollectionsSeparatedByAny:#(1 2 3) |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1858 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1859 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1860 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1861 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 1862 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1863 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1864 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1865 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1866 |
|words |
8874 | 1867 |
start "{ Class:SmallInteger }" |
1868 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1869 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1870 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1871 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1872 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1873 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1874 |
[start <= mySize] whileTrue:[ |
8874 | 1875 |
"skip multiple separators" |
1876 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
1877 |
start := start + 1 . |
|
1878 |
start > mySize ifTrue:[ |
|
1879 |
^ words |
|
1880 |
]. |
|
1881 |
]. |
|
1882 |
||
1883 |
stop := self findFirst:aBlock startingAt:start. |
|
1884 |
stop == 0 ifTrue:[ |
|
1885 |
words add:(self copyFrom:start to:mySize). |
|
1886 |
^ words |
|
1887 |
]. |
|
1888 |
words add:(self copyFrom:start to:(stop - 1)). |
|
1889 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1890 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1891 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1892 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1893 |
" |
8874 | 1894 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
1895 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1896 |
#(1 9 2 8 3 7 4 6 5 5) asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:n | n odd] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1897 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1898 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1899 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1900 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1901 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1902 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1903 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1904 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1905 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1906 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1907 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1908 |
|
1413 | 1909 |
asStringCollection |
1910 |
"return a new string collection containing the elements; |
|
1911 |
these ought to be strings. (i.e. String or Text instances)" |
|
1912 |
||
1913 |
|newColl sz| |
|
1914 |
||
1915 |
sz := self size. |
|
9162 | 1916 |
newColl := StringCollection withSize:sz. |
1413 | 1917 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
1918 |
^ newColl |
|
1919 |
||
1920 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
1921 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
1922 |
! |
|
1923 |
||
1188 | 1924 |
asStringWith:sepChar |
8874 | 1925 |
"return a string generated by concatenating my elements |
1188 | 1926 |
(which must be strings or nil) and embedding sepChar characters in between. |
1927 |
Nil entries and empty strings are counted as empty lines." |
|
1928 |
||
8874 | 1929 |
^ self |
1930 |
from:1 to:(self size) |
|
1931 |
asStringWith:sepChar |
|
1932 |
compressTabs:false |
|
1933 |
final:nil |
|
1413 | 1934 |
|
1935 |
" |
|
1936 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
1937 |
" |
|
1938 |
||
1939 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 1940 |
! |
1941 |
||
1942 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
1943 |
"return part of myself as a string with embedded sepCharacters. |
|
1944 |
My elements must be strings or nil; nil entries and empty strings are |
|
1945 |
taken as empty lines." |
|
1946 |
||
8874 | 1947 |
^ self |
1948 |
from:firstLine to:lastLine |
|
1949 |
asStringWith:sepCharacter |
|
1950 |
compressTabs:false |
|
1951 |
final:nil |
|
1188 | 1952 |
" |
1953 |
creating entries for searchpath: |
|
1954 |
||
8874 | 1955 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
1956 |
||
1957 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 1958 |
" |
1959 |
||
1960 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
1961 |
! |
|
1962 |
||
260 | 1963 |
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
|
1964 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 1965 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 1966 |
taken as empty lines. |
1967 |
If the argument compressTabs is true, leading spaces are converted |
|
1968 |
to tab-characters (8col tabs). The last line is followed by a final |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1969 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1970 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1971 |
^ self |
8874 | 1972 |
from:firstLine to:lastLine |
1973 |
asStringWith:sepCharacter |
|
1974 |
compressTabs:compressTabs |
|
1975 |
final:endCharacter |
|
1976 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1977 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1978 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1979 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1980 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1981 |
asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
1982 |
"return part of myself as a string or text with embedded sepCharacters |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
1983 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1984 |
My elements must be strings or nil; nil entries and empty strings are |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1985 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
1986 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1987 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1988 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 1989 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1990 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1991 |
any emphasis. If false, a plain string is returned. |
260 | 1992 |
This method is tuned for big collections, in not creating many |
263 | 1993 |
intermediate strings (has linear runtime). For very small collections |
1994 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 1995 |
|
5575 | 1996 |
^ self |
8874 | 1997 |
from:firstLine to:lastLine |
1998 |
asStringWith:sepCharacter |
|
1999 |
compressTabs:compressTabs |
|
2000 |
final:endCharacter |
|
2001 |
withEmphasis:withEmphasis |
|
5575 | 2002 |
! |
2003 |
||
2004 |
asStringWithCRs |
|
8874 | 2005 |
"return a string generated by concatenating my elements |
5575 | 2006 |
(which must be strings or nil) and embedding cr characters in between. |
2007 |
Nil entries and empty strings are counted as empty lines." |
|
2008 |
||
2009 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2010 |
||
2011 |
" |
|
8874 | 2012 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2013 |
|
2014 |
(OrderedCollection new |
|
8874 | 2015 |
add:'hello'; |
2016 |
add:'world'; |
|
2017 |
add:'foo'; |
|
2018 |
add:('bar' asText allBold); |
|
2019 |
yourself) asStringWithCRs |
|
5575 | 2020 |
|
2021 |
Transcript showCR: |
|
8874 | 2022 |
(OrderedCollection new |
2023 |
add:'hello'; |
|
2024 |
add:'world'; |
|
2025 |
add:'foo'; |
|
2026 |
add:('bar' asText allBold); |
|
2027 |
yourself) asStringWithCRs |
|
5575 | 2028 |
" |
2029 |
||
2030 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2031 |
! |
|
2032 |
||
2033 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2034 |
"return a string generated by concatenating some of my elements |
5575 | 2035 |
(which must be strings or nil) and embedding cr characters in between. |
2036 |
Nil entries and empty strings are counted as empty lines." |
|
2037 |
||
2038 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2039 |
||
2040 |
" |
|
8874 | 2041 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2042 |
|
2043 |
" |
|
2044 |
||
2045 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2046 |
! |
|
2047 |
||
2048 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2049 |
"return part of myself as a string with embedded cr's. |
|
2050 |
My elements must be strings or nil. |
|
2051 |
If the argument compressTabs is true, leading spaces are converted |
|
2052 |
to tab-characters (8col tabs). |
|
2053 |
Nil entries and empty strings are taken as empty lines." |
|
2054 |
||
2055 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2056 |
! |
|
2057 |
||
2058 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2059 |
"return part of myself as a string with embedded cr's. |
|
2060 |
My elements must be strings or nil; nil entries and empty strings are |
|
2061 |
taken as empty lines. |
|
2062 |
If the argument compressTabs is true, leading spaces are converted |
|
2063 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2064 |
should be followed by a cr or not." |
|
2065 |
||
2066 |
^ self asStringWith:(Character cr) |
|
8874 | 2067 |
from:firstLine |
2068 |
to:lastLine |
|
2069 |
compressTabs:compressTabs |
|
2070 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2071 |
! |
2072 |
||
2073 |
decodeAsLiteralArray |
|
2074 |
"given a literalEncoding in the receiver, |
|
2075 |
create & return the corresponding object. |
|
2076 |
The inverse operation to #literalArrayEncoding." |
|
2077 |
||
9414 | 2078 |
|clsName cls| |
5575 | 2079 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2080 |
self size == 0 ifTrue:[ |
9358 | 2081 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2082 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2083 |
|
9414 | 2084 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2085 |
clsName isSymbol ifFalse:[ |
9358 | 2086 |
^ MissingClassInLiteralArrayErrorSignal |
2087 |
raiseRequestWith:clsName |
|
9414 | 2088 |
errorString:('non-symbol className in literalArray-spec: ', clsName printString) |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2089 |
]. |
9414 | 2090 |
cls := Smalltalk classNamed:clsName. |
5575 | 2091 |
cls isNil ifTrue:[ |
9414 | 2092 |
^ MissingClassInLiteralArrayErrorSignal |
2093 |
raiseRequestWith:clsName |
|
2094 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2095 |
]. |
9414 | 2096 |
|
5575 | 2097 |
^ cls decodeFromLiteralArray:self. |
2098 |
||
2099 |
" |
|
8874 | 2100 |
#(Point 10 20) decodeAsLiteralArray |
2101 |
#(Rectangle 100 200 400 500) decodeAsLiteralArray |
|
2102 |
#(#MenuItem #rawLabel: 'right' #value: #right) decodeAsLiteralArray |
|
5575 | 2103 |
" |
2104 |
||
2105 |
"Modified: / 18.5.1999 / 14:51:58 / cg" |
|
2106 |
! |
|
2107 |
||
8874 | 2108 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2109 |
"return part of myself as a string with embedded sepCharacters. |
2110 |
My elements must be strings or nil; nil entries and empty strings are |
|
2111 |
taken as empty lines." |
|
2112 |
||
8874 | 2113 |
^ self |
2114 |
from:firstLine |
|
2115 |
to:lastLine |
|
2116 |
asStringWith:sepCharacter |
|
2117 |
compressTabs:false |
|
2118 |
final:nil |
|
5575 | 2119 |
" |
2120 |
creating entries for searchpath: |
|
2121 |
||
8874 | 2122 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2123 |
||
2124 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2125 |
||
2126 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2127 |
" |
2128 |
||
2129 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2130 |
! |
|
2131 |
||
2132 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2133 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2134 |
My elements must be strings or nil; nil entries and empty strings are |
|
2135 |
taken as empty lines. |
|
2136 |
If the argument compressTabs is true, leading spaces are converted |
|
2137 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2138 |
character (if non-nil)." |
|
2139 |
||
2140 |
^ self |
|
8874 | 2141 |
from:firstLine |
2142 |
to:lastLine |
|
2143 |
asStringWith:sepCharacter |
|
2144 |
compressTabs:compressTabs |
|
2145 |
final:endCharacter |
|
2146 |
withEmphasis:true |
|
5575 | 2147 |
|
2148 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2149 |
! |
|
2150 |
||
2151 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2152 |
"return part of myself as a string or text with embedded sepCharacters |
|
2153 |
and followup endCharacter. |
|
2154 |
My elements must be strings or nil; nil entries and empty strings are |
|
2155 |
taken as empty lines. |
|
2156 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2157 |
If the argument compressTabs is true, leading spaces are converted |
|
2158 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2159 |
character (if non-nil). |
|
2160 |
The withEmphais argument controls if the returned string should preserve |
|
2161 |
any emphasis. If false, a plain string is returned. |
|
2162 |
This method is tuned for big collections, in not creating many |
|
2163 |
intermediate strings (has linear runtime). For very small collections |
|
2164 |
and small strings, it may be faster to use the comma , operation." |
|
2165 |
||
293 | 2166 |
|idx1 "{ Class:SmallInteger }" |
2167 |
idx2 "{ Class:SmallInteger }" |
|
2168 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2169 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2170 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2171 |
newString lineString spaces idx nTabs |
8099 | 2172 |
maxBitsPerCharacter stringClass needEmphasis newRuns c |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2173 |
thisLen anyTab| |
260 | 2174 |
|
2175 |
" |
|
2176 |
first accumulate the size of the string, to avoid |
|
2177 |
countless reallocations. If tabs are compressed, |
|
2178 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2179 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2180 |
" |
8099 | 2181 |
needEmphasis := false. |
2182 |
maxBitsPerCharacter := 8. |
|
1015 | 2183 |
|
260 | 2184 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2185 |
sepCharacter isNil ifTrue:[ |
8874 | 2186 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2187 |
] ifFalse:[ |
8874 | 2188 |
sepCharacter isCharacter ifTrue:[ |
2189 |
sepCnt := 1 |
|
2190 |
] ifFalse:[ |
|
2191 |
sepCnt := sepCharacter size |
|
2192 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2193 |
]. |
260 | 2194 |
|
293 | 2195 |
idx1 := firstLine. |
2196 |
idx2 := lastLine. |
|
2197 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2198 |
lineString := self at:lineIndex. |
2199 |
||
2200 |
lineString notNil ifTrue:[ |
|
2201 |
withEmphasis ifTrue:[ |
|
2202 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2203 |
needEmphasis := true |
|
2204 |
]. |
|
2205 |
]. |
|
2206 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2207 |
totalLength := totalLength + lineString size |
|
2208 |
]. |
|
2209 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2210 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2211 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2212 |
|
8099 | 2213 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2214 |
maxBitsPerCharacter > 16 ifTrue:[ |
2215 |
stringClass := Unicode32String. |
|
2216 |
] ifFalse:[ |
|
2217 |
stringClass := Unicode16String. |
|
2218 |
] |
|
8099 | 2219 |
] ifFalse:[ |
8874 | 2220 |
stringClass := String. |
8099 | 2221 |
]. |
2222 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2223 |
endCharacter notNil ifTrue:[ |
8874 | 2224 |
endCharacter isCharacter ifTrue:[ |
2225 |
totalLength := totalLength + 1 |
|
2226 |
] ifFalse:[ |
|
2227 |
totalLength := totalLength + endCharacter size |
|
2228 |
]. |
|
260 | 2229 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2230 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2231 |
|
260 | 2232 |
spaces := ' '. |
1016 | 2233 |
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
|
2234 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2235 |
needEmphasis ifTrue:[ |
8874 | 2236 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2237 |
]. |
260 | 2238 |
|
2239 |
" |
|
2240 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2241 |
Be careful with runArrays: |
8874 | 2242 |
replacing individual elements is VERY expensive. |
2243 |
Therefore, create a new runArray from scratch. |
|
260 | 2244 |
" |
2245 |
pos := 1. |
|
293 | 2246 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2247 |
lineString := self at:lineIndex. |
2248 |
thisLen := lineString size. |
|
2249 |
thisLen ~~ 0 ifTrue:[ |
|
2250 |
withEmphasis ifFalse:[ |
|
2251 |
lineString := lineString string. |
|
2252 |
]. |
|
2253 |
||
2254 |
(anyTab := compressTabs) ifTrue:[ |
|
2255 |
" |
|
2256 |
mhmh: could use withTabs from String-class here, |
|
2257 |
but we should avoid creating too many temporary strings |
|
2258 |
(especially, since this method is typically used when converting |
|
2259 |
big texts such as when saving in the filebrowser ...). |
|
2260 |
Therefore, we convert tabs inline here doing a direct replace |
|
2261 |
in newString." |
|
2262 |
||
2263 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2264 |
nTabs := (idx-1) // 8. |
|
2265 |
anyTab := (nTabs > 0) |
|
2266 |
]. |
|
2267 |
anyTab ifTrue:[ |
|
2268 |
"any tabs" |
|
2269 |
||
2270 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2271 |
||
2272 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2273 |
newRuns notNil ifTrue:[ |
|
2274 |
newRuns add:nil withOccurrences:nTabs |
|
2275 |
]. |
|
2276 |
pos := pos + nTabs. |
|
2277 |
||
2278 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2279 |
newRuns notNil ifTrue:[ |
|
2280 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2281 |
idx to:lineString size do:[:pos | |
|
2282 |
newRuns add:(lineString emphasisAt:pos) |
|
2283 |
] |
|
2284 |
] ifFalse:[ |
|
2285 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2286 |
] |
|
2287 |
]. |
|
2288 |
pos := pos + thisLen - (nTabs * 8). |
|
2289 |
] ifFalse:[ |
|
2290 |
newString replaceFrom:pos with:lineString. |
|
2291 |
||
2292 |
newRuns notNil ifTrue:[ |
|
2293 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2294 |
newRuns addAll:(lineString emphasis) |
|
2295 |
] ifFalse:[ |
|
2296 |
newRuns add:nil withOccurrences:lineString size |
|
2297 |
] |
|
2298 |
]. |
|
2299 |
pos := pos + thisLen. |
|
2300 |
]. |
|
2301 |
]. |
|
2302 |
||
2303 |
lineIndex ~~ lastLine ifTrue:[ |
|
2304 |
c := sepCharacter |
|
2305 |
] ifFalse:[ |
|
2306 |
c := endCharacter |
|
2307 |
]. |
|
2308 |
||
2309 |
c notNil ifTrue:[ |
|
2310 |
c isCharacter ifTrue:[ |
|
2311 |
newString at:pos put:c. |
|
2312 |
newRuns notNil ifTrue:[ |
|
2313 |
newRuns add:nil. |
|
2314 |
]. |
|
2315 |
pos := pos + 1 |
|
2316 |
] ifFalse:[ |
|
2317 |
newString replaceFrom:pos with:c. |
|
2318 |
newRuns notNil ifTrue:[ |
|
2319 |
newRuns add:nil withOccurrences:c size |
|
2320 |
]. |
|
2321 |
pos := pos + c size |
|
2322 |
] |
|
2323 |
]. |
|
260 | 2324 |
]. |
2325 |
||
2326 |
" |
|
2327 |
in case of tab compression, the result has to be |
|
2328 |
cut to size ... sorry |
|
2329 |
" |
|
2330 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2331 |
newString := newString copyTo:(pos - 1) |
260 | 2332 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2333 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2334 |
newRuns notNil ifTrue:[ |
8874 | 2335 |
newString := Text string:newString runs:newRuns. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2336 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2337 |
|
260 | 2338 |
^ newString |
2339 |
||
2340 |
" |
|
2341 |
creating entries for searchpath: |
|
2342 |
||
8874 | 2343 |
#('foo' 'bar' 'baz' '/foo/bar') |
2344 |
asStringWith:$: |
|
2345 |
from:1 to:4 |
|
2346 |
compressTabs:false |
|
2347 |
final:nil |
|
260 | 2348 |
|
2349 |
with trailing colon: |
|
2350 |
||
8874 | 2351 |
#('foo' 'bar' 'baz' '/foo/bar') |
2352 |
asStringWith:$: |
|
2353 |
from:1 to:4 |
|
2354 |
compressTabs:false |
|
2355 |
final:$: |
|
260 | 2356 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2357 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2358 |
|
8874 | 2359 |
#('foo' 'bar' 'baz') |
2360 |
asStringWith:nil |
|
2361 |
from:1 to:3 |
|
2362 |
compressTabs:false |
|
2363 |
final:nil |
|
260 | 2364 |
|
2365 |
creating a string from a collection of lines: |
|
2366 |
||
8874 | 2367 |
#('foo' 'bar' 'baz') |
2368 |
asStringWith:(Character cr) |
|
2369 |
from:1 to:3 |
|
2370 |
compressTabs:false |
|
2371 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2372 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2373 |
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
|
2374 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2375 |
(Array |
8874 | 2376 |
with:'foo' |
2377 |
with:('bar' asText allBold) |
|
2378 |
with:'baz' |
|
2379 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2380 |
) |
|
2381 |
asStringWith:(Character cr) |
|
2382 |
from:1 to:4 |
|
2383 |
compressTabs:false |
|
2384 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2385 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2386 |
can also use strings as separating characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2387 |
|
8874 | 2388 |
#('foo' 'bar' 'baz') |
2389 |
asStringWith:'__' |
|
2390 |
from:1 to:3 |
|
2391 |
compressTabs:false |
|
2392 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2393 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2394 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2395 |
|
8874 | 2396 |
#('foo' 'bar' 'baz') |
2397 |
asStringWith:'__' |
|
2398 |
from:1 to:3 |
|
2399 |
compressTabs:false |
|
2400 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2401 |
|
260 | 2402 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2403 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2404 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2405 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
260 | 2406 |
! ! |
2407 |
||
7253 | 2408 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2409 |
||
8874 | 2410 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2411 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2412 |
The new collection represents the elements starting at startIndex to the end. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2413 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2414 |
however, physically, no copy is made). |
9291 | 2415 |
Warning: |
2416 |
The slice SHARES the memory for the element-data with the original, |
|
2417 |
this means that any modifications in the original are visible in the slice |
|
2418 |
and vice versa." |
|
7253 | 2419 |
|
2420 |
^ self |
|
9291 | 2421 |
from:startIndex |
2422 |
to:self size |
|
2423 |
by:1 |
|
8874 | 2424 |
|
2425 |
" |
|
2426 |
#(1 2 3 4 5 6 7) from:3 |
|
2427 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2428 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2429 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2430 |
" |
|
2431 |
! |
|
2432 |
||
2433 |
from:startIndex by:step |
|
9291 | 2434 |
"Create a ReindexedCollection from the receiver. |
2435 |
The new collection represents the elements starting at startIndex to the end. |
|
2436 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2437 |
however, physically, no copy is made). |
|
2438 |
Warning: |
|
2439 |
The slice SHARES the memory for the element-data with the original, |
|
2440 |
this means that any modifications in the original are visible in the slice |
|
2441 |
and vice versa." |
|
7253 | 2442 |
|
2443 |
^ self |
|
9291 | 2444 |
from:startIndex |
2445 |
to:(step > 1 |
|
2446 |
ifTrue: [self size] |
|
2447 |
ifFalse: [1]) |
|
2448 |
by:step |
|
8874 | 2449 |
|
2450 |
" |
|
2451 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2452 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2453 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2454 |
( #(1 2 3 4 5 6 7) from:3 by:2) size |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2455 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2456 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2457 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2458 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2459 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2460 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2461 |
Warning: |
2462 |
The slice SHARES the memory for the element-data with the original, |
|
2463 |
this means that any modifications in the original are visible in the slice |
|
2464 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2465 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2466 |
^ self |
9291 | 2467 |
from:startIndex |
2468 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2469 |
|
2470 |
" |
|
2471 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2472 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2473 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2474 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2475 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2476 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2477 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2478 |
coll := #(1 2 3 4 5 6 7 8 9 10) copy. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2479 |
slice := coll from:3 to:7. |
8874 | 2480 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2481 |
coll at:4 put:40. |
8874 | 2482 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2483 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2484 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2485 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2486 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2487 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2488 |
coll := #(1 2 3 4 5 6 7 8 9 10) copy. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2489 |
slice := coll from:3 to:7. |
8874 | 2490 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2491 |
slice at:1 put:40. |
8874 | 2492 |
slice. |
2493 |
" |
|
2494 |
! |
|
2495 |
||
2496 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2497 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2498 |
The new collection represents the elements starting at startIndex to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2499 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2500 |
however, physically, no copy is made). |
9291 | 2501 |
Warning: |
2502 |
The slice SHARES the memory for the element-data with the original, |
|
2503 |
this means that any modifications in the original are visible in the slice |
|
2504 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2505 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2506 |
^ self |
9291 | 2507 |
from:startIndex |
2508 |
to:endIndex |
|
2509 |
by: (startIndex <= endIndex |
|
2510 |
ifTrue: [1] |
|
2511 |
ifFalse: [-1]) |
|
8874 | 2512 |
|
2513 |
" |
|
2514 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2515 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2516 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2517 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2518 |
" |
|
2519 |
! |
|
2520 |
||
2521 |
from:startIndex to:endIndex by:step |
|
9291 | 2522 |
"Create a ReindexedCollection from the receiver. |
2523 |
The new collection represents the receivers elements up to endIndex. |
|
2524 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2525 |
however, physically, no copy is made). |
|
2526 |
Warning: |
|
2527 |
The slice SHARES the memory for the element-data with the original, |
|
2528 |
this means that any modifications in the original are visible in the slice |
|
2529 |
and vice versa." |
|
7253 | 2530 |
|
2531 |
^ ReindexedCollection |
|
9291 | 2532 |
on:self |
2533 |
from:startIndex |
|
2534 |
to:endIndex |
|
2535 |
by:step |
|
8874 | 2536 |
|
2537 |
" |
|
2538 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2539 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2540 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2541 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2542 |
" |
|
2543 |
! |
|
2544 |
||
2545 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2546 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2547 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2548 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2549 |
however, physically, no copy is made). |
9291 | 2550 |
Warning: |
2551 |
The slice SHARES the memory for the element-data with the original, |
|
2552 |
this means that any modifications in the original are visible in the slice |
|
2553 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2554 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2555 |
^ self |
9291 | 2556 |
from:1 |
2557 |
to:endIndex |
|
2558 |
by:1 |
|
8874 | 2559 |
|
2560 |
" |
|
2561 |
#(1 2 3 4 5 6 7) to:4 |
|
2562 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2563 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2564 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2565 |
" |
|
2566 |
! |
|
2567 |
||
2568 |
to:endIndex by:step |
|
9291 | 2569 |
"Create a ReindexedCollection from the receiver. |
2570 |
The new collection represents the receivers elements up to endIndex. |
|
2571 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2572 |
however, physically, no copy is made). |
|
2573 |
Warning: |
|
2574 |
The slice SHARES the memory for the element-data with the original, |
|
2575 |
this means that any modifications in the original are visible in the slice |
|
2576 |
and vice versa." |
|
7253 | 2577 |
|
2578 |
^ self |
|
9291 | 2579 |
from:(step > 0 |
2580 |
ifTrue: [1] |
|
2581 |
ifFalse: [self size]) |
|
2582 |
to:endIndex |
|
2583 |
by:step |
|
8874 | 2584 |
|
2585 |
" |
|
2586 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2587 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2588 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2589 |
( #(1 2 3 4 5 6 7) to:4 by:2) size |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2590 |
" |
7253 | 2591 |
! ! |
2592 |
||
1 | 2593 |
!SequenceableCollection methodsFor:'copying'! |
2594 |
||
9106 | 2595 |
++ aCollection |
2596 |
"return a new collection formed from concatenating the receiver with |
|
2597 |
the argument. The class of the new collection is determined by the |
|
2598 |
receivers class, so mixing classes is possible, if the second collections |
|
2599 |
elements can be stored into instances of the receivers class." |
|
2600 |
||
2601 |
^ self , aCollection |
|
2602 |
||
2603 |
" |
|
2604 |
#($a $b $c) ++ #(1 2 3) |
|
2605 |
#($a $b $c) ++ '123' |
|
2606 |
'abc' ++ '123' |
|
2607 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2608 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2609 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2610 |
" |
|
2611 |
! |
|
2612 |
||
1 | 2613 |
, aCollection |
2614 |
"return a new collection formed from concatenating the receiver with |
|
44 | 2615 |
the argument. The class of the new collection is determined by the |
2616 |
receivers class, so mixing classes is possible, if the second collections |
|
2617 |
elements can be stored into instances of the receivers class." |
|
1 | 2618 |
|
8874 | 2619 |
|newCollection |
1 | 2620 |
mySize "{ Class: SmallInteger }" |
2621 |
newSize "{ Class: SmallInteger }" |
|
2622 |
otherSize "{ Class: SmallInteger }" |
|
2623 |
dstIndex "{ Class: SmallInteger }"| |
|
2624 |
||
2625 |
mySize := self size. |
|
2626 |
otherSize := aCollection size. |
|
2627 |
newSize := mySize + otherSize. |
|
202 | 2628 |
|
252 | 2629 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 2630 |
|
2631 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
2632 |
dstIndex := mySize + 1. |
|
202 | 2633 |
aCollection isSequenceable ifTrue:[ |
8874 | 2634 |
" |
2635 |
yes, aCollection has indexed elements |
|
2636 |
" |
|
2637 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 2638 |
] ifFalse:[ |
8874 | 2639 |
" |
2640 |
no, enumerate aCollection |
|
2641 |
" |
|
2642 |
aCollection do:[:element | |
|
2643 |
newCollection at:dstIndex put:element. |
|
2644 |
dstIndex := dstIndex + 1 |
|
2645 |
] |
|
1 | 2646 |
]. |
2647 |
^ newCollection |
|
44 | 2648 |
|
95 | 2649 |
" |
2650 |
#($a $b $c) , #(1 2 3) |
|
2651 |
#($a $b $c) , '123' |
|
2652 |
'abc' , '123' |
|
2653 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
2654 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2655 |
'abc' asArray , #(1 2 3 4 5) |
|
2656 |
" |
|
1 | 2657 |
! |
2658 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2659 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2660 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2661 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2662 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2663 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2664 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2665 |
" |
8874 | 2666 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2667 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2668 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2669 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2670 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2671 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2672 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2673 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2674 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2675 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2676 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2677 |
" |
8874 | 2678 |
#($a $b $c $d $e $f $g) copyButLast:2 |
2679 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2680 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2681 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2682 |
|
602 | 2683 |
copyFirst:count |
2684 |
"return a new collection consisting of the receivers first count |
|
2685 |
elements - this is just a rename of copyTo: - for compatibility." |
|
2686 |
||
2687 |
^ self copyFrom:1 to:count |
|
2688 |
||
2689 |
" |
|
2690 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
2691 |
'1234567890' copyFirst:4 |
|
2692 |
" |
|
2693 |
! |
|
2694 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2695 |
copyFrom:startIndex |
9234 | 2696 |
"return a new collection consisting of receivers elements from startIndex to the end of the collection. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2697 |
Return an empty collection, if startIndex is beyond the receivers size." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2698 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2699 |
^ self copyFrom:startIndex to:(self size) |
602 | 2700 |
|
2701 |
" |
|
2702 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
2703 |
'1234567890' copyFrom:2 |
|
8874 | 2704 |
(10 to:19) copyFrom:5 |
602 | 2705 |
" |
2706 |
! |
|
2707 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2708 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2709 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2710 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2711 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2712 |
Returns an empty collection if startIndex is beyond the receivers size. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2713 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2714 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2715 |
^ self |
8874 | 2716 |
copyFrom:startIndex |
2717 |
to:(startIndex + numberOfElements - 1) |
|
2718 |
||
2719 |
" |
|
2720 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
2721 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2722 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2723 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2724 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2725 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2726 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2727 |
copyFrom:startIndex to:stopIndex |
602 | 2728 |
"return a new collection consisting of receivers elements |
4403 | 2729 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2730 |
Returns an empty collection if startIndex is beyond the receivers size. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2731 |
Raise an error, if stopIndex is out of range." |
602 | 2732 |
|
2733 |
|newCollection newSize| |
|
2734 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2735 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2736 |
newSize < 0 ifTrue:[ |
8874 | 2737 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2738 |
]. |
602 | 2739 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2740 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 2741 |
^ newCollection |
2742 |
||
2743 |
" |
|
2744 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
2745 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 2746 |
(1 to:10) copyFrom:2 to:5 |
2747 |
||
2748 |
'1234567890' copyFrom:2 to:15 |
|
2749 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 2750 |
" |
2751 |
! |
|
2752 |
||
2753 |
copyLast:count |
|
2754 |
"return a new collection consisting of the receivers last count |
|
2755 |
elements." |
|
2756 |
||
2757 |
|sz| |
|
2758 |
||
2759 |
sz := self size. |
|
2760 |
^ self copyFrom:(sz - count + 1) to:sz |
|
2761 |
||
2762 |
" |
|
2763 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
2764 |
'1234567890' copyLast:4 |
|
2765 |
" |
|
2766 |
! |
|
2767 |
||
3653 | 2768 |
copyReplaceAll:oldElement with:newElement |
2769 |
"return a copy of the receiver, where all elements equal to oldElement |
|
2770 |
have been replaced by newElement." |
|
2771 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2772 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 2773 |
^ self copy replaceAll:oldElement with:newElement |
2774 |
||
2775 |
" |
|
8874 | 2776 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
2777 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 2778 |
" |
2779 |
||
2780 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
2781 |
! |
|
2782 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2783 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2784 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2785 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2786 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2787 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2788 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2789 |
|
5489 | 2790 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2791 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2792 |
"/ 'Warning: #copyReplaceAll:withAll: will change semantics as defined in ANSI soon' errorPrintCR. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2793 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2794 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2795 |
self do:[:el | |
8874 | 2796 |
el = oldObject ifTrue:[ |
2797 |
aCollection do:[:el2 | |
|
2798 |
s nextPut:el2 |
|
2799 |
] |
|
2800 |
] ifFalse:[ |
|
2801 |
s nextPut:el |
|
2802 |
] |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2803 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2804 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2805 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2806 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2807 |
args: oldObject : <object> |
8874 | 2808 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2809 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2810 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2811 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2812 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2813 |
" |
8874 | 2814 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
2815 |
#(1 2 3 4 1 2 3 4) copyReplaceAll:1 withAll:'one' |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2816 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2817 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2818 |
|
602 | 2819 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
2820 |
"return a copy of the receiver, where the elements from startIndex to |
|
2821 |
endIndex have been replaced by the elements of aCollection" |
|
2822 |
||
2823 |
|newColl sz replSize| |
|
2824 |
||
2825 |
replSize := aCollection size. |
|
2826 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
2827 |
newColl := self copyEmptyAndGrow:sz. |
|
2828 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
2829 |
newColl replaceFrom:startIndex with:aCollection. |
|
2830 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
2831 |
^ newColl |
|
2832 |
||
2833 |
" |
|
2834 |
#(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) |
|
8874 | 2835 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 2836 |
" |
2837 |
! |
|
2838 |
||
2839 |
copyReplaceFrom:startIndex with:aCollection |
|
2840 |
"return a copy of the receiver, where the elements from startIndex to |
|
2841 |
the end have been replaced by the elements of aCollection" |
|
2842 |
||
2843 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
2844 |
||
2845 |
" |
|
8874 | 2846 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
2847 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 2848 |
" |
2849 |
! |
|
2850 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2851 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2852 |
"return a copy of the receiver, where all elements equal to oldElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2853 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2854 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2855 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2856 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2857 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2858 |
" |
8874 | 2859 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
2860 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2861 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2862 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2863 |
|
602 | 2864 |
copyThrough:element |
2865 |
"return a new collection consisting of the receiver elements |
|
8874 | 2866 |
up-to (AND including) the first occurrence of element." |
602 | 2867 |
|
2868 |
|idx| |
|
2869 |
||
2870 |
idx := self indexOf:element. |
|
2871 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
2872 |
^ self copyFrom:1 to:idx |
|
2873 |
||
2874 |
" |
|
2875 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
2876 |
'1234567890' copyThrough:$5 |
|
2877 |
'1234567890' copyThrough:$a |
|
2878 |
'1234567890' copyThrough:$1 |
|
2879 |
" |
|
2880 |
! |
|
2881 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2882 |
copyTo:stopIndex |
9234 | 2883 |
"Return a new collection consisting of receivers elements from 1 up to (including) stopIndex. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2884 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2885 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2886 |
^ self copyFrom:1 to:stopIndex |
602 | 2887 |
|
2888 |
" |
|
2889 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
2890 |
'1234567890' copyTo:4 |
|
2891 |
" |
|
2892 |
! |
|
2893 |
||
5526 | 2894 |
copyToMax:stop |
2895 |
"return a new collection consisting of receivers elements |
|
8874 | 2896 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 2897 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
2898 |
||
2899 |
^ self copyFrom:1 to:(self size min:stop) |
|
2900 |
||
2901 |
" |
|
2902 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
2903 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
2904 |
" |
|
2905 |
! |
|
2906 |
||
602 | 2907 |
copyUpTo:element |
2908 |
"return a new collection consisting of the receiver elements |
|
8874 | 2909 |
up-to (but excluding) the first occurrence of element; |
602 | 2910 |
or to the end, if element is not included" |
2911 |
||
2912 |
|idx| |
|
2913 |
||
2914 |
idx := self indexOf:element. |
|
2915 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
2916 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
2917 |
^ self copyFrom:1 to:(idx-1) |
|
2918 |
||
2919 |
" |
|
2920 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
2921 |
'1234567890' copyUpTo:$5 |
|
2922 |
'1234567890' copyUpTo:$a |
|
2923 |
'1234567890' copyUpTo:$1 |
|
2924 |
'123456789' copyUpTo:$0 |
|
2925 |
" |
|
2926 |
! |
|
2927 |
||
1 | 2928 |
copyWith:newElement |
233 | 2929 |
"return a new collection containing the receivers elements |
8874 | 2930 |
and the single new element, newElement. |
233 | 2931 |
This is different from concatentation, which expects another collection |
2932 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 2933 |
|
2934 |
|newCollection mySize newSize| |
|
2935 |
||
2936 |
mySize := self size. |
|
2937 |
newSize := mySize + 1. |
|
252 | 2938 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 2939 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
2940 |
newCollection at:newSize put:newElement. |
|
2941 |
^newCollection |
|
44 | 2942 |
|
95 | 2943 |
" |
2944 |
#(1 2 3 4 5) copyWith:$a |
|
2945 |
'abcdefg' copyWith:$h |
|
2946 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
2947 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
2948 |
" |
|
1 | 2949 |
! |
2950 |
||
61 | 2951 |
copyWithout:elementToSkip |
2952 |
"return a new collection consisting of a copy of the receiver, with |
|
2953 |
ALL elements equal to elementToSkip are left out. |
|
2954 |
No error is reported, if elementToSkip is not in the collection." |
|
2955 |
||
293 | 2956 |
|n "{ Class: SmallInteger }" |
2957 |
sz "{ Class: SmallInteger }" |
|
2958 |
srcIndex "{ Class: SmallInteger }" |
|
2959 |
dstIndex "{ Class: SmallInteger }" |
|
2960 |
skipIndex "{ Class: SmallInteger }" |
|
2961 |
copy l| |
|
61 | 2962 |
|
8874 | 2963 |
"the code below may look like overkill, |
61 | 2964 |
however, for big collections its better to move data |
2965 |
around in big chunks" |
|
2966 |
||
2967 |
n := self occurrencesOf:elementToSkip. |
|
2968 |
n == 0 ifTrue:[^ self copy]. |
|
2969 |
||
2970 |
sz := self size. |
|
252 | 2971 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 2972 |
|
2973 |
srcIndex := 1. |
|
2974 |
dstIndex := 1. |
|
2975 |
||
2976 |
n timesRepeat:[ |
|
8874 | 2977 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
2978 |
l := skipIndex - srcIndex. |
|
2979 |
l ~~ 0 ifTrue:[ |
|
2980 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
2981 |
with:self startingAt:srcIndex. |
|
2982 |
dstIndex := dstIndex + l |
|
2983 |
]. |
|
2984 |
srcIndex := skipIndex + 1 |
|
61 | 2985 |
]. |
2986 |
l := sz - srcIndex. |
|
2987 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 2988 |
with:self startingAt:srcIndex. |
61 | 2989 |
^ copy |
2990 |
||
95 | 2991 |
" |
2992 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
2993 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
2994 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 2995 |
'abcdefghi' copyWithout:$h |
2996 |
'abcdefg' copyWithout:$h |
|
95 | 2997 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
2998 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
2999 |
#(90 80 70 60 50) copyWithout:70 |
|
3000 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3001 |
" |
|
61 | 3002 |
! |
3003 |
||
602 | 3004 |
copyWithoutFirst:elementToSkip |
3005 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3006 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3007 |
No error is reported, if elementToSkip is not in the collection. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3008 |
Do not confuse this with copyButFirst:" |
602 | 3009 |
|
3010 |
|copy skipIndex sz| |
|
3011 |
||
3012 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3013 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3014 |
||
3015 |
sz := self size - 1. |
|
3016 |
copy := self copyEmptyAndGrow:sz. |
|
3017 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3018 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3019 |
^ copy |
|
3020 |
||
3021 |
" |
|
3022 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3023 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3024 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3025 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3026 |
" |
|
3027 |
! |
|
3028 |
||
8673 | 3029 |
copyWithoutIdentical:elementToSkip |
3030 |
"return a new collection consisting of a copy of the receiver, with |
|
3031 |
ALL elements identical to elementToSkip are left out. |
|
3032 |
No error is reported, if elementToSkip is not in the collection." |
|
3033 |
||
3034 |
|n "{ Class: SmallInteger }" |
|
3035 |
sz "{ Class: SmallInteger }" |
|
3036 |
srcIndex "{ Class: SmallInteger }" |
|
3037 |
dstIndex "{ Class: SmallInteger }" |
|
3038 |
skipIndex "{ Class: SmallInteger }" |
|
3039 |
copy l| |
|
3040 |
||
8874 | 3041 |
"the code below may look like overkill, |
8673 | 3042 |
however, for big collections its better to move data |
3043 |
around in big chunks" |
|
3044 |
||
3045 |
n := self count:[:el | el == elementToSkip]. |
|
3046 |
n == 0 ifTrue:[^ self copy]. |
|
3047 |
||
3048 |
sz := self size. |
|
3049 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3050 |
||
3051 |
srcIndex := 1. |
|
3052 |
dstIndex := 1. |
|
3053 |
||
3054 |
n timesRepeat:[ |
|
8874 | 3055 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3056 |
l := skipIndex - srcIndex. |
|
3057 |
l ~~ 0 ifTrue:[ |
|
3058 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3059 |
with:self startingAt:srcIndex. |
|
3060 |
dstIndex := dstIndex + l |
|
3061 |
]. |
|
3062 |
srcIndex := skipIndex + 1 |
|
8673 | 3063 |
]. |
3064 |
l := sz - srcIndex. |
|
3065 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3066 |
with:self startingAt:srcIndex. |
8673 | 3067 |
^ copy |
3068 |
||
3069 |
" |
|
8874 | 3070 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3071 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3072 |
" |
3073 |
! |
|
3074 |
||
61 | 3075 |
copyWithoutIndex:omitIndex |
3076 |
"return a new collection consisting of receivers elements |
|
3077 |
without the argument stored at omitIndex" |
|
3078 |
||
3079 |
|copy sz| |
|
3080 |
||
202 | 3081 |
sz := self size - 1. |
252 | 3082 |
copy := self copyEmptyAndGrow:sz. |
293 | 3083 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3084 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3085 |
^ copy |
3086 |
||
95 | 3087 |
" |
3088 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3089 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3090 |
" |
|
1 | 3091 |
! |
3092 |
||
202 | 3093 |
copyWithoutLast:count |
3094 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3095 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3096 |
That is the same as copyButLast: and badly named (for compatibility), |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3097 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3098 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3099 |
|
9250 | 3100 |
<resource:#obsolete> |
3101 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3102 |
^ self copyTo:(self size - count) |
202 | 3103 |
|
3104 |
" |
|
8874 | 3105 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3106 |
'1234567890' copyWithoutLast:4 |
|
202 | 3107 |
" |
5231 | 3108 |
! |
3109 |
||
3110 |
restAfter:anElement |
|
8366 | 3111 |
"return a new collection consisting of the receivers elements after |
5231 | 3112 |
(but excluding) anElement. |
8366 | 3113 |
If anElement is not in the receiver, the returned collection |
3114 |
will be empty. |
|
3115 |
See also #upTo:." |
|
5231 | 3116 |
|
3117 |
|pos| |
|
3118 |
||
3119 |
pos := self indexOf:anElement. |
|
8366 | 3120 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3121 |
||
5231 | 3122 |
^ self copyFrom:(pos + 1) |
3123 |
||
3124 |
" |
|
8874 | 3125 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3126 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3127 |
'hello world' upTo:Character space |
|
3128 |
'hello world' restAfter:Character space |
|
3129 |
'1234.5678' upTo:$. |
|
3130 |
'1234.5678' restAfter:$. |
|
8366 | 3131 |
" |
5231 | 3132 |
! |
3133 |
||
3134 |
upTo:anElement |
|
3135 |
"return a new collection consisting of the receivers elements upTo |
|
3136 |
(but excluding) anElement. |
|
3137 |
If anElement is not in the receiver, the returned collection |
|
3138 |
will consist of all elements of the receiver. |
|
9307 | 3139 |
See also #restAfter: , #copyFrom:index." |
5231 | 3140 |
|
3141 |
|pos| |
|
3142 |
||
3143 |
pos := self indexOf:anElement. |
|
3144 |
pos == 0 ifTrue:[^ self copy]. |
|
3145 |
||
3146 |
^ self copyFrom:1 to:(pos - 1) |
|
3147 |
||
3148 |
" |
|
8874 | 3149 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3150 |
'hello world' upTo:Character space |
|
3151 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3152 |
'1234.5678' upTo:$. |
|
3153 |
'1234' upTo:$. |
|
5231 | 3154 |
|
3155 |
raises an error: |
|
3156 |
||
8874 | 3157 |
(Dictionary new |
9307 | 3158 |
at:#foo put:'foo'; |
3159 |
at:#bar put:'bar'; |
|
3160 |
at:#baz put:'baz'; |
|
3161 |
yourself) upTo:#bar |
|
8874 | 3162 |
|
5231 | 3163 |
" |
3164 |
||
3165 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3166 |
! |
|
3167 |
||
3168 |
upTo:anElement count:count |
|
3169 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3170 |
(but excluding) count found anElements, i.e. the first found count-1 |
3171 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3172 |
procedure is done from the end of the collection backwards. |
3173 |
If anElement is not in the receiver, the returned collection |
|
3174 |
will consist of all elements of the receiver" |
|
3175 |
||
3176 |
|startPos endPos pos found| |
|
3177 |
||
3178 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3179 |
^ self upTo:anElement |
5231 | 3180 |
]. |
3181 |
||
3182 |
startPos := 1. |
|
3183 |
endPos := self size. |
|
3184 |
found := 0. |
|
3185 |
||
3186 |
count > 1 ifTrue:[ |
|
8874 | 3187 |
pos := 0. |
3188 |
[pos < self size and: [found < count]] |
|
3189 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3190 |
found == count ifTrue:[ |
|
3191 |
endPos := pos - 1 |
|
3192 |
] |
|
3193 |
]. |
|
5231 | 3194 |
|
3195 |
count < 0 ifTrue:[ |
|
8874 | 3196 |
pos := self size + 1. |
3197 |
[pos > 1 and: [found < count abs]] |
|
3198 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3199 |
found == count abs ifTrue:[ |
|
3200 |
startPos := pos + 1 |
|
3201 |
] |
|
3202 |
]. |
|
5231 | 3203 |
|
3204 |
^ self copyFrom: startPos to: endPos |
|
3205 |
||
3206 |
" |
|
8874 | 3207 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3208 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3209 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3210 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3211 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3212 |
" |
|
5563 | 3213 |
! |
3214 |
||
3215 |
upToAny:aCollectionOfObjects |
|
3216 |
"return a new collection consisting of the receivers elements upTo |
|
3217 |
(but excluding) any in aCollectionOfObjects. |
|
3218 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3219 |
will consist of all elements of the receiver. |
|
3220 |
See also #upTo:" |
|
3221 |
||
3222 |
|pos| |
|
3223 |
||
3224 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3225 |
pos == 0 ifTrue:[^ self copy]. |
|
3226 |
||
3227 |
^ self copyFrom:1 to:(pos - 1) |
|
3228 |
||
3229 |
" |
|
8874 | 3230 |
'hello world' upToAny:#($/ $:) |
3231 |
'hello:world' upToAny:#($/ $:) |
|
3232 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3233 |
" |
602 | 3234 |
! ! |
3235 |
||
3236 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3237 |
||
3238 |
collect:aBlock |
|
3239 |
"evaluate the argument, aBlock for every element in the collection |
|
3240 |
and return a collection of the results" |
|
3241 |
||
3242 |
|newCollection |
|
3243 |
sz "{ Class:SmallInteger }"| |
|
3244 |
||
3245 |
sz := self size. |
|
3246 |
newCollection := self copyEmptyAndGrow:sz. |
|
3247 |
1 to:sz do:[:index | |
|
8874 | 3248 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3249 |
]. |
3250 |
^ newCollection |
|
3251 |
||
3252 |
" |
|
8874 | 3253 |
#(one two three four five six) collect:[:element | element asUppercase] |
3254 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3255 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3256 |
" |
202 | 3257 |
! |
3258 |
||
602 | 3259 |
do:aBlock |
3260 |
"evaluate the argument, aBlock for every element in the collection." |
|
3261 |
||
3262 |
|stop "{ Class:SmallInteger }"| |
|
3263 |
||
3264 |
stop := self size. |
|
3265 |
1 to:stop do:[:index | |
|
8874 | 3266 |
aBlock value:(self at:index). |
602 | 3267 |
] |
2927 | 3268 |
|
602 | 3269 |
" |
1422 | 3270 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 3271 |
" |
3272 |
! |
|
3273 |
||
2927 | 3274 |
do:aBlock separatedBy:sepBlock |
3275 |
"evaluate the argument, aBlock for every element in the collection. |
|
3276 |
Between each element (i.e. not before the first element and not after the |
|
3277 |
last element), evaluate sepBlock. |
|
3278 |
This supports printing of collections with elements separated by some |
|
3279 |
string." |
|
3280 |
||
3281 |
|stop "{ Class:SmallInteger }"| |
|
3282 |
||
3283 |
stop := self size. |
|
3284 |
stop < 1 ifTrue:[^ self]. |
|
3285 |
aBlock value:(self at:1). |
|
3286 |
2 to:stop do:[:index | |
|
8874 | 3287 |
sepBlock value. |
3288 |
aBlock value:(self at:index) |
|
2927 | 3289 |
]. |
3290 |
||
3291 |
" |
|
3292 |
#(one two three four five six) |
|
8874 | 3293 |
do:[:each | Transcript show:each] |
3294 |
separatedBy:[Transcript show:' , '] |
|
2927 | 3295 |
" |
3296 |
! |
|
3003 | 3297 |
|
3044 | 3298 |
doWithIndex:aBlock |
3299 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
3300 |
the index as second argument." |
|
3301 |
||
3302 |
self keysAndValuesDo:[:index :value | |
|
8874 | 3303 |
aBlock value:value value:index |
3044 | 3304 |
]. |
3305 |
||
3306 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
3307 |
! |
|
3308 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3309 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3310 |
"evaluate the argument, aBlock for the elements starting at start |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3311 |
to the end and return a collection of the results" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3312 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3313 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3314 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3315 |
" |
8874 | 3316 |
#(one two three four five six) |
3317 |
from:2 |
|
3318 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3319 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3320 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3321 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3322 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3323 |
|
602 | 3324 |
from:startIndex do:aBlock |
3325 |
"evaluate the argument, aBlock for the elements starting with the |
|
3326 |
element at startIndex to the end." |
|
3327 |
||
3328 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 3329 |
|
95 | 3330 |
" |
8874 | 3331 |
#(one two three four five six) |
3332 |
from:3 |
|
3333 |
do:[:element | Transcript showCR:element] |
|
602 | 3334 |
" |
3335 |
! |
|
3336 |
||
7490 | 3337 |
from:startIndex keysAndValuesDo:aBlock |
3338 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
3339 |
element at startIndex to the end." |
|
3340 |
||
3341 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
3342 |
||
3343 |
" |
|
8874 | 3344 |
#(one two three four five six) |
3345 |
from:3 |
|
3346 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 3347 |
" |
3348 |
! |
|
3349 |
||
7337 | 3350 |
from:index1 to:index2 by:stepArg do:aBlock |
3351 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
3352 |
||
3353 |
|start "{ Class:SmallInteger }" |
|
3354 |
stop "{ Class:SmallInteger }" |
|
3355 |
step "{ Class:SmallInteger }" | |
|
3356 |
||
3357 |
start := index1. "/ these assignments force type checking... |
|
3358 |
stop := index2. "/ and guarantee inline loop code below. |
|
3359 |
step := stepArg. |
|
3360 |
||
3361 |
start to:stop by:step do:[:index | |
|
8874 | 3362 |
aBlock value:(self at:index). |
7337 | 3363 |
] |
3364 |
||
3365 |
" |
|
8874 | 3366 |
#(one two three four five six seven eight nine ten) |
3367 |
from:2 |
|
3368 |
to:10 |
|
3369 |
by:2 |
|
3370 |
do:[:element | Transcript showCR:element] |
|
7337 | 3371 |
" |
3372 |
! |
|
3373 |
||
602 | 3374 |
from:start to:stop collect:aBlock |
3375 |
"evaluate the argument, aBlock for the elements indexed by start |
|
3376 |
to stop in the collection and return a collection of the results" |
|
3377 |
||
3378 |
|newCollection sz |
|
3379 |
idx "{ Class:SmallInteger }"| |
|
3380 |
||
3381 |
sz := stop - start + 1. |
|
3382 |
newCollection := self copyEmptyAndGrow:sz. |
|
3383 |
idx := 1. |
|
3384 |
start to:stop do:[:index | |
|
8874 | 3385 |
newCollection at:idx put:(aBlock value:(self at:index)). |
3386 |
idx := idx + 1 |
|
602 | 3387 |
]. |
3388 |
^ newCollection |
|
3389 |
||
3390 |
" |
|
8874 | 3391 |
#(one two three four five six) |
3392 |
from:2 |
|
3393 |
to:4 |
|
3394 |
collect:[:element | element asUppercase] |
|
602 | 3395 |
" |
3396 |
! |
|
3397 |
||
3398 |
from:index1 to:index2 do:aBlock |
|
3399 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3400 |
index2 in the collection" |
|
3401 |
||
3402 |
|start "{ Class:SmallInteger }" |
|
3403 |
stop "{ Class:SmallInteger }" | |
|
3404 |
||
7337 | 3405 |
start := index1. "/ these assignments force type checking... |
3406 |
stop := index2. "/ and guarantee inline loop code below. |
|
3407 |
||
602 | 3408 |
start to:stop do:[:index | |
8874 | 3409 |
aBlock value:(self at:index). |
602 | 3410 |
] |
3411 |
||
3412 |
" |
|
8874 | 3413 |
#(one two three four five six) |
3414 |
from:3 |
|
3415 |
to:5 |
|
3416 |
do:[:element | Transcript showCR:element] |
|
602 | 3417 |
" |
3418 |
! |
|
3419 |
||
6868 | 3420 |
from:index1 to:index2 keysAndValuesDo:aBlock |
3421 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3422 |
index2 in the collection; pass both index and value." |
|
3423 |
||
3424 |
|start "{ Class:SmallInteger }" |
|
3425 |
stop "{ Class:SmallInteger }" | |
|
3426 |
||
7337 | 3427 |
start := index1. "/ these assignments force type checking... |
3428 |
stop := index2. "/ and guarantee inline loop code below. |
|
3429 |
||
6868 | 3430 |
start to:stop do:[:index | |
8874 | 3431 |
aBlock value:index value:(self at:index). |
6868 | 3432 |
] |
3433 |
||
3434 |
" |
|
8874 | 3435 |
#(one two three four five six) |
3436 |
from:3 |
|
3437 |
to:5 |
|
3438 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 3439 |
" |
3440 |
! |
|
3441 |
||
602 | 3442 |
from:index1 to:index2 reverseDo:aBlock |
3443 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3444 |
index2 in the collection. Step in reverse order" |
|
3445 |
||
3446 |
|start "{ Class:SmallInteger }" |
|
3447 |
stop "{ Class:SmallInteger }" | |
|
3448 |
||
7337 | 3449 |
start := index1. "/ these assignments force type checking... |
3450 |
stop := index2. "/ and guarantee inline loop code below. |
|
3451 |
||
602 | 3452 |
stop to:start by:-1 do:[:index | |
8874 | 3453 |
aBlock value:(self at:index). |
602 | 3454 |
] |
3455 |
||
3456 |
" |
|
8874 | 3457 |
#(one two three four five six) |
3458 |
from:3 |
|
3459 |
to:5 |
|
3460 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 3461 |
" |
61 | 3462 |
! |
3463 |
||
6027 | 3464 |
from:startIndex to:stopIndex select:aBlock |
3465 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
3466 |
and return a collection of those elements for which the block return true." |
|
3467 |
||
3468 |
|element newColl species needCopy |
|
3469 |
n "{ Class:SmallInteger }"| |
|
3470 |
||
3471 |
n := stopIndex - startIndex + 1. |
|
3472 |
species := self species. |
|
3473 |
species growIsCheap ifFalse:[ |
|
8874 | 3474 |
newColl := OrderedCollection new:n. |
3475 |
needCopy := true |
|
6027 | 3476 |
] ifTrue:[ |
8874 | 3477 |
newColl := self copyEmpty:n. |
3478 |
needCopy := false |
|
6027 | 3479 |
]. |
3480 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3481 |
element := self at:index. |
3482 |
(aBlock value:element) ifTrue:[ |
|
3483 |
newColl add:element |
|
3484 |
]. |
|
6027 | 3485 |
]. |
3486 |
needCopy ifTrue:[ |
|
8874 | 3487 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 3488 |
]. |
3489 |
^ newColl |
|
3490 |
||
3491 |
" |
|
8874 | 3492 |
#(faba one two three four five six) |
3493 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 3494 |
" |
3495 |
! |
|
3496 |
||
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3497 |
inGroupsOf:n collect:aBlock |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3498 |
"evaluate the argument, aBlock for every group of n elements in the collection, |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3499 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3500 |
The block is called with n arguments for group of n consecutive elements in the receiver. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3501 |
An error will be reported, if the number of elements in the receiver |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3502 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3503 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3504 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3505 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3506 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3507 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3508 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3509 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3510 |
n == 2 ifTrue:[ |
8874 | 3511 |
1 to:stop by:n do:[:index | |
3512 |
rslt := aBlock value:(self at:index) value:(self at:index+1). |
|
3513 |
newCollection at:dstIdx put:rslt. |
|
3514 |
dstIdx := dstIdx + 1. |
|
3515 |
]. |
|
3516 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3517 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3518 |
n == 3 ifTrue:[ |
8874 | 3519 |
1 to:stop by:n do:[:index | |
3520 |
rslt := aBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3521 |
newCollection at:dstIdx put:rslt. |
|
3522 |
dstIdx := dstIdx + 1. |
|
3523 |
]. |
|
3524 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3525 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3526 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3527 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3528 |
1 to:stop by:n do:[:index | |
8874 | 3529 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3530 |
rslt := aBlock valueWithArguments:argVector. |
|
3531 |
newCollection at:dstIdx put:rslt. |
|
3532 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3533 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3534 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3535 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3536 |
"for groups of 2, this is the same as: |
8874 | 3537 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3538 |
pairWiseCollect:[:num :sym | num->sym] |
|
3539 |
||
3540 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
3541 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3542 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3543 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
8874 | 3544 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3545 |
" |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3546 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3547 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3548 |
inGroupsOf:n do:aBlock |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3549 |
"evaluate the argument, aBlock for every group of n elements in the collection. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3550 |
The block is called with n arguments for group of n consecutive elements in the receiver. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3551 |
An error will be reported, if the number of elements in the receiver |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3552 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3553 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3554 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3555 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3556 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3557 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3558 |
n == 2 ifTrue:[ |
8874 | 3559 |
1 to:stop by:n do:[:index | |
3560 |
aBlock value:(self at:index) value:(self at:index+1). |
|
3561 |
]. |
|
3562 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3563 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3564 |
n == 3 ifTrue:[ |
8874 | 3565 |
1 to:stop by:n do:[:index | |
3566 |
aBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3567 |
]. |
|
3568 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3569 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3570 |
n == 4 ifTrue:[ |
8874 | 3571 |
1 to:stop by:n do:[:index | |
3572 |
aBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
3573 |
]. |
|
3574 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3575 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3576 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3577 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3578 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3579 |
1 to:stop by:n do:[:index | |
8874 | 3580 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3581 |
aBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3582 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3583 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3584 |
"for groups of 2, this is the same as: |
8874 | 3585 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3586 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
|
3587 |
||
3588 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
3589 |
inGroupsOf:2 do:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3590 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3591 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
8874 | 3592 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3593 |
" |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3594 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3595 |
|
8853 | 3596 |
keysAndValuesCollect:aTwoArgBlock |
3597 |
"evaluate the argument, aBlock for every element in the collection, |
|
3598 |
passing both index and element as arguments. |
|
3599 |
Collect the returned values and return them." |
|
3600 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3601 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3602 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3603 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3604 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3605 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3606 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 3607 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3608 |
|
8853 | 3609 |
^ newCollection |
3610 |
||
3611 |
" |
|
8874 | 3612 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3613 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3614 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 3615 |
|
3616 |
||
3617 |
|d| |
|
3618 |
||
3619 |
d := Dictionary new. |
|
3620 |
d at:'a' put:'A'. |
|
3621 |
d at:'b' put:'B'. |
|
3622 |
d at:'c' put:'C'. |
|
3623 |
d at:'d' put:'D'. |
|
3624 |
d at:'e' put:'E'. |
|
8874 | 3625 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3626 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3627 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3628 |
ifFalse:[element asLowercase]] |
8853 | 3629 |
" |
3630 |
! |
|
3631 |
||
602 | 3632 |
keysAndValuesDo:aTwoArgBlock |
3633 |
"evaluate the argument, aBlock for every element in the collection, |
|
3634 |
passing both index and element as arguments." |
|
3635 |
||
3636 |
|stop "{ Class:SmallInteger }"| |
|
3637 |
||
3638 |
stop := self size. |
|
3639 |
1 to:stop do:[:index | |
|
8874 | 3640 |
aTwoArgBlock value:index value:(self at:index). |
602 | 3641 |
] |
3642 |
" |
|
8874 | 3643 |
#(one two three four five six) |
3644 |
keysAndValuesDo:[:key :element | |
|
3645 |
Transcript show:key; space; showCR:element |
|
3646 |
] |
|
602 | 3647 |
" |
3648 |
! |
|
3649 |
||
1359 | 3650 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 3651 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 3652 |
in the collection, passing both index and element as arguments." |
3653 |
||
3654 |
|stop "{ Class:SmallInteger }"| |
|
3655 |
||
3656 |
stop := self size. |
|
3657 |
stop to:1 by:-1 do:[:index | |
|
8874 | 3658 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 3659 |
] |
3660 |
||
3661 |
" |
|
8874 | 3662 |
#(one two three four five six) |
3663 |
keysAndValuesReverseDo:[:key :element | |
|
3664 |
Transcript show:key; space; showCR:element |
|
3665 |
] |
|
1359 | 3666 |
" |
3667 |
||
3668 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
3669 |
! |
|
3670 |
||
602 | 3671 |
nonNilElementsDo:aBlock |
3672 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
3673 |
||
3674 |
|stop "{ Class:SmallInteger }" |
|
3675 |
element| |
|
3676 |
||
3677 |
stop := self size. |
|
3678 |
1 to:stop do:[:index | |
|
8874 | 3679 |
(element := self at:index) notNil ifTrue:[ |
3680 |
aBlock value:element. |
|
3681 |
] |
|
602 | 3682 |
] |
3683 |
" |
|
1422 | 3684 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 3685 |
" |
3686 |
! |
|
3687 |
||
3688 |
pairWiseCollect:aBlock |
|
3689 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
3690 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
3691 |
An error will be reported, if the number of elements in the receiver |
|
3692 |
is not a multiple of 2. |
|
3693 |
Collect the results and return a new collection containing those." |
|
3694 |
||
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3695 |
^ self inGroupsOf:2 collect:aBlock |
44 | 3696 |
|
95 | 3697 |
" |
8874 | 3698 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3699 |
pairWiseCollect:[:num :sym | sym->num] |
|
3700 |
||
3701 |
||
3702 |
#(1 1 1 2 1 3 1 4 1 5) |
|
3703 |
pairWiseCollect:[:x :y | x@y] |
|
602 | 3704 |
" |
3705 |
! |
|
3706 |
||
3707 |
pairWiseDo:aBlock |
|
3708 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
3709 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
3710 |
An error will be reported, if the number of elements in the receiver |
|
3711 |
is not a multiple of 2." |
|
3712 |
||
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3713 |
^ self inGroupsOf:2 do:aBlock |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3714 |
|
602 | 3715 |
" |
8874 | 3716 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3717 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
|
3718 |
||
3719 |
||
3720 |
#(1 1 1 2 1 3 1 4 1 5) |
|
3721 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
|
95 | 3722 |
" |
61 | 3723 |
! |
3724 |
||
602 | 3725 |
reverseDo:aBlock |
3726 |
"evaluate the argument, aBlock for every element in the collection |
|
3727 |
in reverse order" |
|
3728 |
||
3729 |
|sz "{ Class:SmallInteger }"| |
|
3730 |
||
3731 |
sz := self size. |
|
3732 |
sz to:1 by:-1 do:[:index | |
|
8874 | 3733 |
aBlock value:(self at:index). |
602 | 3734 |
] |
3735 |
||
3736 |
" |
|
1422 | 3737 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 3738 |
" |
3739 |
! |
|
3740 |
||
3741 |
select:aBlock |
|
3742 |
"evaluate the argument, aBlock for every element in the collection |
|
3743 |
and return a collection of all elements for which the block return |
|
3744 |
true" |
|
3745 |
||
3746 |
|element newColl species needCopy |
|
3747 |
sz "{ Class:SmallInteger }"| |
|
3748 |
||
3749 |
sz := self size. |
|
3750 |
species := self species. |
|
3751 |
species growIsCheap ifFalse:[ |
|
8874 | 3752 |
newColl := OrderedCollection new:sz. |
3753 |
needCopy := true |
|
602 | 3754 |
] ifTrue:[ |
8874 | 3755 |
newColl := self copyEmpty:sz. |
3756 |
needCopy := false |
|
602 | 3757 |
]. |
3758 |
1 to:sz do:[:index | |
|
8874 | 3759 |
element := self at:index. |
3760 |
(aBlock value:element) ifTrue:[ |
|
3761 |
newColl add:element |
|
3762 |
]. |
|
602 | 3763 |
]. |
3764 |
needCopy ifTrue:[ |
|
8874 | 3765 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 3766 |
]. |
61 | 3767 |
^ newColl |
3768 |
||
95 | 3769 |
" |
8874 | 3770 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
3771 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
3772 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
3773 |
(1 to:9) select:[:element | element odd] |
|
3774 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 3775 |
" |
186 | 3776 |
! |
3777 |
||
602 | 3778 |
with:aSequenceableCollection do:aTwoArgBlock |
3779 |
"evaluate the argument, aBlock for successive elements from |
|
3780 |
each the receiver and the argument, aSequenceableCollection. |
|
3781 |
The second argument, aBlock must be a two-argument block. |
|
3782 |
The collection argument must implement access via a numeric key." |
|
3783 |
||
3784 |
|stop "{ Class: SmallInteger }" | |
|
3785 |
||
3786 |
stop := self size. |
|
3787 |
1 to:stop do:[:index | |
|
8874 | 3788 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 3789 |
] |
186 | 3790 |
" |
8874 | 3791 |
#(one two three four five six) |
3792 |
with:(1 to:10) |
|
3793 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 3794 |
" |
1 | 3795 |
! ! |
3796 |
||
4154 | 3797 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 3798 |
|
602 | 3799 |
atAllPut:anObject |
3800 |
"replace all elements of the collection by the argument, anObject. |
|
3801 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3802 |
therefore the change may affect all others referencing the receiver." |
|
3803 |
||
3804 |
self from:1 to:(self size) put:anObject |
|
3805 |
||
3806 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3807 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3808 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3809 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3810 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3811 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3812 |
" |
602 | 3813 |
(Array new:10) atAllPut:1 |
3814 |
(String new:10) atAllPut:$a |
|
3815 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3816 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3817 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 3818 |
! |
3819 |
||
1 | 3820 |
from:index1 to:index2 put:anObject |
3821 |
"replace the elements from index1 to index2 of the collection |
|
44 | 3822 |
by the argument, anObject. |
3823 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3824 |
therefore the change may affect all others referencing the receiver." |
|
1 | 3825 |
|
3826 |
|index "{ Class: SmallInteger }" |
|
3827 |
end "{ Class: SmallInteger }"| |
|
3828 |
||
3829 |
index := index1. |
|
3830 |
end := index2. |
|
3831 |
[index <= end] whileTrue:[ |
|
8874 | 3832 |
self at:index put:anObject. |
3833 |
index := index + 1 |
|
1 | 3834 |
] |
44 | 3835 |
|
95 | 3836 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3837 |
args: index1 : <integer> |
8874 | 3838 |
index2 : <integer> |
3839 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3840 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3841 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3842 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3843 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3844 |
" |
95 | 3845 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
3846 |
'abcdefghijkl' from:3 to:6 put:$X |
|
3847 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3848 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3849 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 3850 |
! |
3851 |
||
3003 | 3852 |
replaceAll:oldObject by:newObject |
3853 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
3854 |
Pleace do no longer use this one; |
|
3855 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
3856 |
||
5872 | 3857 |
<resource:#obsolete> |
3858 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3859 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 3860 |
^ self replaceAll:oldObject with:newObject |
3861 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3862 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3863 |
args: oldObject : <object> |
8874 | 3864 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3865 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3866 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3867 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3868 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3869 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 3870 |
! |
3871 |
||
3872 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
3873 |
"backward ST/X compatibility: an alias for #replaceAll:with:from:to:. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3874 |
Please do no longer use this one; |
3003 | 3875 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
3876 |
||
5872 | 3877 |
<resource:#obsolete> |
3878 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3879 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 3880 |
^ self replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3881 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3882 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3883 |
args: oldObject : <object> |
8874 | 3884 |
newObject : <object> |
3885 |
startIndex : <integer> |
|
3886 |
stopIndex : <integer> |
|
3887 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3888 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3889 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3890 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3891 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 3892 |
! |
3893 |
||
2927 | 3894 |
replaceAll:oldObject with:newObject |
44 | 3895 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3896 |
|
44 | 3897 |
Notice: This operation modifies the receiver, NOT a copy; |
3898 |
therefore the change may affect all others referencing the receiver." |
|
1 | 3899 |
|
8874 | 3900 |
^ self |
3901 |
replaceAll:oldObject |
|
3902 |
with:newObject |
|
3903 |
from:1 |
|
3904 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3905 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3906 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3907 |
args: oldObject : <object> |
8874 | 3908 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3909 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3910 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3911 |
" |
362 | 3912 |
|
3913 |
" |
|
8874 | 3914 |
'123123abc123' replaceAll:$1 with:$* |
3915 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 3916 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3917 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3918 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 3919 |
! |
293 | 3920 |
|
2927 | 3921 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 3922 |
"replace all oldObjects found between startIndex and endIndex, |
3923 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3924 |
|
362 | 3925 |
Notice: This operation modifies the receiver, NOT a copy; |
3926 |
therefore the change may affect all others referencing the receiver." |
|
3927 |
||
3928 |
|start "{ Class: SmallInteger }" |
|
3929 |
stop "{ Class: SmallInteger }"| |
|
3930 |
||
3931 |
start := startIndex. |
|
3932 |
stop := stopIndex. |
|
3933 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3934 |
(self at:index) = oldObject ifTrue:[ |
3935 |
self at:index put:newObject |
|
3936 |
] |
|
1 | 3937 |
] |
44 | 3938 |
|
95 | 3939 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3940 |
args: oldObject : <object> |
8874 | 3941 |
newObject : <object> |
3942 |
startIndex : <integer> |
|
3943 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3944 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3945 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3946 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3947 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3948 |
" |
2980 | 3949 |
'123123abc123' replaceAll:$1 with:$* |
3950 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
3951 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 3952 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3953 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3954 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 3955 |
! |
3956 |
||
7615 | 3957 |
replaceAllForWhich:aConditionBlock with:newObject |
3958 |
"replace all elements for which aConditionBlock returns true |
|
3959 |
between startIndex and endIndex, by newObject in the receiver. |
|
3960 |
||
3961 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3962 |
therefore the change may affect all others referencing the receiver." |
|
3963 |
||
8874 | 3964 |
self |
3965 |
replaceAllForWhich:aConditionBlock with:newObject |
|
3966 |
from:1 to:(self size) |
|
7615 | 3967 |
|
3968 |
" |
|
3969 |
args: aConditionBlock : <block> |
|
8874 | 3970 |
newObject : <object> |
7615 | 3971 |
|
3972 |
returns: self |
|
3973 |
" |
|
3974 |
! |
|
3975 |
||
3976 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
3977 |
"replace all elements for which aConditionBlock returns true |
|
3978 |
between startIndex and endIndex, by newObject in the receiver. |
|
3979 |
||
3980 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3981 |
therefore the change may affect all others referencing the receiver." |
|
3982 |
||
3983 |
|start "{ Class: SmallInteger }" |
|
3984 |
stop "{ Class: SmallInteger }"| |
|
3985 |
||
3986 |
start := startIndex. |
|
3987 |
stop := stopIndex. |
|
3988 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3989 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
3990 |
self at:index put:newObject |
|
3991 |
] |
|
7615 | 3992 |
] |
3993 |
||
3994 |
" |
|
3995 |
args: aConditionBlock : <block> |
|
8874 | 3996 |
newObject : <object> |
3997 |
startIndex : <integer> |
|
3998 |
stopIndex : <integer> |
|
7615 | 3999 |
|
4000 |
returns: self |
|
4001 |
" |
|
4002 |
! |
|
4003 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4004 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4005 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4006 |
This is like #replaceAll:with:from:to:, but uses an identity compare. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4007 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4008 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4009 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4010 |
|
8874 | 4011 |
^ self |
4012 |
replaceAllIdentical:oldObject |
|
4013 |
with:newObject |
|
4014 |
from:1 |
|
4015 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4016 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4017 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4018 |
args: oldObject : <object> |
8874 | 4019 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4020 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4021 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4022 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4023 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4024 |
" |
8874 | 4025 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4026 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAllIdentical:1 with:'one' |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4027 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4028 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4029 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4030 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4031 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4032 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4033 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4034 |
This is like #replaceAll:with:from:to:, but uses an identity compare. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4035 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4036 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4037 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4038 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4039 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4040 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4041 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4042 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4043 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4044 |
startIndex to:stopIndex do:[:index | |
8874 | 4045 |
(self at:index) == oldObject ifTrue:[ |
4046 |
self at:index put:newObject |
|
4047 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4048 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4049 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4050 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4051 |
args: oldObject : <object> |
8874 | 4052 |
newObject : <object> |
4053 |
startIndex : <integer> |
|
4054 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4055 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4056 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4057 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4058 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4059 |
" |
8874 | 4060 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4061 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4062 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4063 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4064 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4065 |
|
3003 | 4066 |
replaceAny:aCollection by:newObject |
4067 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4068 |
Pleace do no longer use this one; |
3003 | 4069 |
instead use #replaceAny:with: for naming consistence." |
4070 |
||
5872 | 4071 |
<resource:#obsolete> |
4072 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4073 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4074 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4075 |
|
4076 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4077 |
args: aCollection : <colleciton of <object> > |
8874 | 4078 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4079 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4080 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4081 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4082 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4083 |
" |
3003 | 4084 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4085 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4086 |
" |
|
4087 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4088 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4089 |
! |
4090 |
||
3003 | 4091 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4092 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4093 |
Pleace do no longer use this one; |
3003 | 4094 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4095 |
|
5872 | 4096 |
<resource:#obsolete> |
4097 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4098 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4099 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4100 |
|
4101 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4102 |
args: aCollection : <colleciton of <object> > |
8874 | 4103 |
newObject : <object> |
4104 |
startIndex : <integer> |
|
4105 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4106 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4107 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4108 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4109 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4110 |
" |
8874 | 4111 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4112 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4113 |
" |
4114 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4115 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4116 |
! |
4117 |
||
4118 |
replaceAny:aCollection with:newObject |
|
4119 |
"replace all elements, which are in aCollection, by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4120 |
|
2927 | 4121 |
Notice: This operation modifies the receiver, NOT a copy; |
4122 |
therefore the change may affect all others referencing the receiver." |
|
4123 |
||
8874 | 4124 |
^ self |
4125 |
replaceAny:aCollection |
|
4126 |
with:newObject |
|
4127 |
from:1 |
|
4128 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4129 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4130 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4131 |
args: aCollection : <colleciton of <object> > |
8874 | 4132 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4133 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4134 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4135 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4136 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4137 |
"Modified: / 20.5.1998 / 15:25:58 / cg" |
602 | 4138 |
! |
4139 |
||
2927 | 4140 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4141 |
"replace all elements within a range, |
4142 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4143 |
|
362 | 4144 |
Notice: This operation modifies the receiver, NOT a copy; |
4145 |
therefore the change may affect all others referencing the receiver." |
|
4146 |
||
4147 |
|start "{ Class: SmallInteger }" |
|
4148 |
stop "{ Class: SmallInteger }"| |
|
4149 |
||
4150 |
start := startIndex. |
|
4151 |
stop := stopIndex. |
|
4152 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4153 |
(aCollection includes:(self at:index)) ifTrue:[ |
4154 |
self at:index put:newObject |
|
4155 |
] |
|
95 | 4156 |
] |
4157 |
||
4158 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4159 |
args: aCollection : <colleciton of <object> > |
8874 | 4160 |
newObject : <object> |
4161 |
startIndex : <integer> |
|
4162 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4163 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4164 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4165 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4166 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4167 |
" |
8874 | 4168 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4169 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4170 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4171 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4172 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4173 |
! |
4174 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4175 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4176 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4177 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4178 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4179 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4180 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4181 |
|
8874 | 4182 |
^ self |
4183 |
replaceFrom:startIndex |
|
4184 |
to:(startIndex + numberOfElements - 1) |
|
4185 |
with:replacementCollection |
|
4186 |
startingAt:1 |
|
4187 |
||
4188 |
" |
|
4189 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4190 |
#($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
|
4191 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4192 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4193 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4194 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4195 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4196 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4197 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4198 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4199 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4200 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4201 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4202 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4203 |
^ self |
8874 | 4204 |
replaceFrom:startIndex |
4205 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4206 |
with:replacementCollection |
|
4207 |
startingAt:repStartIndex |
|
4208 |
||
4209 |
" |
|
4210 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4211 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4212 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4213 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4214 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4215 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4216 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4217 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4218 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4219 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 4220 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4221 |
|
44 | 4222 |
Notice: This operation modifies the receiver, NOT a copy; |
4223 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4224 |
|
8874 | 4225 |
^ self |
4226 |
replaceFrom:startIndex |
|
4227 |
to:stopIndex |
|
4228 |
with:replacementCollection |
|
4229 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4230 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4231 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4232 |
args: startIndex : <integer> |
8874 | 4233 |
stopIndex : <integer> |
4234 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4235 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4236 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4237 |
" |
61 | 4238 |
|
95 | 4239 |
" |
4240 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
4241 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
4242 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4243 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4244 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 4245 |
! |
4246 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4247 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4248 |
"replace elements in the receiver between index startIndex and stopIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4249 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 4250 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4251 |
|
44 | 4252 |
Notice: This operation modifies the receiver, NOT a copy; |
4253 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4254 |
|
4255 |
|srcIndex "{ Class: SmallInteger }" |
|
4256 |
dstIndex "{ Class: SmallInteger }" |
|
4257 |
end "{ Class: SmallInteger }" | |
|
4258 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4259 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 4260 |
" must do reverse copy " |
4261 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
4262 |
dstIndex := stopIndex. |
|
4263 |
end := startIndex. |
|
4264 |
[dstIndex >= end] whileTrue:[ |
|
4265 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
4266 |
srcIndex := srcIndex - 1. |
|
4267 |
dstIndex := dstIndex - 1 |
|
4268 |
]. |
|
4269 |
^ self |
|
1 | 4270 |
]. |
4271 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4272 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4273 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4274 |
end := stopIndex. |
1 | 4275 |
[dstIndex <= end] whileTrue:[ |
8874 | 4276 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
4277 |
srcIndex := srcIndex + 1. |
|
4278 |
dstIndex := dstIndex + 1 |
|
1 | 4279 |
] |
61 | 4280 |
|
95 | 4281 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4282 |
args: startIndex : <integer> |
8874 | 4283 |
stopIndex : <integer> |
4284 |
replacementCollection : <colleciton of <object> > |
|
4285 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4286 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4287 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4288 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4289 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4290 |
" |
8874 | 4291 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
4292 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
4293 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 4294 |
|
4295 |
|c| |
|
4296 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4297 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 4298 |
|
4299 |
|c| |
|
4300 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4301 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 4302 |
" |
3234 | 4303 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4304 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 4305 |
! |
4306 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4307 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4308 |
"replace elements in the receiver starting at startIndex, |
602 | 4309 |
with elements taken from replacementCollection starting at 1 |
4310 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4311 |
|
602 | 4312 |
Notice: This operation modifies the receiver, NOT a copy; |
4313 |
therefore the change may affect all others referencing the receiver." |
|
4314 |
||
8874 | 4315 |
^ self |
4316 |
replaceFrom:startIndex |
|
4317 |
to:(startIndex + replacementCollection size - 1) |
|
4318 |
with:replacementCollection |
|
4319 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4320 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4321 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4322 |
args: startIndex : <integer> |
8874 | 4323 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4324 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4325 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4326 |
" |
602 | 4327 |
|
4328 |
" |
|
4329 |
'1234567890' replaceFrom:5 with:'abc' |
|
4330 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
4331 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4332 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4333 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 4334 |
! |
4335 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4336 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4337 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4338 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 4339 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4340 |
|
602 | 4341 |
Notice: This operation modifies the receiver, NOT a copy; |
4342 |
therefore the change may affect all others referencing the receiver." |
|
4343 |
||
8874 | 4344 |
^ self |
4345 |
replaceFrom:startIndex |
|
4346 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
4347 |
with:replacementCollection |
|
4348 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4349 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4350 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4351 |
args: startIndex : <integer> |
8874 | 4352 |
replacementCollection : <colleciton of <object> > |
4353 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4354 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4355 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4356 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4357 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4358 |
" |
8874 | 4359 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 4360 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
4361 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4362 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4363 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 4364 |
! |
4365 |
||
61 | 4366 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
4367 |
"replace elements in destColl with elements from the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4368 |
|
61 | 4369 |
Notice: This operation modifies the destination collection, NOT a copy; |
4370 |
therefore the change may affect all others referencing this object." |
|
4371 |
||
8874 | 4372 |
destColl |
4373 |
replaceFrom:destStartIndex |
|
4374 |
to:destEndIndex |
|
4375 |
with:self |
|
4376 |
startingAt:sourceStart |
|
61 | 4377 |
|
4378 |
" |
|
4379 |
|s| |
|
4380 |
s := 'abcdefghijklmnop'. |
|
4381 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
4382 |
s'123defghijklmnop' |
|
4383 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4384 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4385 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 4386 |
! ! |
4387 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4388 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4389 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4390 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4391 |
"return a new collection of length size, which contains the receiver |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4392 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4393 |
Elements on the left are filled with padElement. |
8874 | 4394 |
If the receivers size is equal or greater than the length argument, |
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4395 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4396 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4397 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4398 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4399 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4400 |
(len < size) ifTrue:[ |
8874 | 4401 |
s := self species new:size withAll:padElement. |
4402 |
s replaceFrom:(size - len + 1) with:self. |
|
4403 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4404 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4405 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4406 |
" |
8874 | 4407 |
'foo' leftPaddedTo:10 with:$. |
4408 |
'fooBar' leftPaddedTo:5 with:$. |
|
4409 |
123 printString leftPaddedTo:10 with:$. |
|
4410 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
4411 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
4412 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
4413 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
4414 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
4415 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
4416 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4417 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4418 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4419 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4420 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4421 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4422 |
plus pad elements up to length. |
8874 | 4423 |
If the receivers size is equal or greater than the length argument, |
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4424 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4425 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4426 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4427 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4428 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4429 |
len < newSize ifTrue:[ |
8874 | 4430 |
s := self species new:newSize withAll:padElement. |
4431 |
s replaceFrom:1 to:len with:self. |
|
4432 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4433 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4434 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4435 |
" |
8874 | 4436 |
'foo' paddedTo:10 with:$. |
4437 |
123 printString paddedTo:10 with:$* |
|
4438 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
4439 |
(Float pi printString) paddedTo:15 with:$- |
|
4440 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
4441 |
#[1 2 3 4] paddedTo:6 with:0 |
|
4442 |
#[1 2 3 4] paddedTo:10 with:99 |
|
4443 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4444 |
" |
7116 | 4445 |
! |
4446 |
||
4447 |
paddedToMultipleOf:sizeModulu with:padElement |
|
4448 |
|mySize mod numPad| |
|
4449 |
||
4450 |
mySize := self size. |
|
4451 |
mod := (mySize \\ sizeModulu). |
|
4452 |
mod == 0 ifTrue:[^ self]. |
|
4453 |
||
4454 |
numPad := sizeModulu - mod. |
|
4455 |
^ self , (self species new:numPad withAll:padElement). |
|
4456 |
||
4457 |
" |
|
8874 | 4458 |
#[] paddedToMultipleOf:3 with:0 |
4459 |
#[1] paddedToMultipleOf:3 with:0 |
|
4460 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
4461 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 4462 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
4463 |
||
4464 |
#() paddedToMultipleOf:3 with:nil |
|
4465 |
#(1) paddedToMultipleOf:3 with:nil |
|
4466 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
4467 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
4468 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
4469 |
||
4470 |
'' paddedToMultipleOf:3 with:Character space |
|
4471 |
'a' paddedToMultipleOf:3 with:Character space |
|
4472 |
'ab' paddedToMultipleOf:3 with:Character space |
|
4473 |
'abc' paddedToMultipleOf:3 with:Character space |
|
4474 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
4475 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4476 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4477 |
|
7260 | 4478 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 4479 |
|
5358 | 4480 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 4481 |
"Private!! |
5358 | 4482 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
4483 |
||
4484 |
| i1 i2 val1 val2 out | |
|
4485 |
||
4486 |
i1 := first. |
|
4487 |
i2 := middle + 1. |
|
4488 |
val1 := self at: i1. |
|
4489 |
val2 := self at: i2. |
|
4490 |
out := first - 1. "will be pre-incremented" |
|
4491 |
||
4492 |
"select 'lower' half of the elements based on comparator" |
|
4493 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 4494 |
(aBlock value: val1 value: val2) |
4495 |
ifTrue: [ |
|
4496 |
dst at: (out := out + 1) put: val1. |
|
4497 |
val1 := self at: (i1 := i1 + 1)] |
|
4498 |
ifFalse: [ |
|
4499 |
dst at: (out := out + 1) put: val2. |
|
4500 |
i2 := i2 + 1. |
|
4501 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 4502 |
|
4503 |
"copy the remaining elements" |
|
4504 |
i1 <= middle |
|
8874 | 4505 |
ifTrue: [ |
4506 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
4507 |
ifFalse: [ |
|
4508 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 4509 |
|
4510 |
! |
|
4511 |
||
4512 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
4513 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
4514 |
||
4515 |
| middle | |
|
4516 |
||
4517 |
first = last ifTrue: [^ self]. |
|
4518 |
middle := (first + last) // 2. |
|
4519 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
4520 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
4521 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
4522 |
||
4523 |
||
4524 |
! |
|
4525 |
||
2069 | 4526 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4527 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4528 |
Simulates recursion in a stack, to avoid recursion overflow |
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4529 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4530 |
|
8874 | 4531 |
Use #< for element comparisons, since this is the (fastest) base |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
4532 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4533 |
|
4534 |
|begin "{ Class: SmallInteger }" |
|
4535 |
end "{ Class: SmallInteger }" |
|
4536 |
b "{ Class: SmallInteger }" |
|
4537 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4538 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4539 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4540 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4541 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4542 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4543 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4544 |
[stack notEmpty] whileTrue:[ |
8874 | 4545 |
end := stack removeLast. |
4546 |
begin := stack removeLast. |
|
4547 |
||
4548 |
b := begin. |
|
4549 |
e := end. |
|
4550 |
middleElement := self at:((b + e) // 2). |
|
4551 |
||
4552 |
[b < e] whileTrue:[ |
|
4553 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4554 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4555 |
||
4556 |
(b <= e) ifTrue:[ |
|
4557 |
(b == e) ifFalse:[ |
|
4558 |
temp1 := self at:b. temp2 := self at:e. |
|
4559 |
self at:b put:temp2. self at:e put:temp1 |
|
4560 |
]. |
|
4561 |
b := b + 1. |
|
4562 |
e := e - 1 |
|
4563 |
] |
|
4564 |
]. |
|
4565 |
(begin < e) ifTrue:[ |
|
4566 |
stack add:begin. |
|
4567 |
stack add:e. |
|
4568 |
]. |
|
4569 |
(b < end) ifTrue:[ |
|
4570 |
stack add:b. |
|
4571 |
stack add:end. |
|
4572 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4573 |
] |
2069 | 4574 |
! |
4575 |
||
4576 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4577 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4578 |
Simulates recursion in a stack, to avoid recursion overflow |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4579 |
with degenerated collections." |
2069 | 4580 |
|
4581 |
|begin "{ Class: SmallInteger }" |
|
4582 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4583 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4584 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4585 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4586 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4587 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4588 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4589 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4590 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4591 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4592 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4593 |
[stack notEmpty] whileTrue:[ |
8874 | 4594 |
end := stack removeLast. |
4595 |
begin := stack removeLast. |
|
4596 |
||
4597 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4598 |
elB := self at:begin. |
|
4599 |
elE := self at:end. |
|
4600 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4601 |
temp := elB. elB := elE. elE := temp. |
|
4602 |
self at:begin put:elB. |
|
4603 |
self at:end put:elE. |
|
4604 |
]. |
|
4605 |
||
4606 |
nToSort > 2 ifTrue:[ |
|
4607 |
m := (begin + end) // 2. |
|
4608 |
elM := self at:m. |
|
4609 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4610 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4611 |
temp := self at:end. |
|
4612 |
self at:end put:(self at:m). |
|
4613 |
self at:m put:temp. |
|
4614 |
elM := elE |
|
4615 |
] |
|
4616 |
] ifFalse:[ |
|
4617 |
temp := self at:begin. |
|
4618 |
self at:begin put:(self at:m). |
|
4619 |
self at:m put:temp. |
|
4620 |
elM := elB |
|
4621 |
]. |
|
4622 |
nToSort > 3 ifTrue:[ |
|
4623 |
bRun := begin. |
|
4624 |
eRun := end. |
|
4625 |
[ |
|
4626 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4627 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4628 |
bRun <= eRun |
|
4629 |
] whileTrue:[ |
|
4630 |
temp := self at:bRun. |
|
4631 |
self at:bRun put:(self at:eRun). |
|
4632 |
self at:eRun put:temp. |
|
4633 |
]. |
|
4634 |
stack add:begin. |
|
4635 |
stack add:eRun. |
|
4636 |
||
4637 |
stack add:bRun. |
|
4638 |
stack add:end. |
|
4639 |
] |
|
4640 |
] |
|
4641 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4642 |
] |
2069 | 4643 |
! |
4644 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4645 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock policy:p |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4646 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4647 |
Simulates recursion in a stack, to avoid recursion overflow |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4648 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4649 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4650 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4651 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4652 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4653 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4654 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4655 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4656 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4657 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4658 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4659 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4660 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4661 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4662 |
[stack notEmpty] whileTrue:[ |
8874 | 4663 |
end := stack removeLast. |
4664 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4665 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4666 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 4667 |
elB := self at:begin. |
4668 |
elE := self at:end. |
|
4669 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
4670 |
temp := elB. elB := elE. elE := temp. |
|
4671 |
self at:begin put:elB. |
|
4672 |
self at:end put:elE. |
|
4673 |
]. |
|
4674 |
||
4675 |
nToSort > 2 ifTrue:[ |
|
4676 |
m := (begin + end) // 2. |
|
4677 |
elM := self at:m. |
|
4678 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
4679 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
4680 |
temp := self at:end. |
|
4681 |
self at:end put:(self at:m). |
|
4682 |
self at:m put:temp. |
|
4683 |
elM := elE |
|
4684 |
] |
|
4685 |
] ifFalse:[ |
|
4686 |
temp := self at:begin. |
|
4687 |
self at:begin put:(self at:m). |
|
4688 |
self at:m put:temp. |
|
4689 |
elM := elB |
|
4690 |
]. |
|
4691 |
nToSort > 3 ifTrue:[ |
|
4692 |
bRun := begin. |
|
4693 |
eRun := end. |
|
4694 |
[ |
|
4695 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
4696 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
4697 |
bRun <= eRun |
|
4698 |
] whileTrue:[ |
|
4699 |
temp := self at:bRun. |
|
4700 |
self at:bRun put:(self at:eRun). |
|
4701 |
self at:eRun put:temp. |
|
4702 |
]. |
|
4703 |
stack add:begin. |
|
4704 |
stack add:eRun. |
|
4705 |
||
4706 |
stack add:bRun. |
|
4707 |
stack add:end. |
|
4708 |
] |
|
4709 |
] |
|
4710 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4711 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4712 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4713 |
|
2069 | 4714 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4715 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4716 |
Simulates recursion in a stack, to avoid recursion overflow |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4717 |
with degenerated collections." |
2069 | 4718 |
|
4719 |
|begin "{ Class: SmallInteger }" |
|
4720 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4721 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4722 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4723 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4724 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4725 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4726 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4727 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4728 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4729 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4730 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4731 |
[stack notEmpty] whileTrue:[ |
8874 | 4732 |
end := stack removeLast. |
4733 |
begin := stack removeLast. |
|
4734 |
||
4735 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4736 |
||
4737 |
elB := self at:begin. |
|
4738 |
elE := self at:end. |
|
4739 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4740 |
temp := elB. elB := elE. elE := temp. |
|
4741 |
self at:begin put:elB. |
|
4742 |
self at:end put:elE. |
|
4743 |
||
4744 |
temp := aCollection at:end. |
|
4745 |
aCollection at:end put:(aCollection at:begin). |
|
4746 |
aCollection at:begin put:temp. |
|
4747 |
]. |
|
4748 |
||
4749 |
nToSort > 2 ifTrue:[ |
|
4750 |
m := (begin + end) // 2. |
|
4751 |
elM := self at:m. |
|
4752 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4753 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4754 |
temp := self at:end. |
|
4755 |
self at:end put:(self at:m). |
|
4756 |
self at:m put:temp. |
|
4757 |
||
4758 |
temp := aCollection at:end. |
|
4759 |
aCollection at:end put:(aCollection at:m). |
|
4760 |
aCollection at:m put:temp. |
|
4761 |
||
4762 |
elM := elE |
|
4763 |
] |
|
4764 |
] ifFalse:[ |
|
4765 |
temp := self at:begin. |
|
4766 |
self at:begin put:(self at:m). |
|
4767 |
self at:m put:temp. |
|
4768 |
||
4769 |
temp := aCollection at:begin. |
|
4770 |
aCollection at:begin put:(aCollection at:m). |
|
4771 |
aCollection at:m put:temp. |
|
4772 |
elM := elB |
|
4773 |
]. |
|
4774 |
nToSort > 3 ifTrue:[ |
|
4775 |
bRun := begin. |
|
4776 |
eRun := end. |
|
4777 |
[ |
|
4778 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4779 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4780 |
bRun <= eRun |
|
4781 |
] whileTrue:[ |
|
4782 |
temp := self at:bRun. |
|
4783 |
self at:bRun put:(self at:eRun). |
|
4784 |
self at:eRun put:temp. |
|
4785 |
||
4786 |
temp := aCollection at:bRun. |
|
4787 |
aCollection at:bRun put:(aCollection at:eRun). |
|
4788 |
aCollection at:eRun put:temp. |
|
4789 |
]. |
|
4790 |
stack add:begin. |
|
4791 |
stack add:eRun. |
|
4792 |
||
4793 |
stack add:bRun. |
|
4794 |
stack add:end. |
|
4795 |
] |
|
4796 |
] |
|
4797 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4798 |
] |
2069 | 4799 |
! |
4800 |
||
4801 |
quickSortFrom:inBegin to:inEnd with:aCollection |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4802 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4803 |
Simulates recursion in a stack, to avoid recursion overflow |
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4804 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4805 |
|
8874 | 4806 |
Use #< for element comparisons, since this is the (fastest) base |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
4807 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4808 |
|
4809 |
|begin "{ Class: SmallInteger }" |
|
4810 |
end "{ Class: SmallInteger }" |
|
4811 |
b "{ Class: SmallInteger }" |
|
4812 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4813 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4814 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4815 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4816 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4817 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4818 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4819 |
[stack notEmpty] whileTrue:[ |
8874 | 4820 |
end := stack removeLast. |
4821 |
begin := stack removeLast. |
|
4822 |
||
4823 |
b := begin. |
|
4824 |
e := end. |
|
4825 |
middleElement := self at:((b + e) // 2). |
|
4826 |
||
4827 |
[b < e] whileTrue:[ |
|
4828 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4829 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4830 |
||
4831 |
(b <= e) ifTrue:[ |
|
4832 |
(b == e) ifFalse:[ |
|
4833 |
temp1 := self at:b. temp2 := self at:e. |
|
4834 |
self at:b put:temp2. self at:e put:temp1. |
|
4835 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
4836 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
4837 |
]. |
|
4838 |
b := b + 1. |
|
4839 |
e := e - 1 |
|
4840 |
] |
|
4841 |
]. |
|
4842 |
(begin < e) ifTrue:[ |
|
4843 |
stack add:begin. |
|
4844 |
stack add:e. |
|
4845 |
]. |
|
4846 |
(b < end) ifTrue:[ |
|
4847 |
stack add:b. |
|
4848 |
stack add:end. |
|
4849 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4850 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4851 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4852 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4853 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4854 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4855 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4856 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4857 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4858 |
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
|
4859 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4860 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4861 |
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
|
4862 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4863 |
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
|
4864 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4865 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4866 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4867 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4868 |
self obsoleteMethodWarning:'use quickSortFrom:to:sortBlock:with:'. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4869 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4870 |
"/ code was never used - use regular sort for backward compatibility |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4871 |
^ self quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4872 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4873 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4874 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4875 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4876 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4877 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4878 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4879 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4880 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4881 |
"/ begin := inBegin. "/ this also does a type-check |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4882 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4883 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4884 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4885 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4886 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4887 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4888 |
"/ "/ by exchanging the first element with any random |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4889 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4890 |
"/ rnd := Random nextIntegerBetween:b and:e. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4891 |
"/ temp1 := self at:b. temp2 := self at:rnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4892 |
"/ self at:b put:temp2. self at:rnd put:temp1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4893 |
"/ aCollection notNil ifTrue:[ |
8874 | 4894 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4895 |
"/ aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4896 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4897 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4898 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4899 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4900 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4901 |
"/ middleElement := self at:((b + e) // 2). |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4902 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4903 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4904 |
"/ [b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4905 |
"/ [e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4906 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4907 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4908 |
"/ (b == e) ifFalse:[ |
8874 | 4909 |
"/ temp1 := self at:b. temp2 := self at:e. |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4910 |
"/ self at:b put:temp2. self at:e put:temp1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4911 |
"/ aCollection notNil ifTrue:[ |
8874 | 4912 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:e. |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4913 |
"/ aCollection at:b put:temp2. aCollection at:e put:temp1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4914 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4915 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4916 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4917 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4918 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4919 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4920 |
"/ (begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4921 |
"/ (b < end) ifTrue:[self randomizedQuickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4922 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4923 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4924 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4925 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4926 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4927 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4928 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4929 |
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
|
4930 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4931 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4932 |
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
|
4933 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4934 |
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
|
4935 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4936 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4937 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4938 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4939 |
self obsoleteMethodWarning:'use quickSortFrom:to:with:'. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4940 |
"/ code was never used - use regular sort for backward compatibility |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4941 |
^ self quickSortFrom:inBegin to:inEnd with:aCollection |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4942 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4943 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4944 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4945 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4946 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4947 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4948 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4949 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4950 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4951 |
"/ begin := inBegin. "/ this also does a type-check |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4952 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4953 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4954 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4955 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4956 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4957 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4958 |
"/ "/ by exchanging the first element with any random |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4959 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4960 |
"/ rnd := Random nextIntegerBetween:b and:e. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4961 |
"/ temp1 := self at:b. temp2 := self at:rnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4962 |
"/ self at:b put:temp2. self at:rnd put:temp1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4963 |
"/ aCollection notNil ifTrue:[ |
8874 | 4964 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4965 |
"/ aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4966 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4967 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4968 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4969 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4970 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4971 |
"/ middleElement := self at:((b + e) // 2). |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4972 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4973 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4974 |
"/ [b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4975 |
"/ [e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4976 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4977 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4978 |
"/ (b == e) ifFalse:[ |
8874 | 4979 |
"/ temp1 := self at:b. temp2 := self at:e. |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4980 |
"/ self at:b put:temp2. self at:e put:temp1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4981 |
"/ aCollection notNil ifTrue:[ |
8874 | 4982 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:e. |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4983 |
"/ aCollection at:b put:temp2. aCollection at:e put:temp1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4984 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4985 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4986 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4987 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4988 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4989 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4990 |
"/ (begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e with:aCollection]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4991 |
"/ (b < end) ifTrue:[self randomizedQuickSortFrom:b to:end with:aCollection] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4992 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4993 |
"/ "Modified: 21.8.1997 / 18:30:19 / cg" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4994 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 4995 |
! ! |
4996 |
||
602 | 4997 |
!SequenceableCollection methodsFor:'queries'! |
4998 |
||
4999 |
firstIndex |
|
5000 |
"return the first elements index" |
|
5001 |
||
5002 |
^ 1 |
|
5003 |
! |
|
5004 |
||
5005 |
isSequenceable |
|
5006 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5007 |
||
5008 |
^ true |
|
1 | 5009 |
! |
5010 |
||
602 | 5011 |
isSequenceableCollection |
5012 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5013 |
This method is a historic leftover and will be removed." |
|
5014 |
||
5872 | 5015 |
<resource:#obsolete> |
5016 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5017 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5018 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5019 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5020 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5021 |
! |
5022 |
||
5358 | 5023 |
isSorted |
5024 |
"return true. if my elements are sorted (already)" |
|
5025 |
||
5026 |
|lastEl el| |
|
5027 |
||
5028 |
lastEl := self first. |
|
5029 |
2 to:self size do:[:i | |
|
8874 | 5030 |
el := self at:i. |
5031 |
el >= lastEl ifFalse: [^ false]. |
|
5032 |
lastEl := el |
|
5358 | 5033 |
]. |
5034 |
^ true |
|
5035 |
||
5036 |
" |
|
8874 | 5037 |
#(1 2 3 5 10 100) isSorted |
5038 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5039 |
" |
5040 |
! |
|
5041 |
||
5042 |
isSortedBy:aBlock |
|
5043 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5044 |
||
5045 |
|lastEl el| |
|
5046 |
||
5047 |
lastEl := self first. |
|
5048 |
2 to:self size do:[:i | |
|
8874 | 5049 |
el := self at:i. |
5050 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5051 |
lastEl := el |
|
5358 | 5052 |
]. |
5053 |
^ true |
|
5054 |
||
5055 |
" |
|
8874 | 5056 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5057 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5058 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5059 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5060 |
" |
5061 |
||
5062 |
! |
|
5063 |
||
602 | 5064 |
keys |
5065 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5066 |
||
5067 |
|sz| |
|
5068 |
||
1 | 5069 |
sz := self size. |
602 | 5070 |
sz == 0 ifTrue:[ |
8874 | 5071 |
^ #() |
61 | 5072 |
]. |
602 | 5073 |
^ 1 to:sz |
1 | 5074 |
! |
5075 |
||
602 | 5076 |
lastIndex |
5077 |
"return the last elements index" |
|
5078 |
||
5079 |
^ self size |
|
61 | 5080 |
! |
5081 |
||
602 | 5082 |
size |
5083 |
"return the number of elements in the collection. |
|
5084 |
concrete implementations must define this" |
|
5085 |
||
5086 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5087 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5088 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5089 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5090 |
"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
|
5091 |
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
|
5092 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5093 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5094 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5095 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5096 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5097 |
! ! |
5098 |
||
9414 | 5099 |
|
1 | 5100 |
!SequenceableCollection methodsFor:'searching'! |
5101 |
||
3887 | 5102 |
detect:aBlock startingAt:startIndex |
5103 |
"find the first element, for which evaluation of the argument, aBlock |
|
5104 |
return true, starting the search with startIndex. |
|
5105 |
If none does so, report an error" |
|
5106 |
||
5107 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5108 |
||
5109 |
" |
|
8874 | 5110 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5111 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5112 |
" |
5113 |
||
5114 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5115 |
! |
|
5116 |
||
5117 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5118 |
"find the first element, for which evaluation of the argument, aBlock |
|
5119 |
return true, starting the search with startIndex. |
|
5717 | 5120 |
If none does so, return the evaluation of exceptionBlock" |
5121 |
||
5122 |
|stop| |
|
3887 | 5123 |
|
5124 |
stop := self size. |
|
5717 | 5125 |
self from:startIndex to:stop do:[:el | |
8874 | 5126 |
(aBlock value:el) ifTrue:[ |
5127 |
^ el |
|
5128 |
]. |
|
3887 | 5129 |
]. |
5130 |
^ exceptionBlock value |
|
5131 |
||
5132 |
" |
|
8874 | 5133 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5134 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5135 |
" |
5136 |
||
5137 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5138 |
! |
|
5139 |
||
602 | 5140 |
findFirst:aBlock |
5141 |
"find the first element, for which evaluation of the argument, aBlock |
|
3887 | 5142 |
returns true; return its index or 0 if none detected. |
5143 |
This is much like #detect, however, here an INDEX is returned, |
|
5144 |
while #detect returns the element." |
|
602 | 5145 |
|
5146 |
|stop "{ Class: SmallInteger }" | |
|
5147 |
||
32 | 5148 |
stop := self size. |
602 | 5149 |
1 to:stop do:[:index | |
8874 | 5150 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5151 |
]. |
5152 |
^ 0 |
|
159 | 5153 |
|
95 | 5154 |
" |
602 | 5155 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
5156 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
8874 | 5157 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
602 | 5158 |
'one.two.three' findFirst:[:c | (c == $.)] |
8874 | 5159 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
5160 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
95 | 5161 |
" |
3887 | 5162 |
|
5163 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
1 | 5164 |
! |
5165 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5166 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5167 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5168 |
returns true; return its index or 0 if none detected. |
5169 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5170 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5171 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5172 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5173 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5174 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5175 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5176 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5177 |
start to:stop do:[:index | |
8874 | 5178 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5179 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5180 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5181 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5182 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5183 |
#(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
|
5184 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5185 |
" |
3887 | 5186 |
|
5187 |
"Modified: / 21.10.1998 / 18:48:22 / cg" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5188 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5189 |
|
602 | 5190 |
findLast:aBlock |
5191 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5192 |
returns true. |
5193 |
Return its index or 0 if none detected." |
|
602 | 5194 |
|
5195 |
|start "{ Class: SmallInteger }"| |
|
5196 |
||
5197 |
start := self size. |
|
5198 |
start to:1 by:-1 do:[:index | |
|
8874 | 5199 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5200 |
]. |
5201 |
^ 0 |
|
5202 |
||
5203 |
" |
|
602 | 5204 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5205 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5206 |
" |
5207 |
! |
|
5208 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5209 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5210 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5211 |
returns true. Start the search at startIndex. |
5212 |
Return its index or 0 if none detected." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5213 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5214 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5215 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5216 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5217 |
start to:1 by:-1 do:[:index | |
8874 | 5218 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5219 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5220 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5221 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5222 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5223 |
#(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
|
5224 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5225 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5226 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5227 |
|
8193 | 5228 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
5229 |
"find the last element, for which evaluation of the argument, aBlock |
|
5230 |
returns true. Start the search at startIndex. |
|
5231 |
End the search at endIndex or when an element is found. |
|
5232 |
Return its index or 0 if none detected." |
|
5233 |
||
5234 |
|start "{ Class: SmallInteger }"| |
|
5235 |
||
5236 |
start := startIndex. |
|
5237 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 5238 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 5239 |
]. |
5240 |
^ 0 |
|
5241 |
||
5242 |
" |
|
5243 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
5244 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
5245 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
5246 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
5247 |
" |
|
5248 |
! |
|
5249 |
||
159 | 5250 |
indexOfAny:aCollection |
5251 |
"search the collection for an element in aCollection. |
|
5252 |
if found, return the index otherwise return 0. |
|
8874 | 5253 |
The comparison is done using = |
159 | 5254 |
(i.e. equality test - not identity test). |
5255 |
||
5256 |
Notice, that for big collections, the runtime of this search |
|
5257 |
grows proportional to size(receiver) * size(aCollection). |
|
5258 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5259 |
||
5260 |
^ self indexOfAny:aCollection startingAt:1 |
|
5261 |
||
5262 |
" |
|
5263 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
5264 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
5265 |
" |
|
5266 |
! |
|
5267 |
||
5268 |
indexOfAny:aCollection startingAt:start |
|
5269 |
"search the collection for an element in aCollection, |
|
5270 |
starting the search at index start; |
|
5271 |
if found, return the index otherwise return 0. |
|
8874 | 5272 |
The comparison is done using = |
159 | 5273 |
(i.e. equality test - not identity test). |
5274 |
||
5275 |
Notice, that for big collections, the runtime of this search |
|
5276 |
grows proportional to size(receiver) * size(aCollection). |
|
5277 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5278 |
||
5279 |
||
5280 |
|startIndex "{ Class: SmallInteger }" |
|
5281 |
stop "{ Class: SmallInteger }" | |
|
5282 |
||
5283 |
startIndex := start. |
|
5284 |
stop := self size. |
|
5285 |
startIndex to:stop do:[:index | |
|
8874 | 5286 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 5287 |
]. |
5288 |
^ 0 |
|
5289 |
" |
|
8874 | 5290 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
5291 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 5292 |
" |
1 | 5293 |
! |
5294 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5295 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5296 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5297 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5298 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 5299 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5300 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5301 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5302 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5303 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5304 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5305 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5306 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5307 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5308 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5309 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5310 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5311 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5312 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5313 |
" |
8874 | 5314 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5315 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
5316 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(99 88 77) startingAt:5 ifAbsent:['oops'] |
|
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5317 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5318 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5319 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5320 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5321 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5322 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5323 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5324 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5325 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5326 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40 30 50) |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5327 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5328 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5329 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5330 |
|
8874 | 5331 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5332 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5333 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5334 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5335 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5336 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5337 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40 30 50) startingAt:2 |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5338 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) startingAt:2 |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5339 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5340 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5341 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5342 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5343 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5344 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5345 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5346 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5347 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5348 |
|
368 | 5349 |
indexOfSubCollection:aCollection |
9234 | 5350 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 5351 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5352 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 5353 |
|
5354 |
" |
|
8874 | 5355 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
5356 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
5357 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
5358 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
5359 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
5360 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
5361 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
5362 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 5363 |
" |
5364 |
! |
|
5365 |
||
602 | 5366 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
5367 |
"find a subcollection. If found, return the index; |
|
5368 |
if not found, return the result of evaluating exceptionBlock." |
|
5369 |
||
5370 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
5371 |
! |
|
5372 |
||
368 | 5373 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5374 |
"find a subcollection starting at index. |
368 | 5375 |
If found, return the index; if not found, return 0." |
5376 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5377 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 5378 |
! |
5379 |
||
5380 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
5381 |
"find a subcollection, starting at index. If found, return the index; |
|
5382 |
if not found, return the result of evaluating exceptionBlock. |
|
5383 |
This is a q&d hack - not very efficient" |
|
5384 |
||
8874 | 5385 |
|same first |
368 | 5386 |
mySize "{Class: SmallInteger }" |
5387 |
checkIndex "{Class: SmallInteger }" |
|
5388 |
cmpIndex "{Class: SmallInteger }" |
|
5389 |
sz "{Class: SmallInteger }"| |
|
5390 |
||
5391 |
mySize := self size. |
|
5392 |
sz := aCollection size. |
|
5393 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
5394 |
first := aCollection at:1. |
|
5395 |
||
8874 | 5396 |
checkIndex := startIndex - 1. |
368 | 5397 |
[true] whileTrue:[ |
8874 | 5398 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
5399 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5400 |
||
5401 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
5402 |
^ 0 |
|
5403 |
]. |
|
5404 |
same := true. |
|
5405 |
cmpIndex := 1. |
|
5406 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5407 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
5408 |
same := false |
|
5409 |
] ifTrue:[ |
|
5410 |
cmpIndex := cmpIndex + 1 |
|
5411 |
] |
|
5412 |
]. |
|
5413 |
same ifTrue:[^ checkIndex]. |
|
5414 |
] |
|
5415 |
||
5416 |
" |
|
5417 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
5418 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
5419 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
5420 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
5421 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
5422 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 5423 |
" |
602 | 5424 |
! |
5425 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5426 |
lastIndexOfSubCollection:aCollection |
8874 | 5427 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5428 |
If found, return the index; if not found, return 0." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5429 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5430 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5431 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5432 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5433 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5434 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5435 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5436 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5437 |
"find a subcollection from the end. If found, return the index; |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5438 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5439 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5440 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5441 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5442 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5443 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5444 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5445 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5446 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5447 |
"find a subcollection from the end starting at index. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5448 |
If found, return the index; if not found, return 0." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5449 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5450 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5451 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5452 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5453 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5454 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5455 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5456 |
"find a subcollection from the end, starting at index. If found, return the index; |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5457 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5458 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5459 |
|
8874 | 5460 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5461 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5462 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5463 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5464 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5465 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5466 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5467 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5468 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5469 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5470 |
|
8874 | 5471 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5472 |
checkIndex > mySize ifTrue:[ |
8874 | 5473 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5474 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5475 |
[true] whileTrue:[ |
8874 | 5476 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
5477 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5478 |
||
5479 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
5480 |
^ 0 |
|
5481 |
]. |
|
5482 |
same := true. |
|
5483 |
cmpIndex := 1. |
|
5484 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5485 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
5486 |
same := false |
|
5487 |
] ifTrue:[ |
|
5488 |
cmpIndex := cmpIndex + 1 |
|
5489 |
] |
|
5490 |
]. |
|
5491 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
5492 |
] |
|
5493 |
||
5494 |
" |
|
5495 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
5496 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
5497 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
5498 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
5499 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
5500 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
5501 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
5502 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
5503 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:4 |
|
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5504 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5505 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5506 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5507 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5508 |
|
7264 | 5509 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5510 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5511 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5512 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5513 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5514 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5515 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5516 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5517 |
^ true |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5518 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5519 |
" |
8874 | 5520 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5521 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5522 |
returns: true - if found |
8874 | 5523 |
false - if not found |
5524 |
" |
|
5525 |
||
5526 |
" |
|
5527 |
#(10 20 30 40 50 60 70) includes:99 |
|
5528 |
#(10 20 30 40 50 60 70) includes:40 |
|
5529 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5530 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5531 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5532 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5533 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5534 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5535 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5536 |
"search the collection for anElement; |
602 | 5537 |
if found, return the index otherwise return 0. |
8874 | 5538 |
The comparison is done using = |
602 | 5539 |
(i.e. equality test - not identity test)." |
5540 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5541 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5542 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5543 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5544 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5545 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5546 |
returns: elementIndex - if found |
8874 | 5547 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5548 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5549 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5550 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5551 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5552 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5553 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5554 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5555 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5556 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5557 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5558 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5559 |
"search the collection for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5560 |
if found, return the index otherwise return the value of the |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5561 |
exceptionBlock. |
8874 | 5562 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5563 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5564 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5565 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5566 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5567 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5568 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5569 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5570 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5571 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5572 |
args: anElement : <object> |
8874 | 5573 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5574 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5575 |
returns: elementIndex - if found |
8874 | 5576 |
exceptionBlock value - if not found |
5577 |
" |
|
5578 |
||
5579 |
" |
|
5580 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
5581 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
5582 |
#(10 20 30 40 10 20 30 40) indexOf:35 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5583 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5584 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5585 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5586 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5587 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5588 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 5589 |
"search for the first occurrence of elementToFind starting at start, |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5590 |
stopping the search at stop. If found, replace the element by replacement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5591 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5592 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5593 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5594 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5595 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5596 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5597 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 5598 |
self at:idx put:replacement. |
5599 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5600 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5601 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5602 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5603 |
" |
8874 | 5604 |
args: elementToFind : <object> |
5605 |
replacement : <integer> |
|
5606 |
start : <integer> |
|
5607 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5608 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5609 |
returns: elementIndex - if found (and replaced) |
8874 | 5610 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5611 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5612 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5613 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5614 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5615 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5616 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5617 |
(a indexOf:30 replaceWith:nil startingAt:1 stoppingAt:7) printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5618 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5619 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5620 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5621 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5622 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5623 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5624 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5625 |
"search the collection for anElement, starting the search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5626 |
if found, return the index otherwise return 0. |
8874 | 5627 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5628 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5629 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5630 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5631 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5632 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5633 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5634 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5635 |
startIndex to:stop do:[:index | |
8874 | 5636 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5637 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5638 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5639 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5640 |
" |
8874 | 5641 |
args: anElement : <object> |
5642 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5643 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5644 |
returns: elementIndex - if found |
8874 | 5645 |
0 - if not found |
5646 |
" |
|
5647 |
||
5648 |
" |
|
5649 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
5650 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5651 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5652 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5653 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5654 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5655 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5656 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5657 |
"search the collection for anElement, starting the search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5658 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5659 |
If found (within the range), return the index, otherwise return 0. |
8874 | 5660 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5661 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5662 |
|
602 | 5663 |
|startIndex "{ Class: SmallInteger }" |
5664 |
stopIndex "{ Class: SmallInteger }" | |
|
5665 |
||
5666 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5667 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5668 |
startIndex to:stopIndex do:[:index | |
8874 | 5669 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5670 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5671 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5672 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5673 |
" |
8874 | 5674 |
args: anElement : <object> |
5675 |
start : <integer> |
|
5676 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5677 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5678 |
returns: elementIndex - if found |
8874 | 5679 |
exceptionBlock value - if not found |
5680 |
" |
|
5681 |
||
5682 |
" |
|
5683 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
5684 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
5685 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
5686 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5687 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5688 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5689 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5690 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5691 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5692 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5693 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5694 |
"search the collection for anElement starting the search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5695 |
if found, return the index otherwise return the value of the |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5696 |
exceptionBlock. |
8874 | 5697 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5698 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5699 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5700 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5701 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5702 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5703 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5704 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5705 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5706 |
" |
8874 | 5707 |
args: anElement : <object> |
5708 |
start : <integer> |
|
5709 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5710 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5711 |
returns: elementIndex - if found |
8874 | 5712 |
exceptionBlock value - if not found |
5713 |
" |
|
5714 |
||
5715 |
" |
|
5716 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
5717 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
5718 |
#(10 20 30 40 10 20 30 40) indexOf:35 startingAt:5 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5719 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5720 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5721 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5722 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5723 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5724 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5725 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5726 |
if found, return the index otherwise return 0. |
8874 | 5727 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5728 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5729 |
|
8874 | 5730 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5731 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5732 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5733 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5734 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5735 |
returns: elementIndex - if found |
8874 | 5736 |
0 - if not found |
5737 |
" |
|
5738 |
||
5739 |
" |
|
5740 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
5741 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
5742 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
5743 |
#(10 20 30 40 50 60 70) lastIndexOf:10 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5744 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5745 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5746 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5747 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5748 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5749 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5750 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5751 |
if found, return the index otherwise return the value of the |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5752 |
exceptionBlock. |
8874 | 5753 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5754 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5755 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5756 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5757 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5758 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5759 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5760 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5761 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5762 |
" |
8874 | 5763 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
5764 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
5765 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
5766 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5767 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5768 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5769 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5770 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5771 |
"search the collection backwards for anElement, starting the search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5772 |
if found, return the index otherwise return 0. |
8874 | 5773 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5774 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5775 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5776 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5777 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5778 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5779 |
startIndex to:1 by:-1 do:[:index | |
8874 | 5780 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 5781 |
]. |
5782 |
^ 0 |
|
5783 |
||
5784 |
" |
|
8874 | 5785 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
5786 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
5787 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
5788 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
5789 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:4 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5790 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5791 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5792 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5793 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 5794 |
"search the collection backwards for anElement starting the search at |
5795 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5796 |
otherwise return the value of the exceptionBlock. |
8874 | 5797 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5798 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5799 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5800 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5801 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5802 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5803 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5804 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5805 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5806 |
" |
8874 | 5807 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
5808 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
5809 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
5810 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5811 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5812 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5813 |
|
4788 | 5814 |
lastIndexOfAny:aCollection |
5815 |
"search the collection backwards for any in aCollection; |
|
5816 |
if found, return the index, otherwise return 0. |
|
8874 | 5817 |
The comparison is done using = |
4788 | 5818 |
(i.e. equality test - not identity test)." |
5819 |
||
8874 | 5820 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 5821 |
|
5822 |
" |
|
5823 |
args: aCollection : <collection> |
|
5824 |
||
5825 |
returns: elementIndex - if found |
|
8874 | 5826 |
0 - if not found |
5827 |
" |
|
5828 |
||
5829 |
" |
|
5830 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 5831 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 5832 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
5833 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 5834 |
" |
5835 |
||
5836 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
5837 |
! |
|
5838 |
||
5839 |
lastIndexOfAny:aCollection startingAt:start |
|
5840 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
5841 |
if found, return the index, otherwise return 0. |
|
8874 | 5842 |
The comparison is done using = |
4788 | 5843 |
(i.e. equality test - not identity test)." |
5844 |
||
5845 |
|startIndex "{ Class: SmallInteger }"| |
|
5846 |
||
5847 |
startIndex := start. |
|
5848 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 5849 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 5850 |
]. |
5851 |
^ 0 |
|
5852 |
||
5853 |
" |
|
8874 | 5854 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
5855 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 5856 |
" |
5857 |
! |
|
5858 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5859 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5860 |
"search the collection for anElement, starting the search at index start, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5861 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5862 |
if found, return the index otherwise return nil. |
8874 | 5863 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5864 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5865 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5866 |
NOTICE: nil is returned here if the element is not found, |
8874 | 5867 |
although this is somewhat strange (the indexOf-search |
5868 |
methods return 0), this is done for VW compatibility." |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5869 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5870 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5871 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5872 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5873 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5874 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5875 |
startIndex to:stop do:[:index | |
8874 | 5876 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5877 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5878 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5879 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5880 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5881 |
args: anElement : <object> |
8874 | 5882 |
start : <integer> |
5883 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5884 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5885 |
returns: elementIndex - if found |
8874 | 5886 |
nil - if not found |
5887 |
" |
|
5888 |
||
5889 |
" |
|
5890 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
5891 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
5892 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 5893 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5894 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5895 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 5896 |
! |
5897 |
||
5898 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
5899 |
"search the collection for anElement, starting the search at index start |
|
5900 |
and stopping at stop; |
|
5901 |
if found, return the index otherwise return the value of the |
|
5902 |
exceptionBlock. |
|
8874 | 5903 |
The comparison is done using = |
602 | 5904 |
(i.e. equality test - not identity test)." |
5905 |
||
5906 |
|index| |
|
5907 |
||
5908 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5909 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 5910 |
^ index |
5911 |
||
5912 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5913 |
args: anElement : <object> |
8874 | 5914 |
start : <integer> |
5915 |
stop : <integer> |
|
5916 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5917 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5918 |
returns: elementIndex - if found |
8874 | 5919 |
valueImplementor value - if not found |
5920 |
" |
|
5921 |
||
5922 |
" |
|
5923 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
5924 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
5925 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 5926 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5927 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5928 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 5929 |
! |
5930 |
||
5931 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
5932 |
"search the collection for anElement, starting the search at index start; |
|
5933 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5934 |
If found (within the range), return the index, otherwise return nil. |
8874 | 5935 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5936 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5937 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5938 |
NOTICE: nil is returned here if the element is not found, |
8874 | 5939 |
although this is somewhat strange (the indexOf-search |
5940 |
methods return 0), this is done for VW compatibility." |
|
2275 | 5941 |
|
5942 |
|startIndex "{ Class: SmallInteger }" |
|
5943 |
stopIndex "{ Class: SmallInteger }" | |
|
5944 |
||
5945 |
startIndex := startSearchIndex. |
|
5946 |
stopIndex := endSearchIndex max:1. |
|
5947 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 5948 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 5949 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5950 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5951 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5952 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5953 |
args: anElement : <object> |
8874 | 5954 |
start : <integer> |
5955 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5956 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5957 |
returns: elementIndex - if found |
8874 | 5958 |
nil - if not found |
5959 |
" |
|
5960 |
||
5961 |
" |
|
5962 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
5963 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
5964 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 5965 |
" |
5966 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5967 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 5968 |
! |
5969 |
||
5970 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
5971 |
"search the collection for anElement, starting the search at index start |
|
5972 |
and stopping at stop, doing a reverse search; |
|
5973 |
if found, return the index otherwise return the value of the |
|
5974 |
exceptionBlock. |
|
8874 | 5975 |
The comparison is done using = |
2275 | 5976 |
(i.e. equality test - not identity test)." |
5977 |
||
5978 |
|index| |
|
5979 |
||
5980 |
index := self prevIndexOf:anElement from:start to:stop. |
|
5981 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
5982 |
^ index |
|
5983 |
||
5984 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5985 |
args: anElement : <object> |
8874 | 5986 |
start : <integer> |
5987 |
stop : <integer> |
|
5988 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5989 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5990 |
returns: elementIndex - if found |
8874 | 5991 |
valueImplementor value - if not found |
5992 |
" |
|
5993 |
||
5994 |
" |
|
5995 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
5996 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
5997 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 5998 |
" |
5999 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6000 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6001 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6002 |
|
7264 | 6003 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6004 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6005 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6006 |
"search the collection for anElement using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6007 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6008 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6009 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6010 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6011 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6012 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6013 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6014 |
returns: elementIndex - if found |
8874 | 6015 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6016 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6017 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6018 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6019 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6020 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6021 |
#(10 20 30 40 50 60 70) indexOf:40.0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6022 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6023 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6024 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6025 |
#(10 20 30 40.0 50 60 70) indexOf:40.0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6026 |
#(10 20 30 40.0 50 60 70) identityIndexOf:40.0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6027 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6028 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6029 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6030 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6031 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6032 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6033 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6034 |
"search the collection for anElement using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6035 |
if found, return the index otherwise return the value of the |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6036 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6037 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6038 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6039 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6040 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6041 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6042 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6043 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6044 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6045 |
args: anElement : <object> |
8874 | 6046 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6047 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6048 |
returns: elementIndex - if found |
8874 | 6049 |
exceptionBlock value - if not found |
6050 |
" |
|
6051 |
||
6052 |
" |
|
6053 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6054 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6055 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6056 |
#(10 20 30 40 50 60 70) indexOf:40.0 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6057 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6058 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6059 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6060 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6061 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6062 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6063 |
"search the collection for anElement and anotherElement using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6064 |
if any is found, return the index otherwise return 0." |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6065 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6066 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6067 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6068 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6069 |
args: anElement : <object> |
8874 | 6070 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6071 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6072 |
returns: elementIndex - if found |
8874 | 6073 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6074 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6075 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6076 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6077 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6078 |
"search the collection for anElement and anotherElement, starting search at index start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6079 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6080 |
if any is found, return the index otherwise return 0." |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6081 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6082 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6083 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6084 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6085 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6086 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6087 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6088 |
startIndex to:stop do:[:index | |
8874 | 6089 |
el := self at:index. |
6090 |
anElement == el ifTrue:[^ index]. |
|
6091 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6092 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6093 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6094 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6095 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6096 |
args: anElement : <object> |
8874 | 6097 |
anotherElement : <object> |
6098 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6099 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6100 |
returns: elementIndex - if found |
8874 | 6101 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6102 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6103 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6104 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6105 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40 or:30 startingAt:2 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6106 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6107 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6108 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6109 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6110 |
"search the collection for anElement, starting search at index start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6111 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6112 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6113 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6114 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6115 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6116 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6117 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6118 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6119 |
startIndex to:stop do:[:index | |
8874 | 6120 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6121 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6122 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6123 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6124 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6125 |
args: anElement : <object> |
8874 | 6126 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6127 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6128 |
returns: elementIndex - if found |
8874 | 6129 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6130 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6131 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6132 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6133 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40 startingAt:5 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6134 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6135 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6136 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6137 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6138 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6139 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6140 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6141 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6142 |
"search the collection for anElement, starting the search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6143 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6144 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6145 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6146 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6147 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6148 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6149 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6150 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6151 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6152 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6153 |
startIndex to:stopIndex do:[:index | |
8874 | 6154 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6155 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6156 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6157 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6158 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6159 |
args: anElement : <object> |
8874 | 6160 |
start : <integer> |
6161 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6162 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6163 |
returns: elementIndex - if found |
8874 | 6164 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6165 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6166 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6167 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6168 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6169 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6170 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6171 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6172 |
"search the collection for anElement, starting search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6173 |
if found, return the index otherwise return the value of the |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6174 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6175 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6176 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6177 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6178 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6179 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6180 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6181 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6182 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6183 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6184 |
args: anElement : <object> |
8874 | 6185 |
start : <integer> |
6186 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6187 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6188 |
returns: elementIndex - if found |
8874 | 6189 |
exceptionBlock value - if not found |
6190 |
" |
|
6191 |
||
6192 |
" |
|
6193 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6194 |
#(10 20 30 40 10) identityIndexOf:35 startingAt:3 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6195 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6196 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6197 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6198 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6199 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6200 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6201 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6202 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6203 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6204 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6205 |
((self identityIndexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6206 |
^ true |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6207 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6208 |
" |
8874 | 6209 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6210 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6211 |
returns: true - if found |
8874 | 6212 |
false - if not found |
6213 |
" |
|
6214 |
||
6215 |
" |
|
6216 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6217 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6218 |
#(10 20 30 40 50 60 70) includes:40 |
|
6219 |
#(10 20 30 40 50 60 70) includes:40.0 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6220 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6221 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6222 |
|
8874 | 6223 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6224 |
#(10 20 30 40.0 50 60 70) includesIdentical:40.0 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6225 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6226 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6227 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6228 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6229 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6230 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6231 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6232 |
if found, return the index otherwise return 0. |
8874 | 6233 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6234 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6235 |
|
8874 | 6236 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6237 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6238 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6239 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6240 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6241 |
returns: elementIndex - if found |
8874 | 6242 |
0 - if not found |
6243 |
" |
|
6244 |
||
6245 |
" |
|
6246 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
6247 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
6248 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
6249 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:10 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6250 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6251 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6252 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6253 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6254 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6255 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6256 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6257 |
if found, return the index otherwise return the value of the |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6258 |
exceptionBlock. |
8874 | 6259 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6260 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6261 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6262 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6263 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6264 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6265 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6266 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6267 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6268 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6269 |
args: anElement : <object> |
8874 | 6270 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6271 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6272 |
returns: elementIndex - if found |
8874 | 6273 |
exceptionBlock value - if not found |
6274 |
" |
|
6275 |
||
6276 |
" |
|
6277 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
6278 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
6279 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
6280 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6281 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6282 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6283 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6284 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6285 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6286 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6287 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6288 |
"search the collection backwards for anElement, starting the search at index start; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6289 |
if found, return the index otherwise return 0. |
8874 | 6290 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6291 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6292 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6293 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6294 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6295 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6296 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6297 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6298 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6299 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6300 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6301 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6302 |
args: anElement : <object> |
8874 | 6303 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6304 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6305 |
returns: elementIndex - if found |
8874 | 6306 |
0 - if not found |
6307 |
" |
|
6308 |
||
6309 |
" |
|
6310 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
6311 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
6312 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
6313 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
6314 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:4 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6315 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6316 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6317 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6318 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6319 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6320 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6321 |
"search the collection backwards for anElement starting the search at |
6322 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6323 |
otherwise return the value of the exceptionBlock. |
8874 | 6324 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6325 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6326 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6327 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6328 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6329 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6331 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6332 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6333 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
args: anElement : <object> |
8874 | 6335 |
start : <integer> |
6336 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6337 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6338 |
returns: elementIndex - if found |
8874 | 6339 |
exceptionBlock value - if not found |
6340 |
" |
|
6341 |
||
6342 |
" |
|
6343 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
6344 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6345 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6346 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 ifAbsent:['none'] |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6347 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6348 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6349 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6350 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 6351 |
! ! |
6352 |
||
6353 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
6354 |
||
6958 | 6355 |
detectFirstInOrder:sortBlock |
6356 |
"find the first element of the collection sorted with sortBlock" |
|
6357 |
||
6358 |
|selectedForNow start| |
|
6359 |
||
6360 |
start := true. |
|
6361 |
self do:[:element| |
|
8874 | 6362 |
start ifTrue:[ |
6363 |
start := false. |
|
6364 |
selectedForNow := element. |
|
6365 |
] ifFalse:[ |
|
6366 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
6367 |
selectedForNow := element. |
|
6368 |
]. |
|
6369 |
]. |
|
6958 | 6370 |
]. |
6371 |
||
6372 |
^ selectedForNow |
|
6373 |
||
6374 |
" |
|
8874 | 6375 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
6376 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
6377 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
6378 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 6379 |
" |
6380 |
! |
|
6381 |
||
5358 | 6382 |
mergeSort |
8874 | 6383 |
"sort the collection using a mergeSort algorithm. |
6384 |
The elements are compared using'<' |
|
5358 | 6385 |
i.e. they should offer a magnitude-like protocol. |
6386 |
||
6387 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 6388 |
the best possible for all situations |
5358 | 6389 |
See also #quickSort and #randomizedSort for other sort algorithms |
6390 |
with different worst- and average case behavior)" |
|
6391 |
||
6392 |
|stop| |
|
6393 |
||
6394 |
stop := self size. |
|
6395 |
(stop > 1) ifTrue:[ |
|
8874 | 6396 |
self mergeSort:[:a :b | a < b] from:1 to:stop |
5358 | 6397 |
] |
6398 |
||
6399 |
" |
|
6400 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
6401 |
||
6402 |
|data| |
|
6403 |
data := Random new next:100000. |
|
6404 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
6405 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 6406 |
data reverse. |
5358 | 6407 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
6408 |
||
6409 |
data := Random new next:100000. |
|
6410 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6411 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6412 |
data reverse. |
5358 | 6413 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6414 |
||
6415 |
data := Random new next:100000. |
|
6416 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
6417 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 6418 |
data reverse. |
5358 | 6419 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
6420 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6421 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6422 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6423 |
mergeSort:sortBlock |
8874 | 6424 |
"sort the collection using a mergeSort algorithm. |
6425 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6426 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6427 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6428 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 6429 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6430 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6431 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6432 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6433 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6434 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6435 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6436 |
(stop > 1) ifTrue:[ |
8874 | 6437 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6438 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6439 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6440 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6441 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6442 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6443 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6444 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6445 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6446 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6447 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6448 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6449 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6450 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6451 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6452 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 6453 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6454 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6455 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6456 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6457 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6458 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 6459 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6460 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6461 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6462 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6463 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6464 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 6465 |
"Sort the given range of indices using the mergesort algorithm. |
6466 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 6467 |
as many comparisons as heapsort or quicksort." |
6468 |
||
8874 | 6469 |
"Details: recursively split the range to be sorted into two halves, |
6470 |
mergesort each half, then merge the two halves together. |
|
6471 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
6472 |
and forth between the receiver and this copy. |
|
6473 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 6474 |
resulting in the receiver being completely sorted." |
6475 |
||
6476 |
| temp | |
|
6477 |
||
6478 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
6479 |
startIndex = stopIndex ifTrue: [^ self]. |
|
6480 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 6481 |
ifFalse: [self error: 'bad start index']. |
5358 | 6482 |
stopIndex <= self size |
8874 | 6483 |
ifFalse: [self error: 'bad stop index']. |
5358 | 6484 |
temp := self clone. |
6485 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
6486 |
||
6487 |
" |
|
8874 | 6488 |
#(1 16 7 98 3 19 4 0) mergeSort |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6489 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6490 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6491 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6492 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6493 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6494 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6495 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6496 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6497 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6498 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6499 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6500 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6501 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6502 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6503 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 6504 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6505 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6506 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6507 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6508 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6509 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6510 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6511 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6512 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6513 |
The implementation uses the quicksort algorithm, which may not be |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6514 |
the best possible for all situations (quickSort has O-square worst |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6515 |
case behavior). See also #randomizedSort for a version with better |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6516 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6517 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6518 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6519 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6520 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6521 |
(stop > 1) ifTrue:[ |
8874 | 6522 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6523 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6524 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6525 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6526 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 6527 |
|
6528 |
|data| |
|
6529 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6530 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6531 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 6532 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6533 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6534 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6535 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6536 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6537 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6538 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6539 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6540 |
"sort the collection inplace using the 2-arg block sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6541 |
for comparison. This allows any sort criteria to be implemented. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6542 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6543 |
The implementation uses the quicksort algorithm, which may not be |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6544 |
the best possible for all situations (quickSort has O-square worst |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6545 |
case behavior). See also #randomizedSort: for a version with better |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6546 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6547 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6548 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6549 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6550 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6551 |
(stop > 1) ifTrue:[ |
8874 | 6552 |
sortBlock numArgs == 3 ifTrue:[ |
6553 |
"/ TODO: pass a collating policy to aBlock |
|
6554 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
6555 |
] ifFalse:[ |
|
6556 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
6557 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6558 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6559 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6560 |
" |
8874 | 6561 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6562 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6563 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6564 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6565 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6566 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6567 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6568 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6569 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6570 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6571 |
"sort the collection inplace using the 2-arg block sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6572 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6573 |
Use this, when you have a key collection to sort some other collection with. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6574 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6575 |
The implementation uses the quicksort algorithm, which may not be |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6576 |
the best possible for all situations (quickSort has O-square worst |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6577 |
case behavior). See also #randomizedSort: for a version with better |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6578 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6579 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6580 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6581 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6582 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6583 |
(stop > 1) ifTrue:[ |
8874 | 6584 |
self quickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6585 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6586 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6587 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6588 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6589 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6590 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6591 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6592 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6593 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6594 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6595 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6596 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6597 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6598 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6599 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6600 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6601 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6602 |
Use this, when you have a key collection to sort another collection with. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6603 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6604 |
The implementation uses the quicksort algorithm, which may not be |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6605 |
the best possible for all situations (quickSort has O-square worst |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6606 |
case behavior). See also #randomizedSort: for a version with better |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6607 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6608 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6609 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6610 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6611 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6612 |
(stop > 1) ifTrue:[ |
8874 | 6613 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6614 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6615 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6616 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 6617 |
! |
6618 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6619 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6620 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6621 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6622 |
|
8874 | 6623 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6624 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6625 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6626 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6627 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6628 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6629 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6630 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6631 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6632 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:nil |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6633 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6634 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6635 |
"/ code was never used - use regular sort for backward compatibility |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6636 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6637 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6638 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6639 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6640 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6641 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6642 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6643 |
|
8874 | 6644 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6645 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6646 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6647 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6648 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6649 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6650 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6651 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6652 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6653 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:nil |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6654 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6655 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6656 |
"/ code was never used - use regular sort for backward compatibility |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6657 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6658 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6659 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6660 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6661 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6662 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6663 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6664 |
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
|
6665 |
|
8874 | 6666 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6667 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6668 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6669 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6670 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6671 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6672 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6673 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6674 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6675 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6676 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6677 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6678 |
"/ code was never used - use regular sort for backward compatibility |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6679 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6680 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6681 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6682 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6683 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6684 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6685 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6686 |
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
|
6687 |
|
8874 | 6688 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6689 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6690 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6691 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6692 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6693 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6694 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6695 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6696 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6697 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6698 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6699 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6700 |
"/ code was never used - use regular sort for backward compatibility |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6701 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6702 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6703 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6704 |
|
602 | 6705 |
reverse |
7335 | 6706 |
"destructively reverse the order of the elements inplace. |
6707 |
WARNING: this is a destructive operation, which modifies the receiver." |
|
602 | 6708 |
|
6709 |
|lowIndex "{ Class: SmallInteger }" |
|
6710 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6711 |
t1 t2| |
602 | 6712 |
|
6713 |
hiIndex := self size. |
|
6714 |
lowIndex := 1. |
|
6715 |
[lowIndex < hiIndex] whileTrue:[ |
|
8874 | 6716 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
6717 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
6718 |
||
6719 |
lowIndex := lowIndex + 1. |
|
6720 |
hiIndex := hiIndex - 1 |
|
602 | 6721 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6722 |
|
69 | 6723 |
" |
602 | 6724 |
#(4 5 6 7 7) reverse |
6725 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 6726 |
" |
1 | 6727 |
! |
6728 |
||
5004 | 6729 |
reversed |
6730 |
"return a copy with elements in reverse order" |
|
6731 |
||
6732 |
^ self copy reverse |
|
6733 |
||
6734 |
" |
|
8874 | 6735 |
#(4 5 6 7 7) reversed |
5004 | 6736 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
6737 |
" |
|
6738 |
! |
|
6739 |
||
1 | 6740 |
sort |
6741 |
"sort the collection inplace. The elements are compared using |
|
2069 | 6742 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6743 |
|
159 | 6744 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6745 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6746 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6747 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 6748 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6749 |
self quickSort |
1 | 6750 |
|
95 | 6751 |
" |
6752 |
#(1 16 7 98 3 19 4 0) sort |
|
6753 |
||
6754 |
|data| |
|
6755 |
data := Random new next:100000. |
|
6756 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6757 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6758 |
data reverse. |
95 | 6759 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6760 |
" |
|
2069 | 6761 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6762 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 6763 |
! |
6764 |
||
6765 |
sort:sortBlock |
|
6766 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6767 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6768 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6769 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6770 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6771 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6772 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6773 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6774 |
self quickSort:sortBlock |
1 | 6775 |
|
95 | 6776 |
" |
8874 | 6777 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6778 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6779 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6780 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 6781 |
" |
2069 | 6782 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
6783 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 6784 |
! |
6785 |
||
6786 |
sort:sortBlock with:aCollection |
|
6787 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 6788 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6789 |
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
|
6790 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6791 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6792 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6793 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6794 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6795 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6796 |
self quickSort:sortBlock with:aCollection |
1 | 6797 |
|
69 | 6798 |
" |
6799 |
|c1 c2| |
|
1 | 6800 |
c1 := #(1 16 7 9). |
6801 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
6802 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 6803 |
c1 printNL. |
6804 |
c2 printNL |
|
69 | 6805 |
" |
2069 | 6806 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6807 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 6808 |
! |
6809 |
||
602 | 6810 |
sortWith:aCollection |
2069 | 6811 |
"sort the receiver collection inplace, using '<' to compare elements. |
6812 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6813 |
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
|
6814 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6815 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6816 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6817 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6818 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 6819 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6820 |
self quickSortWith:aCollection |
424 | 6821 |
! |
6822 |
||
9072 | 6823 |
sortedBy: aBlock |
6824 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
|
6825 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
6826 |
||
6827 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
6828 |
! |
|
6829 |
||
602 | 6830 |
topologicalSort:sortBlock |
6831 |
"sort the collection inplace using a sloooow sort algorithm. |
|
6832 |
This algorithm has O-square runtime behavior and should be used only |
|
6833 |
in special situations. |
|
6834 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
6835 |
a>c (for example, to sort classes by inheritance) |
|
6836 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6837 |
In all other situations, use #sort; which implements the quicksort algorithm. |
77 | 6838 |
" |
602 | 6839 |
|
6840 |
|smallestIndex "{ Class: SmallInteger }" |
|
6841 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6842 |
index2 "{ Class: SmallInteger }" |
602 | 6843 |
smallest thisOne| |
6844 |
||
6845 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
6846 |
||
6847 |
end := self size. |
|
862 | 6848 |
1 to:end-1 do:[:index | |
8874 | 6849 |
smallest := self at:index. |
6850 |
smallestIndex := index. |
|
6851 |
||
6852 |
index2 := index + 1. |
|
6853 |
[ |
|
6854 |
thisOne := self at:index2. |
|
6855 |
(index2 ~~ smallestIndex |
|
6856 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
6857 |
smallestIndex := index2. |
|
6858 |
smallest := thisOne. |
|
6859 |
index2 := index + 1. |
|
6860 |
] ifFalse:[ |
|
6861 |
index2 := index2 + 1. |
|
6862 |
]. |
|
6863 |
] doWhile:[index2 <= end]. |
|
862 | 6864 |
|
6865 |
"/ (index + 1) to:end do:[:index2 | |
|
6866 |
"/ thisOne := self at:index2. |
|
6867 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
6868 |
"/ smallestIndex := index2. |
|
6869 |
"/ smallest := thisOne |
|
6870 |
"/ ]. |
|
6871 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6872 |
|
8874 | 6873 |
(smallestIndex ~~ index) ifTrue:[ |
6874 |
thisOne := self at:index. |
|
6875 |
self at:index put:smallest. |
|
6876 |
self at:smallestIndex put:thisOne |
|
6877 |
]. |
|
1 | 6878 |
] |
95 | 6879 |
|
77 | 6880 |
" |
8874 | 6881 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
6882 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
6883 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
6884 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 6885 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6886 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6887 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 6888 |
! ! |
602 | 6889 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6890 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6891 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6892 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6893 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6894 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 6895 |
however, having a common protocol with indexed collections |
6896 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6897 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6898 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6899 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6900 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6901 |
" |
8874 | 6902 |
#(1 2 3) includesKey:4 |
6903 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6904 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6905 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6906 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6907 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6908 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6909 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6910 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6911 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6912 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6913 |
|
2069 | 6914 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 6915 |
|
6916 |
version |
|
9414 | 6917 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.237 2006-07-03 16:13:22 stefan Exp $' |
628 | 6918 |
! ! |
6820 | 6919 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
6920 |
SequenceableCollection initialize! |