author | Claus Gittinger <cg@exept.de> |
Tue, 09 May 2006 11:44:30 +0200 | |
changeset 9358 | db86dc43a474 |
parent 9307 | 94ed275778b5 |
child 9366 | 218fa4e2b183 |
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]. |
|
1262 |
||
1263 |
stop := self size. |
|
1264 |
stop == (aCollection size) ifFalse:[^false]. |
|
1265 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1266 |
1 to:stop do:[:index| |
8874 | 1267 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1268 |
]. |
1269 |
^ true |
|
1270 |
||
1271 |
" |
|
8874 | 1272 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1273 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1274 |
#($1 $2 $3 $4 $5) = '12345' |
|
1275 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1276 |
" |
1277 |
! |
|
1278 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1279 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1280 |
"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
|
1281 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1282 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1283 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1284 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1285 |
! |
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 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1288 |
"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
|
1289 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1290 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1291 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1292 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1293 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1294 |
|
8874 | 1295 |
commonPrefixWith:aCollection |
6507 | 1296 |
"return the common prefix of myself and the argument, aCollection. |
1297 |
If there is none, an empty collection is returned." |
|
1298 |
||
1299 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1300 |
||
1301 |
" |
|
8874 | 1302 |
'hello' commonPrefixWith:'hello' |
1303 |
'hello' commonPrefixWith:'hElLo' |
|
1304 |
'hello' commonPrefixWith:'hello world' |
|
1305 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1306 |
||
1307 |
'hello world' commonPrefixWith:'hello' |
|
1308 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1309 |
||
1310 |
'abcd' commonPrefixWith:'bcde' |
|
1311 |
||
1312 |
'abcd' commonPrefixWith:'abab' |
|
1313 |
'abcd' commonPrefixWith:'ab' |
|
1314 |
'abcd' commonPrefixWith:'ababab' |
|
1315 |
'abcd' commonPrefixWith:'abcdef' |
|
1316 |
||
1317 |
'abab' commonPrefixWith:'abcd' |
|
1318 |
'ab' commonPrefixWith:'abcd' |
|
1319 |
'ababab' commonPrefixWith:'abcd' |
|
1320 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1321 |
" |
1322 |
! |
|
1323 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1324 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1325 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1326 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1327 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1328 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1329 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1330 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1331 |
1 to:matchLen do:[:idx | |
8874 | 1332 |
|elHere elThere same| |
1333 |
||
1334 |
elHere := self at:idx. |
|
1335 |
elThere := aCollection at:idx. |
|
1336 |
||
1337 |
ignoreCase ifTrue:[ |
|
1338 |
same := elHere sameAs:elThere |
|
1339 |
] ifFalse:[ |
|
1340 |
same := elHere = elThere |
|
1341 |
]. |
|
1342 |
same ifFalse:[ |
|
1343 |
^ self copyTo:(idx - 1). |
|
1344 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1345 |
]. |
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 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1348 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1349 |
" |
8874 | 1350 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1351 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1352 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1353 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1354 |
||
1355 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1356 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1357 |
||
1358 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1359 |
||
1360 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1361 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1362 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1363 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1364 |
||
1365 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1366 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1367 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1368 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1369 |
" |
|
1370 |
! |
|
1371 |
||
1372 |
commonSuffixWith:aCollection |
|
6507 | 1373 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1374 |
If there is none, an empty collection is returned." |
|
1375 |
||
1376 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1377 |
||
1378 |
" |
|
8874 | 1379 |
'hello' commonSuffixWith:'hello' |
1380 |
'hello' commonSuffixWith:'hElLo' |
|
1381 |
'hello' commonSuffixWith:'hello world' |
|
1382 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1383 |
'hello2 world' commonSuffixWith:'hello world' |
|
1384 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1385 |
||
1386 |
'hello world' commonSuffixWith:'world' |
|
1387 |
'hello WoRlD' commonSuffixWith:'world' |
|
1388 |
||
1389 |
'dcba' commonSuffixWith:'edcb' |
|
1390 |
||
1391 |
'dcba' commonSuffixWith:'baba' |
|
1392 |
'dcba' commonSuffixWith:'ba' |
|
1393 |
'dcba' commonSuffixWith:'bababa' |
|
1394 |
'dcba' commonSuffixWith:'fdcba' |
|
1395 |
||
1396 |
'baba' commonSuffixWith:'dcba' |
|
1397 |
'ba' commonSuffixWith:'dcba' |
|
1398 |
'bababa' commonSuffixWith:'dcba' |
|
1399 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1400 |
" |
1401 |
! |
|
1402 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1403 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1404 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1405 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1406 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1407 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1408 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1409 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1410 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1411 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1412 |
1 to:matchLen do:[:idx | |
8874 | 1413 |
|elHere elThere same| |
1414 |
||
1415 |
elHere := self at:(l1 - idx + 1). |
|
1416 |
elThere := aCollection at:(l2 - idx + 1). |
|
1417 |
||
1418 |
ignoreCase ifTrue:[ |
|
1419 |
same := elHere sameAs:elThere |
|
1420 |
] ifFalse:[ |
|
1421 |
same := elHere = elThere |
|
1422 |
]. |
|
1423 |
same ifFalse:[ |
|
1424 |
^ self copyFrom:(l1 - idx + 2). |
|
1425 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1426 |
]. |
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 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1429 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1430 |
" |
8874 | 1431 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1432 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1433 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1434 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1435 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1436 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1437 |
||
1438 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1439 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1440 |
||
1441 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1442 |
||
1443 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1444 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1445 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1446 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1447 |
||
1448 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1449 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1450 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1451 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1452 |
" |
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 |
|
5813 | 1455 |
deepSameContentsAs:aCollection |
1456 |
"return true, if the receiver and the arg have the same contents |
|
1457 |
in both the named instance vars and any indexed instVars. |
|
1458 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1459 |
||
1460 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1461 |
unused space" |
|
1462 |
||
1463 |
|index "{ Class: SmallInteger }" |
|
1464 |
stop "{ Class: SmallInteger }" | |
|
1465 |
||
1466 |
(aCollection == self) ifTrue:[^true]. |
|
1467 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1468 |
||
1469 |
stop := self size. |
|
1470 |
stop == (aCollection size) ifFalse:[^false]. |
|
1471 |
||
1472 |
index := 1. |
|
1473 |
[index <= stop] whileTrue:[ |
|
8874 | 1474 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1475 |
index := index + 1 |
|
5813 | 1476 |
]. |
1477 |
^ true |
|
1478 |
||
1479 |
" |
|
8874 | 1480 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1481 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1482 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1483 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1484 |
" |
1485 |
! |
|
1486 |
||
602 | 1487 |
endsWith:aCollection |
1488 |
"return true, if the receivers last elements match those |
|
1489 |
of aCollection" |
|
1490 |
||
1491 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1492 |
index2 "{ Class: SmallInteger }" |
1493 |
stop "{ Class: SmallInteger }" |
|
602 | 1494 |
sz "{ Class: SmallInteger }"| |
1495 |
||
1496 |
(aCollection == self) ifTrue:[^ true]. |
|
1497 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1498 |
||
1499 |
stop := aCollection size. |
|
77 | 1500 |
sz := self size. |
602 | 1501 |
stop > sz ifTrue:[^false]. |
1502 |
||
1503 |
index1 := sz. |
|
1504 |
index2 := stop. |
|
1505 |
[index2 > 0] whileTrue:[ |
|
8874 | 1506 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1507 |
index1 := index1 - 1. |
|
1508 |
index2 := index2 - 1 |
|
77 | 1509 |
]. |
602 | 1510 |
^ true |
1511 |
||
1512 |
" |
|
1513 |
'abcde' endsWith:#($d $e) |
|
8874 | 1514 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1515 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1516 |
" |
|
1517 |
! |
|
1518 |
||
7278 | 1519 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1520 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1521 |
|
1522 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1523 |
||
1524 |
" |
|
1525 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1526 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1527 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1528 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1529 |
" |
1530 |
! |
|
1531 |
||
602 | 1532 |
hash |
1533 |
"return a hash key for the receiver" |
|
1534 |
||
1535 |
"this hash is stupid - but for larger collections, the hashing |
|
1536 |
time can become much bigger than the time lost in added probing. |
|
1537 |
Time will show ..." |
|
1538 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1539 |
|mySize h| |
3653 | 1540 |
|
1541 |
mySize := self size. |
|
1542 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1543 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1544 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1545 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1546 |
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
|
1547 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1548 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1549 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1550 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1551 |
"/ 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
|
1552 |
"/ ^ ((((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
|
1553 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1554 |
" |
8874 | 1555 |
#(1 2 3 4 5 6) hash |
1556 |
(1 to:6) hash |
|
1557 |
||
1558 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1559 |
#(1 2 3 4 5) hash |
|
602 | 1560 |
" |
3653 | 1561 |
|
1562 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1563 |
! |
1564 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1565 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1566 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1567 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1568 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1569 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1570 |
^ self |
8874 | 1571 |
sameContentsAs:aCollection |
1572 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1573 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1574 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1575 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1576 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1577 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1578 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1579 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1580 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1581 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1582 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1583 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1584 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1585 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1586 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1587 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1588 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1589 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1590 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1591 |
" |
8874 | 1592 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1593 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1594 |
#($1 $2 $3 $4 $5) = '12345' |
|
1595 |
||
1596 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1597 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1598 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1599 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1600 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1601 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1602 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1603 |
|
6118 | 1604 |
isSameSequenceAs: otherCollection |
8874 | 1605 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1606 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1607 |
" |
|
1608 |
||
1609 |
| size | |
|
1610 |
||
1611 |
(size := self size) = otherCollection size ifFalse: [^false]. |
|
1612 |
||
1613 |
1 to: size do:[:index | |
|
8874 | 1614 |
(self at: index) = (otherCollection at: index) ifFalse: [^false] |
6118 | 1615 |
]. |
1616 |
^ true |
|
1617 |
! |
|
1618 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1619 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1620 |
"return true if the receiver and aCollection represent collections |
8874 | 1621 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1622 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1623 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1624 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1625 |
" |
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 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1628 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1629 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1630 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1631 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1632 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1633 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1634 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1635 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1636 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1637 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1638 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1639 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1640 |
[index <= stop] whileTrue:[ |
8874 | 1641 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1642 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1643 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1644 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1645 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1646 |
" |
8874 | 1647 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
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.0 2 3 4.0 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] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1651 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1652 |
#('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
|
1653 |
" |
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 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1656 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1657 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1658 |
|
602 | 1659 |
startsWith:aCollection |
1660 |
"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
|
1661 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1662 |
If the argument is empty, true is returned." |
602 | 1663 |
|
1664 |
|index "{ Class: SmallInteger }" |
|
1665 |
stop "{ Class: SmallInteger }" | |
|
1666 |
||
1667 |
(aCollection == self) ifTrue:[^true]. |
|
1668 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1669 |
||
1670 |
stop := aCollection size. |
|
1671 |
stop > self size ifTrue:[^false]. |
|
1672 |
||
1673 |
index := 1. |
|
1674 |
[index <= stop] whileTrue:[ |
|
8874 | 1675 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1676 |
index := index + 1 |
|
602 | 1677 |
]. |
1678 |
^ true |
|
1679 |
||
1680 |
" |
|
1681 |
'abcde' startsWith:#($a $b $c) |
|
1682 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1683 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1684 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 1685 |
" |
7279 | 1686 |
! |
1687 |
||
1688 |
startsWithAnyOf:aCollectionOfCollections |
|
1689 |
"return true, if the receiver starts with any in aCollection" |
|
1690 |
||
1691 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
1692 |
||
1693 |
" |
|
1694 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
1695 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 1696 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
1697 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 1698 |
" |
13 | 1699 |
! ! |
1700 |
||
260 | 1701 |
!SequenceableCollection methodsFor:'converting'! |
1702 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1703 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 1704 |
"return a collection containing the subcollections (separated by anElement) |
1705 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1706 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1707 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1708 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1709 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1710 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1711 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1712 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1713 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1714 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1715 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1716 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1717 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1718 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1719 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1720 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1721 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1722 |
1 to:numberOfCols do:[:lineNr | |
8874 | 1723 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
1724 |
stopIndex == 0 ifTrue:[ |
|
1725 |
stopIndex := self size |
|
1726 |
] ifFalse: [ |
|
1727 |
stopIndex := stopIndex - 1. |
|
1728 |
]. |
|
1729 |
||
1730 |
(stopIndex < startIndex) ifTrue: [ |
|
1731 |
cols add:(myClass new:0) |
|
1732 |
] ifFalse: [ |
|
1733 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
1734 |
]. |
|
1735 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1736 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1737 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1738 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1739 |
" |
8874 | 1740 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1741 |
'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
|
1742 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1743 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1744 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1745 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1746 |
|
9234 | 1747 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
1748 |
"evaluate aBlock for each subcollection generated by separating elements |
|
1749 |
of the receiver by anElement. |
|
1750 |
If anElement occurs multiple times in a row, |
|
1751 |
the block will be invoked with empty collections as argument. |
|
1752 |
This algorithm uses equality-compare to detect the element." |
|
1753 |
||
1754 |
|subCollection |
|
1755 |
endIndex "{ Class:SmallInteger }" |
|
1756 |
startIndex "{ Class:SmallInteger }" |
|
1757 |
stopIndex "{ Class:SmallInteger }" | |
|
1758 |
||
1759 |
startIndex := 0. |
|
1760 |
endIndex := self size. |
|
1761 |
||
1762 |
[startIndex <= endIndex] whileTrue:[ |
|
1763 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
1764 |
stopIndex == 0 ifTrue:[ |
|
1765 |
stopIndex := self size |
|
1766 |
] ifFalse: [ |
|
1767 |
stopIndex := stopIndex - 1. |
|
1768 |
]. |
|
1769 |
||
1770 |
(stopIndex < startIndex) ifTrue: [ |
|
1771 |
subCollection := self species new:0 |
|
1772 |
] ifFalse: [ |
|
1773 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
1774 |
]. |
|
1775 |
aBlock value:subCollection. |
|
1776 |
startIndex := stopIndex + 1 |
|
1777 |
]. |
|
1778 |
||
1779 |
" |
|
1780 |
'' |
|
1781 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1782 |
||
1783 |
'1 one' |
|
1784 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1785 |
||
1786 |
'1 one:2 two:3 three:4 four:5 five' |
|
1787 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1788 |
||
1789 |
'a::b' |
|
1790 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1791 |
||
1792 |
':' |
|
1793 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1794 |
||
1795 |
':a' |
|
1796 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1797 |
||
1798 |
'a:' |
|
1799 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1800 |
" |
|
1801 |
! |
|
1802 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1803 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 1804 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
1805 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1806 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1807 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1808 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1809 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1810 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1811 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1812 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1813 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1814 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1815 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1816 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1817 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1818 |
[done] whileFalse:[ |
8874 | 1819 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
1820 |
stopIndex == 0 ifTrue:[ |
|
1821 |
stopIndex := self size. |
|
1822 |
done := true. |
|
1823 |
] ifFalse: [ |
|
1824 |
stopIndex := stopIndex - 1. |
|
1825 |
]. |
|
1826 |
||
1827 |
(stopIndex < startIndex) ifTrue: [ |
|
1828 |
items add:(myClass new:0) |
|
1829 |
] ifFalse: [ |
|
1830 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
1831 |
]. |
|
1832 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1833 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1834 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1835 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1836 |
" |
8874 | 1837 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
1838 |
#(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
|
1839 |
" |
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 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 1843 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1844 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1845 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1846 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1847 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1848 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1849 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1850 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1851 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 1852 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
1853 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
1854 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
1855 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
1856 |
#(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
|
1857 |
" |
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 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 1861 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1862 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1863 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1864 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1865 |
|words |
8874 | 1866 |
start "{ Class:SmallInteger }" |
1867 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1868 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1869 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1870 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1871 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1872 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1873 |
[start <= mySize] whileTrue:[ |
8874 | 1874 |
"skip multiple separators" |
1875 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
1876 |
start := start + 1 . |
|
1877 |
start > mySize ifTrue:[ |
|
1878 |
^ words |
|
1879 |
]. |
|
1880 |
]. |
|
1881 |
||
1882 |
stop := self findFirst:aBlock startingAt:start. |
|
1883 |
stop == 0 ifTrue:[ |
|
1884 |
words add:(self copyFrom:start to:mySize). |
|
1885 |
^ words |
|
1886 |
]. |
|
1887 |
words add:(self copyFrom:start to:(stop - 1)). |
|
1888 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1889 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1890 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1891 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1892 |
" |
8874 | 1893 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
1894 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1895 |
#(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
|
1896 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1897 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1898 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1899 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1900 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1901 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1902 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1903 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1904 |
|
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 |
|
1413 | 1908 |
asStringCollection |
1909 |
"return a new string collection containing the elements; |
|
1910 |
these ought to be strings. (i.e. String or Text instances)" |
|
1911 |
||
1912 |
|newColl sz| |
|
1913 |
||
1914 |
sz := self size. |
|
9162 | 1915 |
newColl := StringCollection withSize:sz. |
1413 | 1916 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
1917 |
^ newColl |
|
1918 |
||
1919 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
1920 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
1921 |
! |
|
1922 |
||
1188 | 1923 |
asStringWith:sepChar |
8874 | 1924 |
"return a string generated by concatenating my elements |
1188 | 1925 |
(which must be strings or nil) and embedding sepChar characters in between. |
1926 |
Nil entries and empty strings are counted as empty lines." |
|
1927 |
||
8874 | 1928 |
^ self |
1929 |
from:1 to:(self size) |
|
1930 |
asStringWith:sepChar |
|
1931 |
compressTabs:false |
|
1932 |
final:nil |
|
1413 | 1933 |
|
1934 |
" |
|
1935 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
1936 |
" |
|
1937 |
||
1938 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 1939 |
! |
1940 |
||
1941 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
1942 |
"return part of myself as a string with embedded sepCharacters. |
|
1943 |
My elements must be strings or nil; nil entries and empty strings are |
|
1944 |
taken as empty lines." |
|
1945 |
||
8874 | 1946 |
^ self |
1947 |
from:firstLine to:lastLine |
|
1948 |
asStringWith:sepCharacter |
|
1949 |
compressTabs:false |
|
1950 |
final:nil |
|
1188 | 1951 |
" |
1952 |
creating entries for searchpath: |
|
1953 |
||
8874 | 1954 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
1955 |
||
1956 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 1957 |
" |
1958 |
||
1959 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
1960 |
! |
|
1961 |
||
260 | 1962 |
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
|
1963 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 1964 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 1965 |
taken as empty lines. |
1966 |
If the argument compressTabs is true, leading spaces are converted |
|
1967 |
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
|
1968 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1969 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1970 |
^ self |
8874 | 1971 |
from:firstLine to:lastLine |
1972 |
asStringWith:sepCharacter |
|
1973 |
compressTabs:compressTabs |
|
1974 |
final:endCharacter |
|
1975 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1976 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1977 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1978 |
! |
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 |
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
|
1981 |
"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
|
1982 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1983 |
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
|
1984 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
1985 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1986 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1987 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 1988 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1989 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
1990 |
any emphasis. If false, a plain string is returned. |
260 | 1991 |
This method is tuned for big collections, in not creating many |
263 | 1992 |
intermediate strings (has linear runtime). For very small collections |
1993 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 1994 |
|
5575 | 1995 |
^ self |
8874 | 1996 |
from:firstLine to:lastLine |
1997 |
asStringWith:sepCharacter |
|
1998 |
compressTabs:compressTabs |
|
1999 |
final:endCharacter |
|
2000 |
withEmphasis:withEmphasis |
|
5575 | 2001 |
! |
2002 |
||
2003 |
asStringWithCRs |
|
8874 | 2004 |
"return a string generated by concatenating my elements |
5575 | 2005 |
(which must be strings or nil) and embedding cr characters in between. |
2006 |
Nil entries and empty strings are counted as empty lines." |
|
2007 |
||
2008 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2009 |
||
2010 |
" |
|
8874 | 2011 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2012 |
|
2013 |
(OrderedCollection new |
|
8874 | 2014 |
add:'hello'; |
2015 |
add:'world'; |
|
2016 |
add:'foo'; |
|
2017 |
add:('bar' asText allBold); |
|
2018 |
yourself) asStringWithCRs |
|
5575 | 2019 |
|
2020 |
Transcript showCR: |
|
8874 | 2021 |
(OrderedCollection new |
2022 |
add:'hello'; |
|
2023 |
add:'world'; |
|
2024 |
add:'foo'; |
|
2025 |
add:('bar' asText allBold); |
|
2026 |
yourself) asStringWithCRs |
|
5575 | 2027 |
" |
2028 |
||
2029 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2030 |
! |
|
2031 |
||
2032 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2033 |
"return a string generated by concatenating some of my elements |
5575 | 2034 |
(which must be strings or nil) and embedding cr characters in between. |
2035 |
Nil entries and empty strings are counted as empty lines." |
|
2036 |
||
2037 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2038 |
||
2039 |
" |
|
8874 | 2040 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2041 |
|
2042 |
" |
|
2043 |
||
2044 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2045 |
! |
|
2046 |
||
2047 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2048 |
"return part of myself as a string with embedded cr's. |
|
2049 |
My elements must be strings or nil. |
|
2050 |
If the argument compressTabs is true, leading spaces are converted |
|
2051 |
to tab-characters (8col tabs). |
|
2052 |
Nil entries and empty strings are taken as empty lines." |
|
2053 |
||
2054 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2055 |
! |
|
2056 |
||
2057 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2058 |
"return part of myself as a string with embedded cr's. |
|
2059 |
My elements must be strings or nil; nil entries and empty strings are |
|
2060 |
taken as empty lines. |
|
2061 |
If the argument compressTabs is true, leading spaces are converted |
|
2062 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2063 |
should be followed by a cr or not." |
|
2064 |
||
2065 |
^ self asStringWith:(Character cr) |
|
8874 | 2066 |
from:firstLine |
2067 |
to:lastLine |
|
2068 |
compressTabs:compressTabs |
|
2069 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2070 |
! |
2071 |
||
2072 |
decodeAsLiteralArray |
|
2073 |
"given a literalEncoding in the receiver, |
|
2074 |
create & return the corresponding object. |
|
2075 |
The inverse operation to #literalArrayEncoding." |
|
2076 |
||
6724
e5b2bac20f37
load owner of an unknown private class in decodeLiteralArray
Claus Gittinger <cg@exept.de>
parents:
6593
diff
changeset
|
2077 |
|clsName cls loadOwnerOf| |
5575 | 2078 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2079 |
self size == 0 ifTrue:[ |
9358 | 2080 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2081 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2082 |
|
5575 | 2083 |
clsName := self first. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2084 |
clsName isSymbol ifFalse:[ |
9358 | 2085 |
^ MissingClassInLiteralArrayErrorSignal |
2086 |
raiseRequestWith:clsName |
|
2087 |
errorString:('invalid className in literalArray-spec: ', clsName printString) |
|
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2088 |
]. |
5575 | 2089 |
cls := Smalltalk at:clsName ifAbsent:nil. |
2090 |
cls isNil ifTrue:[ |
|
9358 | 2091 |
"/ is it a private class of an autoloaded class ? |
2092 |
loadOwnerOf := [:clsName | |
|
2093 |
|t ownerName owner| |
|
2094 |
||
2095 |
t := clsName. |
|
2096 |
[(t indexOf:$:) ~~ 0] whileTrue:[ |
|
2097 |
t := t copyTo:(t lastIndexOf:$:). |
|
2098 |
(t endsWith:$:) ifTrue:[t:= t copyWithoutLast:1]. |
|
2099 |
(t endsWith:$:) ifTrue:[t:= t copyWithoutLast:1]. |
|
2100 |
ownerName := t. |
|
2101 |
owner := Smalltalk at:ownerName asSymbol ifAbsent:nil. |
|
2102 |
owner isNil ifTrue:[ |
|
2103 |
loadOwnerOf value:ownerName. |
|
2104 |
owner := Smalltalk at:ownerName asSymbol ifAbsent:nil. |
|
2105 |
]. |
|
2106 |
owner notNil ifTrue:[ |
|
2107 |
owner autoload |
|
2108 |
]. |
|
2109 |
]. |
|
2110 |
]. |
|
2111 |
loadOwnerOf value:clsName. |
|
2112 |
cls := Smalltalk at:clsName ifAbsent:nil. |
|
2113 |
cls isNil ifTrue:[ |
|
2114 |
^ MissingClassInLiteralArrayErrorSignal |
|
2115 |
raiseRequestWith:clsName |
|
2116 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
2117 |
]. |
|
5575 | 2118 |
]. |
2119 |
^ cls decodeFromLiteralArray:self. |
|
2120 |
||
2121 |
" |
|
8874 | 2122 |
#(Point 10 20) decodeAsLiteralArray |
2123 |
#(Rectangle 100 200 400 500) decodeAsLiteralArray |
|
2124 |
#(#MenuItem #rawLabel: 'right' #value: #right) decodeAsLiteralArray |
|
5575 | 2125 |
" |
2126 |
||
2127 |
"Modified: / 18.5.1999 / 14:51:58 / cg" |
|
2128 |
! |
|
2129 |
||
8874 | 2130 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2131 |
"return part of myself as a string with embedded sepCharacters. |
2132 |
My elements must be strings or nil; nil entries and empty strings are |
|
2133 |
taken as empty lines." |
|
2134 |
||
8874 | 2135 |
^ self |
2136 |
from:firstLine |
|
2137 |
to:lastLine |
|
2138 |
asStringWith:sepCharacter |
|
2139 |
compressTabs:false |
|
2140 |
final:nil |
|
5575 | 2141 |
" |
2142 |
creating entries for searchpath: |
|
2143 |
||
8874 | 2144 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2145 |
||
2146 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2147 |
||
2148 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2149 |
" |
2150 |
||
2151 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2152 |
! |
|
2153 |
||
2154 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2155 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2156 |
My elements must be strings or nil; nil entries and empty strings are |
|
2157 |
taken as empty lines. |
|
2158 |
If the argument compressTabs is true, leading spaces are converted |
|
2159 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2160 |
character (if non-nil)." |
|
2161 |
||
2162 |
^ self |
|
8874 | 2163 |
from:firstLine |
2164 |
to:lastLine |
|
2165 |
asStringWith:sepCharacter |
|
2166 |
compressTabs:compressTabs |
|
2167 |
final:endCharacter |
|
2168 |
withEmphasis:true |
|
5575 | 2169 |
|
2170 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2171 |
! |
|
2172 |
||
2173 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2174 |
"return part of myself as a string or text with embedded sepCharacters |
|
2175 |
and followup endCharacter. |
|
2176 |
My elements must be strings or nil; nil entries and empty strings are |
|
2177 |
taken as empty lines. |
|
2178 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2179 |
If the argument compressTabs is true, leading spaces are converted |
|
2180 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2181 |
character (if non-nil). |
|
2182 |
The withEmphais argument controls if the returned string should preserve |
|
2183 |
any emphasis. If false, a plain string is returned. |
|
2184 |
This method is tuned for big collections, in not creating many |
|
2185 |
intermediate strings (has linear runtime). For very small collections |
|
2186 |
and small strings, it may be faster to use the comma , operation." |
|
2187 |
||
293 | 2188 |
|idx1 "{ Class:SmallInteger }" |
2189 |
idx2 "{ Class:SmallInteger }" |
|
2190 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2191 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2192 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2193 |
newString lineString spaces idx nTabs |
8099 | 2194 |
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
|
2195 |
thisLen anyTab| |
260 | 2196 |
|
2197 |
" |
|
2198 |
first accumulate the size of the string, to avoid |
|
2199 |
countless reallocations. If tabs are compressed, |
|
2200 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2201 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2202 |
" |
8099 | 2203 |
needEmphasis := false. |
2204 |
maxBitsPerCharacter := 8. |
|
1015 | 2205 |
|
260 | 2206 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2207 |
sepCharacter isNil ifTrue:[ |
8874 | 2208 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2209 |
] ifFalse:[ |
8874 | 2210 |
sepCharacter isCharacter ifTrue:[ |
2211 |
sepCnt := 1 |
|
2212 |
] ifFalse:[ |
|
2213 |
sepCnt := sepCharacter size |
|
2214 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2215 |
]. |
260 | 2216 |
|
293 | 2217 |
idx1 := firstLine. |
2218 |
idx2 := lastLine. |
|
2219 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2220 |
lineString := self at:lineIndex. |
2221 |
||
2222 |
lineString notNil ifTrue:[ |
|
2223 |
withEmphasis ifTrue:[ |
|
2224 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2225 |
needEmphasis := true |
|
2226 |
]. |
|
2227 |
]. |
|
2228 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2229 |
totalLength := totalLength + lineString size |
|
2230 |
]. |
|
2231 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2232 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2233 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2234 |
|
8099 | 2235 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2236 |
maxBitsPerCharacter > 16 ifTrue:[ |
2237 |
stringClass := Unicode32String. |
|
2238 |
] ifFalse:[ |
|
2239 |
stringClass := Unicode16String. |
|
2240 |
] |
|
8099 | 2241 |
] ifFalse:[ |
8874 | 2242 |
stringClass := String. |
8099 | 2243 |
]. |
2244 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2245 |
endCharacter notNil ifTrue:[ |
8874 | 2246 |
endCharacter isCharacter ifTrue:[ |
2247 |
totalLength := totalLength + 1 |
|
2248 |
] ifFalse:[ |
|
2249 |
totalLength := totalLength + endCharacter size |
|
2250 |
]. |
|
260 | 2251 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2252 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2253 |
|
260 | 2254 |
spaces := ' '. |
1016 | 2255 |
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
|
2256 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2257 |
needEmphasis ifTrue:[ |
8874 | 2258 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2259 |
]. |
260 | 2260 |
|
2261 |
" |
|
2262 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2263 |
Be careful with runArrays: |
8874 | 2264 |
replacing individual elements is VERY expensive. |
2265 |
Therefore, create a new runArray from scratch. |
|
260 | 2266 |
" |
2267 |
pos := 1. |
|
293 | 2268 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2269 |
lineString := self at:lineIndex. |
2270 |
thisLen := lineString size. |
|
2271 |
thisLen ~~ 0 ifTrue:[ |
|
2272 |
withEmphasis ifFalse:[ |
|
2273 |
lineString := lineString string. |
|
2274 |
]. |
|
2275 |
||
2276 |
(anyTab := compressTabs) ifTrue:[ |
|
2277 |
" |
|
2278 |
mhmh: could use withTabs from String-class here, |
|
2279 |
but we should avoid creating too many temporary strings |
|
2280 |
(especially, since this method is typically used when converting |
|
2281 |
big texts such as when saving in the filebrowser ...). |
|
2282 |
Therefore, we convert tabs inline here doing a direct replace |
|
2283 |
in newString." |
|
2284 |
||
2285 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2286 |
nTabs := (idx-1) // 8. |
|
2287 |
anyTab := (nTabs > 0) |
|
2288 |
]. |
|
2289 |
anyTab ifTrue:[ |
|
2290 |
"any tabs" |
|
2291 |
||
2292 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2293 |
||
2294 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2295 |
newRuns notNil ifTrue:[ |
|
2296 |
newRuns add:nil withOccurrences:nTabs |
|
2297 |
]. |
|
2298 |
pos := pos + nTabs. |
|
2299 |
||
2300 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2301 |
newRuns notNil ifTrue:[ |
|
2302 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2303 |
idx to:lineString size do:[:pos | |
|
2304 |
newRuns add:(lineString emphasisAt:pos) |
|
2305 |
] |
|
2306 |
] ifFalse:[ |
|
2307 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2308 |
] |
|
2309 |
]. |
|
2310 |
pos := pos + thisLen - (nTabs * 8). |
|
2311 |
] ifFalse:[ |
|
2312 |
newString replaceFrom:pos with:lineString. |
|
2313 |
||
2314 |
newRuns notNil ifTrue:[ |
|
2315 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2316 |
newRuns addAll:(lineString emphasis) |
|
2317 |
] ifFalse:[ |
|
2318 |
newRuns add:nil withOccurrences:lineString size |
|
2319 |
] |
|
2320 |
]. |
|
2321 |
pos := pos + thisLen. |
|
2322 |
]. |
|
2323 |
]. |
|
2324 |
||
2325 |
lineIndex ~~ lastLine ifTrue:[ |
|
2326 |
c := sepCharacter |
|
2327 |
] ifFalse:[ |
|
2328 |
c := endCharacter |
|
2329 |
]. |
|
2330 |
||
2331 |
c notNil ifTrue:[ |
|
2332 |
c isCharacter ifTrue:[ |
|
2333 |
newString at:pos put:c. |
|
2334 |
newRuns notNil ifTrue:[ |
|
2335 |
newRuns add:nil. |
|
2336 |
]. |
|
2337 |
pos := pos + 1 |
|
2338 |
] ifFalse:[ |
|
2339 |
newString replaceFrom:pos with:c. |
|
2340 |
newRuns notNil ifTrue:[ |
|
2341 |
newRuns add:nil withOccurrences:c size |
|
2342 |
]. |
|
2343 |
pos := pos + c size |
|
2344 |
] |
|
2345 |
]. |
|
260 | 2346 |
]. |
2347 |
||
2348 |
" |
|
2349 |
in case of tab compression, the result has to be |
|
2350 |
cut to size ... sorry |
|
2351 |
" |
|
2352 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2353 |
newString := newString copyTo:(pos - 1) |
260 | 2354 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2355 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2356 |
newRuns notNil ifTrue:[ |
8874 | 2357 |
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
|
2358 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2359 |
|
260 | 2360 |
^ newString |
2361 |
||
2362 |
" |
|
2363 |
creating entries for searchpath: |
|
2364 |
||
8874 | 2365 |
#('foo' 'bar' 'baz' '/foo/bar') |
2366 |
asStringWith:$: |
|
2367 |
from:1 to:4 |
|
2368 |
compressTabs:false |
|
2369 |
final:nil |
|
260 | 2370 |
|
2371 |
with trailing colon: |
|
2372 |
||
8874 | 2373 |
#('foo' 'bar' 'baz' '/foo/bar') |
2374 |
asStringWith:$: |
|
2375 |
from:1 to:4 |
|
2376 |
compressTabs:false |
|
2377 |
final:$: |
|
260 | 2378 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2379 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2380 |
|
8874 | 2381 |
#('foo' 'bar' 'baz') |
2382 |
asStringWith:nil |
|
2383 |
from:1 to:3 |
|
2384 |
compressTabs:false |
|
2385 |
final:nil |
|
260 | 2386 |
|
2387 |
creating a string from a collection of lines: |
|
2388 |
||
8874 | 2389 |
#('foo' 'bar' 'baz') |
2390 |
asStringWith:(Character cr) |
|
2391 |
from:1 to:3 |
|
2392 |
compressTabs:false |
|
2393 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2394 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2395 |
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
|
2396 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2397 |
(Array |
8874 | 2398 |
with:'foo' |
2399 |
with:('bar' asText allBold) |
|
2400 |
with:'baz' |
|
2401 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2402 |
) |
|
2403 |
asStringWith:(Character cr) |
|
2404 |
from:1 to:4 |
|
2405 |
compressTabs:false |
|
2406 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2407 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2408 |
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
|
2409 |
|
8874 | 2410 |
#('foo' 'bar' 'baz') |
2411 |
asStringWith:'__' |
|
2412 |
from:1 to:3 |
|
2413 |
compressTabs:false |
|
2414 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2415 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2416 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2417 |
|
8874 | 2418 |
#('foo' 'bar' 'baz') |
2419 |
asStringWith:'__' |
|
2420 |
from:1 to:3 |
|
2421 |
compressTabs:false |
|
2422 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2423 |
|
260 | 2424 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2425 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2426 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2427 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
379 | 2428 |
! |
2429 |
||
422 | 2430 |
literalArrayEncoding |
1245 | 2431 |
"encode myself as an array literal, from which a copy of the receiver |
2432 |
can be reconstructed with #decodeAsLiteralArray." |
|
2433 |
||
7598
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2434 |
|encoding idx| |
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2435 |
|
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2436 |
encoding := Array new:self size + 1. |
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2437 |
encoding at:1 put:self class name. |
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2438 |
idx := 2. |
8874 | 2439 |
self do:[:element| |
2440 |
encoding at:idx put:element literalArrayEncoding. |
|
2441 |
idx := idx + 1. |
|
7598
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2442 |
]. |
27f37bd56675
Speed aup #literalArrayEncoding
Stefan Vogel <sv@exept.de>
parents:
7537
diff
changeset
|
2443 |
^ encoding |
4807 | 2444 |
|
422 | 2445 |
|
2446 |
" |
|
2447 |
(Array with:(Color red:50 green:50 blue:50) |
|
8874 | 2448 |
with:(1 @ 2) |
2449 |
) literalArrayEncoding decodeAsLiteralArray |
|
422 | 2450 |
" |
1245 | 2451 |
|
2452 |
"Modified: 22.4.1996 / 13:00:56 / cg" |
|
260 | 2453 |
! ! |
2454 |
||
7253 | 2455 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2456 |
||
8874 | 2457 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2458 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2459 |
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
|
2460 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2461 |
however, physically, no copy is made). |
9291 | 2462 |
Warning: |
2463 |
The slice SHARES the memory for the element-data with the original, |
|
2464 |
this means that any modifications in the original are visible in the slice |
|
2465 |
and vice versa." |
|
7253 | 2466 |
|
2467 |
^ self |
|
9291 | 2468 |
from:startIndex |
2469 |
to:self size |
|
2470 |
by:1 |
|
8874 | 2471 |
|
2472 |
" |
|
2473 |
#(1 2 3 4 5 6 7) from:3 |
|
2474 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2475 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2476 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2477 |
" |
|
2478 |
! |
|
2479 |
||
2480 |
from:startIndex by:step |
|
9291 | 2481 |
"Create a ReindexedCollection from the receiver. |
2482 |
The new collection represents the elements starting at startIndex to the end. |
|
2483 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2484 |
however, physically, no copy is made). |
|
2485 |
Warning: |
|
2486 |
The slice SHARES the memory for the element-data with the original, |
|
2487 |
this means that any modifications in the original are visible in the slice |
|
2488 |
and vice versa." |
|
7253 | 2489 |
|
2490 |
^ self |
|
9291 | 2491 |
from:startIndex |
2492 |
to:(step > 1 |
|
2493 |
ifTrue: [self size] |
|
2494 |
ifFalse: [1]) |
|
2495 |
by:step |
|
8874 | 2496 |
|
2497 |
" |
|
2498 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2499 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2500 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2501 |
( #(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
|
2502 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2503 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2504 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2505 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2506 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2507 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2508 |
Warning: |
2509 |
The slice SHARES the memory for the element-data with the original, |
|
2510 |
this means that any modifications in the original are visible in the slice |
|
2511 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2512 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2513 |
^ self |
9291 | 2514 |
from:startIndex |
2515 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2516 |
|
2517 |
" |
|
2518 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2519 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2520 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2521 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2522 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2523 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2524 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2525 |
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
|
2526 |
slice := coll from:3 to:7. |
8874 | 2527 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2528 |
coll at:4 put:40. |
8874 | 2529 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2530 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2531 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2532 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2533 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2534 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2535 |
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
|
2536 |
slice := coll from:3 to:7. |
8874 | 2537 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2538 |
slice at:1 put:40. |
8874 | 2539 |
slice. |
2540 |
" |
|
2541 |
! |
|
2542 |
||
2543 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2544 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2545 |
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
|
2546 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2547 |
however, physically, no copy is made). |
9291 | 2548 |
Warning: |
2549 |
The slice SHARES the memory for the element-data with the original, |
|
2550 |
this means that any modifications in the original are visible in the slice |
|
2551 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2552 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2553 |
^ self |
9291 | 2554 |
from:startIndex |
2555 |
to:endIndex |
|
2556 |
by: (startIndex <= endIndex |
|
2557 |
ifTrue: [1] |
|
2558 |
ifFalse: [-1]) |
|
8874 | 2559 |
|
2560 |
" |
|
2561 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2562 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2563 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2564 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2565 |
" |
|
2566 |
! |
|
2567 |
||
2568 |
from:startIndex 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 |
^ ReindexedCollection |
|
9291 | 2579 |
on:self |
2580 |
from:startIndex |
|
2581 |
to:endIndex |
|
2582 |
by:step |
|
8874 | 2583 |
|
2584 |
" |
|
2585 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2586 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2587 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2588 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2589 |
" |
|
2590 |
! |
|
2591 |
||
2592 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2593 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2594 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2595 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2596 |
however, physically, no copy is made). |
9291 | 2597 |
Warning: |
2598 |
The slice SHARES the memory for the element-data with the original, |
|
2599 |
this means that any modifications in the original are visible in the slice |
|
2600 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2601 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2602 |
^ self |
9291 | 2603 |
from:1 |
2604 |
to:endIndex |
|
2605 |
by:1 |
|
8874 | 2606 |
|
2607 |
" |
|
2608 |
#(1 2 3 4 5 6 7) to:4 |
|
2609 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2610 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2611 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2612 |
" |
|
2613 |
! |
|
2614 |
||
2615 |
to:endIndex by:step |
|
9291 | 2616 |
"Create a ReindexedCollection from the receiver. |
2617 |
The new collection represents the receivers elements up to endIndex. |
|
2618 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2619 |
however, physically, no copy is made). |
|
2620 |
Warning: |
|
2621 |
The slice SHARES the memory for the element-data with the original, |
|
2622 |
this means that any modifications in the original are visible in the slice |
|
2623 |
and vice versa." |
|
7253 | 2624 |
|
2625 |
^ self |
|
9291 | 2626 |
from:(step > 0 |
2627 |
ifTrue: [1] |
|
2628 |
ifFalse: [self size]) |
|
2629 |
to:endIndex |
|
2630 |
by:step |
|
8874 | 2631 |
|
2632 |
" |
|
2633 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2634 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2635 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2636 |
( #(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
|
2637 |
" |
7253 | 2638 |
! ! |
2639 |
||
1 | 2640 |
!SequenceableCollection methodsFor:'copying'! |
2641 |
||
9106 | 2642 |
++ aCollection |
2643 |
"return a new collection formed from concatenating the receiver with |
|
2644 |
the argument. The class of the new collection is determined by the |
|
2645 |
receivers class, so mixing classes is possible, if the second collections |
|
2646 |
elements can be stored into instances of the receivers class." |
|
2647 |
||
2648 |
^ self , aCollection |
|
2649 |
||
2650 |
" |
|
2651 |
#($a $b $c) ++ #(1 2 3) |
|
2652 |
#($a $b $c) ++ '123' |
|
2653 |
'abc' ++ '123' |
|
2654 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2655 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2656 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2657 |
" |
|
2658 |
! |
|
2659 |
||
1 | 2660 |
, aCollection |
2661 |
"return a new collection formed from concatenating the receiver with |
|
44 | 2662 |
the argument. The class of the new collection is determined by the |
2663 |
receivers class, so mixing classes is possible, if the second collections |
|
2664 |
elements can be stored into instances of the receivers class." |
|
1 | 2665 |
|
8874 | 2666 |
|newCollection |
1 | 2667 |
mySize "{ Class: SmallInteger }" |
2668 |
newSize "{ Class: SmallInteger }" |
|
2669 |
otherSize "{ Class: SmallInteger }" |
|
2670 |
dstIndex "{ Class: SmallInteger }"| |
|
2671 |
||
2672 |
mySize := self size. |
|
2673 |
otherSize := aCollection size. |
|
2674 |
newSize := mySize + otherSize. |
|
202 | 2675 |
|
252 | 2676 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 2677 |
|
2678 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
2679 |
dstIndex := mySize + 1. |
|
202 | 2680 |
aCollection isSequenceable ifTrue:[ |
8874 | 2681 |
" |
2682 |
yes, aCollection has indexed elements |
|
2683 |
" |
|
2684 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 2685 |
] ifFalse:[ |
8874 | 2686 |
" |
2687 |
no, enumerate aCollection |
|
2688 |
" |
|
2689 |
aCollection do:[:element | |
|
2690 |
newCollection at:dstIndex put:element. |
|
2691 |
dstIndex := dstIndex + 1 |
|
2692 |
] |
|
1 | 2693 |
]. |
2694 |
^ newCollection |
|
44 | 2695 |
|
95 | 2696 |
" |
2697 |
#($a $b $c) , #(1 2 3) |
|
2698 |
#($a $b $c) , '123' |
|
2699 |
'abc' , '123' |
|
2700 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
2701 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2702 |
'abc' asArray , #(1 2 3 4 5) |
|
2703 |
" |
|
1 | 2704 |
! |
2705 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2706 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2707 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2708 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2709 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2710 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2711 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2712 |
" |
8874 | 2713 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2714 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2715 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2716 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2717 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2718 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2719 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2720 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2721 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2722 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2723 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2724 |
" |
8874 | 2725 |
#($a $b $c $d $e $f $g) copyButLast:2 |
2726 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2727 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2728 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2729 |
|
602 | 2730 |
copyFirst:count |
2731 |
"return a new collection consisting of the receivers first count |
|
2732 |
elements - this is just a rename of copyTo: - for compatibility." |
|
2733 |
||
2734 |
^ self copyFrom:1 to:count |
|
2735 |
||
2736 |
" |
|
2737 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
2738 |
'1234567890' copyFirst:4 |
|
2739 |
" |
|
2740 |
! |
|
2741 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2742 |
copyFrom:startIndex |
9234 | 2743 |
"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
|
2744 |
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
|
2745 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2746 |
^ self copyFrom:startIndex to:(self size) |
602 | 2747 |
|
2748 |
" |
|
2749 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
2750 |
'1234567890' copyFrom:2 |
|
8874 | 2751 |
(10 to:19) copyFrom:5 |
602 | 2752 |
" |
2753 |
! |
|
2754 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2755 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2756 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2757 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2758 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2759 |
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
|
2760 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2761 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2762 |
^ self |
8874 | 2763 |
copyFrom:startIndex |
2764 |
to:(startIndex + numberOfElements - 1) |
|
2765 |
||
2766 |
" |
|
2767 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
2768 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2769 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2770 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2771 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2772 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2773 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2774 |
copyFrom:startIndex to:stopIndex |
602 | 2775 |
"return a new collection consisting of receivers elements |
4403 | 2776 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2777 |
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
|
2778 |
Raise an error, if stopIndex is out of range." |
602 | 2779 |
|
2780 |
|newCollection newSize| |
|
2781 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2782 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2783 |
newSize < 0 ifTrue:[ |
8874 | 2784 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2785 |
]. |
602 | 2786 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2787 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 2788 |
^ newCollection |
2789 |
||
2790 |
" |
|
2791 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
2792 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 2793 |
(1 to:10) copyFrom:2 to:5 |
2794 |
||
2795 |
'1234567890' copyFrom:2 to:15 |
|
2796 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 2797 |
" |
2798 |
! |
|
2799 |
||
2800 |
copyLast:count |
|
2801 |
"return a new collection consisting of the receivers last count |
|
2802 |
elements." |
|
2803 |
||
2804 |
|sz| |
|
2805 |
||
2806 |
sz := self size. |
|
2807 |
^ self copyFrom:(sz - count + 1) to:sz |
|
2808 |
||
2809 |
" |
|
2810 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
2811 |
'1234567890' copyLast:4 |
|
2812 |
" |
|
2813 |
! |
|
2814 |
||
3653 | 2815 |
copyReplaceAll:oldElement with:newElement |
2816 |
"return a copy of the receiver, where all elements equal to oldElement |
|
2817 |
have been replaced by newElement." |
|
2818 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2819 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 2820 |
^ self copy replaceAll:oldElement with:newElement |
2821 |
||
2822 |
" |
|
8874 | 2823 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
2824 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 2825 |
" |
2826 |
||
2827 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
2828 |
! |
|
2829 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2830 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2831 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2832 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2833 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2834 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2835 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2836 |
|
5489 | 2837 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2838 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2839 |
"/ '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
|
2840 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2841 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2842 |
self do:[:el | |
8874 | 2843 |
el = oldObject ifTrue:[ |
2844 |
aCollection do:[:el2 | |
|
2845 |
s nextPut:el2 |
|
2846 |
] |
|
2847 |
] ifFalse:[ |
|
2848 |
s nextPut:el |
|
2849 |
] |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2850 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2851 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2852 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2853 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2854 |
args: oldObject : <object> |
8874 | 2855 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2856 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2857 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2858 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2859 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2860 |
" |
8874 | 2861 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
2862 |
#(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
|
2863 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2864 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2865 |
|
602 | 2866 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
2867 |
"return a copy of the receiver, where the elements from startIndex to |
|
2868 |
endIndex have been replaced by the elements of aCollection" |
|
2869 |
||
2870 |
|newColl sz replSize| |
|
2871 |
||
2872 |
replSize := aCollection size. |
|
2873 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
2874 |
newColl := self copyEmptyAndGrow:sz. |
|
2875 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
2876 |
newColl replaceFrom:startIndex with:aCollection. |
|
2877 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
2878 |
^ newColl |
|
2879 |
||
2880 |
" |
|
2881 |
#(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 | 2882 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 2883 |
" |
2884 |
! |
|
2885 |
||
2886 |
copyReplaceFrom:startIndex with:aCollection |
|
2887 |
"return a copy of the receiver, where the elements from startIndex to |
|
2888 |
the end have been replaced by the elements of aCollection" |
|
2889 |
||
2890 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
2891 |
||
2892 |
" |
|
8874 | 2893 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
2894 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 2895 |
" |
2896 |
! |
|
2897 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2898 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2899 |
"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
|
2900 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2901 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2902 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2903 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2904 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2905 |
" |
8874 | 2906 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
2907 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2908 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2909 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2910 |
|
602 | 2911 |
copyThrough:element |
2912 |
"return a new collection consisting of the receiver elements |
|
8874 | 2913 |
up-to (AND including) the first occurrence of element." |
602 | 2914 |
|
2915 |
|idx| |
|
2916 |
||
2917 |
idx := self indexOf:element. |
|
2918 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
2919 |
^ self copyFrom:1 to:idx |
|
2920 |
||
2921 |
" |
|
2922 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
2923 |
'1234567890' copyThrough:$5 |
|
2924 |
'1234567890' copyThrough:$a |
|
2925 |
'1234567890' copyThrough:$1 |
|
2926 |
" |
|
2927 |
! |
|
2928 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2929 |
copyTo:stopIndex |
9234 | 2930 |
"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
|
2931 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2932 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2933 |
^ self copyFrom:1 to:stopIndex |
602 | 2934 |
|
2935 |
" |
|
2936 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
2937 |
'1234567890' copyTo:4 |
|
2938 |
" |
|
2939 |
! |
|
2940 |
||
5526 | 2941 |
copyToMax:stop |
2942 |
"return a new collection consisting of receivers elements |
|
8874 | 2943 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 2944 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
2945 |
||
2946 |
^ self copyFrom:1 to:(self size min:stop) |
|
2947 |
||
2948 |
" |
|
2949 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
2950 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
2951 |
" |
|
2952 |
! |
|
2953 |
||
602 | 2954 |
copyUpTo:element |
2955 |
"return a new collection consisting of the receiver elements |
|
8874 | 2956 |
up-to (but excluding) the first occurrence of element; |
602 | 2957 |
or to the end, if element is not included" |
2958 |
||
2959 |
|idx| |
|
2960 |
||
2961 |
idx := self indexOf:element. |
|
2962 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
2963 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
2964 |
^ self copyFrom:1 to:(idx-1) |
|
2965 |
||
2966 |
" |
|
2967 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
2968 |
'1234567890' copyUpTo:$5 |
|
2969 |
'1234567890' copyUpTo:$a |
|
2970 |
'1234567890' copyUpTo:$1 |
|
2971 |
'123456789' copyUpTo:$0 |
|
2972 |
" |
|
2973 |
! |
|
2974 |
||
1 | 2975 |
copyWith:newElement |
233 | 2976 |
"return a new collection containing the receivers elements |
8874 | 2977 |
and the single new element, newElement. |
233 | 2978 |
This is different from concatentation, which expects another collection |
2979 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 2980 |
|
2981 |
|newCollection mySize newSize| |
|
2982 |
||
2983 |
mySize := self size. |
|
2984 |
newSize := mySize + 1. |
|
252 | 2985 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 2986 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
2987 |
newCollection at:newSize put:newElement. |
|
2988 |
^newCollection |
|
44 | 2989 |
|
95 | 2990 |
" |
2991 |
#(1 2 3 4 5) copyWith:$a |
|
2992 |
'abcdefg' copyWith:$h |
|
2993 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
2994 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
2995 |
" |
|
1 | 2996 |
! |
2997 |
||
61 | 2998 |
copyWithout:elementToSkip |
2999 |
"return a new collection consisting of a copy of the receiver, with |
|
3000 |
ALL elements equal to elementToSkip are left out. |
|
3001 |
No error is reported, if elementToSkip is not in the collection." |
|
3002 |
||
293 | 3003 |
|n "{ Class: SmallInteger }" |
3004 |
sz "{ Class: SmallInteger }" |
|
3005 |
srcIndex "{ Class: SmallInteger }" |
|
3006 |
dstIndex "{ Class: SmallInteger }" |
|
3007 |
skipIndex "{ Class: SmallInteger }" |
|
3008 |
copy l| |
|
61 | 3009 |
|
8874 | 3010 |
"the code below may look like overkill, |
61 | 3011 |
however, for big collections its better to move data |
3012 |
around in big chunks" |
|
3013 |
||
3014 |
n := self occurrencesOf:elementToSkip. |
|
3015 |
n == 0 ifTrue:[^ self copy]. |
|
3016 |
||
3017 |
sz := self size. |
|
252 | 3018 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3019 |
|
3020 |
srcIndex := 1. |
|
3021 |
dstIndex := 1. |
|
3022 |
||
3023 |
n timesRepeat:[ |
|
8874 | 3024 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3025 |
l := skipIndex - srcIndex. |
|
3026 |
l ~~ 0 ifTrue:[ |
|
3027 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3028 |
with:self startingAt:srcIndex. |
|
3029 |
dstIndex := dstIndex + l |
|
3030 |
]. |
|
3031 |
srcIndex := skipIndex + 1 |
|
61 | 3032 |
]. |
3033 |
l := sz - srcIndex. |
|
3034 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3035 |
with:self startingAt:srcIndex. |
61 | 3036 |
^ copy |
3037 |
||
95 | 3038 |
" |
3039 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3040 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3041 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3042 |
'abcdefghi' copyWithout:$h |
3043 |
'abcdefg' copyWithout:$h |
|
95 | 3044 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3045 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3046 |
#(90 80 70 60 50) copyWithout:70 |
|
3047 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3048 |
" |
|
61 | 3049 |
! |
3050 |
||
602 | 3051 |
copyWithoutFirst:elementToSkip |
3052 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3053 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3054 |
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
|
3055 |
Do not confuse this with copyButFirst:" |
602 | 3056 |
|
3057 |
|copy skipIndex sz| |
|
3058 |
||
3059 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3060 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3061 |
||
3062 |
sz := self size - 1. |
|
3063 |
copy := self copyEmptyAndGrow:sz. |
|
3064 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3065 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3066 |
^ copy |
|
3067 |
||
3068 |
" |
|
3069 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3070 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3071 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3072 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3073 |
" |
|
3074 |
! |
|
3075 |
||
8673 | 3076 |
copyWithoutIdentical:elementToSkip |
3077 |
"return a new collection consisting of a copy of the receiver, with |
|
3078 |
ALL elements identical to elementToSkip are left out. |
|
3079 |
No error is reported, if elementToSkip is not in the collection." |
|
3080 |
||
3081 |
|n "{ Class: SmallInteger }" |
|
3082 |
sz "{ Class: SmallInteger }" |
|
3083 |
srcIndex "{ Class: SmallInteger }" |
|
3084 |
dstIndex "{ Class: SmallInteger }" |
|
3085 |
skipIndex "{ Class: SmallInteger }" |
|
3086 |
copy l| |
|
3087 |
||
8874 | 3088 |
"the code below may look like overkill, |
8673 | 3089 |
however, for big collections its better to move data |
3090 |
around in big chunks" |
|
3091 |
||
3092 |
n := self count:[:el | el == elementToSkip]. |
|
3093 |
n == 0 ifTrue:[^ self copy]. |
|
3094 |
||
3095 |
sz := self size. |
|
3096 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3097 |
||
3098 |
srcIndex := 1. |
|
3099 |
dstIndex := 1. |
|
3100 |
||
3101 |
n timesRepeat:[ |
|
8874 | 3102 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3103 |
l := skipIndex - srcIndex. |
|
3104 |
l ~~ 0 ifTrue:[ |
|
3105 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3106 |
with:self startingAt:srcIndex. |
|
3107 |
dstIndex := dstIndex + l |
|
3108 |
]. |
|
3109 |
srcIndex := skipIndex + 1 |
|
8673 | 3110 |
]. |
3111 |
l := sz - srcIndex. |
|
3112 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3113 |
with:self startingAt:srcIndex. |
8673 | 3114 |
^ copy |
3115 |
||
3116 |
" |
|
8874 | 3117 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3118 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3119 |
" |
3120 |
! |
|
3121 |
||
61 | 3122 |
copyWithoutIndex:omitIndex |
3123 |
"return a new collection consisting of receivers elements |
|
3124 |
without the argument stored at omitIndex" |
|
3125 |
||
3126 |
|copy sz| |
|
3127 |
||
202 | 3128 |
sz := self size - 1. |
252 | 3129 |
copy := self copyEmptyAndGrow:sz. |
293 | 3130 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3131 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3132 |
^ copy |
3133 |
||
95 | 3134 |
" |
3135 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3136 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3137 |
" |
|
1 | 3138 |
! |
3139 |
||
202 | 3140 |
copyWithoutLast:count |
3141 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3142 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3143 |
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
|
3144 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3145 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3146 |
|
9250 | 3147 |
<resource:#obsolete> |
3148 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3149 |
^ self copyTo:(self size - count) |
202 | 3150 |
|
3151 |
" |
|
8874 | 3152 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3153 |
'1234567890' copyWithoutLast:4 |
|
202 | 3154 |
" |
5231 | 3155 |
! |
3156 |
||
3157 |
restAfter:anElement |
|
8366 | 3158 |
"return a new collection consisting of the receivers elements after |
5231 | 3159 |
(but excluding) anElement. |
8366 | 3160 |
If anElement is not in the receiver, the returned collection |
3161 |
will be empty. |
|
3162 |
See also #upTo:." |
|
5231 | 3163 |
|
3164 |
|pos| |
|
3165 |
||
3166 |
pos := self indexOf:anElement. |
|
8366 | 3167 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3168 |
||
5231 | 3169 |
^ self copyFrom:(pos + 1) |
3170 |
||
3171 |
" |
|
8874 | 3172 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3173 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3174 |
'hello world' upTo:Character space |
|
3175 |
'hello world' restAfter:Character space |
|
3176 |
'1234.5678' upTo:$. |
|
3177 |
'1234.5678' restAfter:$. |
|
8366 | 3178 |
" |
5231 | 3179 |
! |
3180 |
||
3181 |
upTo:anElement |
|
3182 |
"return a new collection consisting of the receivers elements upTo |
|
3183 |
(but excluding) anElement. |
|
3184 |
If anElement is not in the receiver, the returned collection |
|
3185 |
will consist of all elements of the receiver. |
|
9307 | 3186 |
See also #restAfter: , #copyFrom:index." |
5231 | 3187 |
|
3188 |
|pos| |
|
3189 |
||
3190 |
pos := self indexOf:anElement. |
|
3191 |
pos == 0 ifTrue:[^ self copy]. |
|
3192 |
||
3193 |
^ self copyFrom:1 to:(pos - 1) |
|
3194 |
||
3195 |
" |
|
8874 | 3196 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3197 |
'hello world' upTo:Character space |
|
3198 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3199 |
'1234.5678' upTo:$. |
|
3200 |
'1234' upTo:$. |
|
5231 | 3201 |
|
3202 |
raises an error: |
|
3203 |
||
8874 | 3204 |
(Dictionary new |
9307 | 3205 |
at:#foo put:'foo'; |
3206 |
at:#bar put:'bar'; |
|
3207 |
at:#baz put:'baz'; |
|
3208 |
yourself) upTo:#bar |
|
8874 | 3209 |
|
5231 | 3210 |
" |
3211 |
||
3212 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3213 |
! |
|
3214 |
||
3215 |
upTo:anElement count:count |
|
3216 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3217 |
(but excluding) count found anElements, i.e. the first found count-1 |
3218 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3219 |
procedure is done from the end of the collection backwards. |
3220 |
If anElement is not in the receiver, the returned collection |
|
3221 |
will consist of all elements of the receiver" |
|
3222 |
||
3223 |
|startPos endPos pos found| |
|
3224 |
||
3225 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3226 |
^ self upTo:anElement |
5231 | 3227 |
]. |
3228 |
||
3229 |
startPos := 1. |
|
3230 |
endPos := self size. |
|
3231 |
found := 0. |
|
3232 |
||
3233 |
count > 1 ifTrue:[ |
|
8874 | 3234 |
pos := 0. |
3235 |
[pos < self size and: [found < count]] |
|
3236 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3237 |
found == count ifTrue:[ |
|
3238 |
endPos := pos - 1 |
|
3239 |
] |
|
3240 |
]. |
|
5231 | 3241 |
|
3242 |
count < 0 ifTrue:[ |
|
8874 | 3243 |
pos := self size + 1. |
3244 |
[pos > 1 and: [found < count abs]] |
|
3245 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3246 |
found == count abs ifTrue:[ |
|
3247 |
startPos := pos + 1 |
|
3248 |
] |
|
3249 |
]. |
|
5231 | 3250 |
|
3251 |
^ self copyFrom: startPos to: endPos |
|
3252 |
||
3253 |
" |
|
8874 | 3254 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3255 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3256 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3257 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3258 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3259 |
" |
|
5563 | 3260 |
! |
3261 |
||
3262 |
upToAny:aCollectionOfObjects |
|
3263 |
"return a new collection consisting of the receivers elements upTo |
|
3264 |
(but excluding) any in aCollectionOfObjects. |
|
3265 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3266 |
will consist of all elements of the receiver. |
|
3267 |
See also #upTo:" |
|
3268 |
||
3269 |
|pos| |
|
3270 |
||
3271 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3272 |
pos == 0 ifTrue:[^ self copy]. |
|
3273 |
||
3274 |
^ self copyFrom:1 to:(pos - 1) |
|
3275 |
||
3276 |
" |
|
8874 | 3277 |
'hello world' upToAny:#($/ $:) |
3278 |
'hello:world' upToAny:#($/ $:) |
|
3279 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3280 |
" |
602 | 3281 |
! ! |
3282 |
||
3283 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3284 |
||
3285 |
collect:aBlock |
|
3286 |
"evaluate the argument, aBlock for every element in the collection |
|
3287 |
and return a collection of the results" |
|
3288 |
||
3289 |
|newCollection |
|
3290 |
sz "{ Class:SmallInteger }"| |
|
3291 |
||
3292 |
sz := self size. |
|
3293 |
newCollection := self copyEmptyAndGrow:sz. |
|
3294 |
1 to:sz do:[:index | |
|
8874 | 3295 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3296 |
]. |
3297 |
^ newCollection |
|
3298 |
||
3299 |
" |
|
8874 | 3300 |
#(one two three four five six) collect:[:element | element asUppercase] |
3301 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3302 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3303 |
" |
202 | 3304 |
! |
3305 |
||
602 | 3306 |
do:aBlock |
3307 |
"evaluate the argument, aBlock for every element in the collection." |
|
3308 |
||
3309 |
|stop "{ Class:SmallInteger }"| |
|
3310 |
||
3311 |
stop := self size. |
|
3312 |
1 to:stop do:[:index | |
|
8874 | 3313 |
aBlock value:(self at:index). |
602 | 3314 |
] |
2927 | 3315 |
|
602 | 3316 |
" |
1422 | 3317 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 3318 |
" |
3319 |
! |
|
3320 |
||
2927 | 3321 |
do:aBlock separatedBy:sepBlock |
3322 |
"evaluate the argument, aBlock for every element in the collection. |
|
3323 |
Between each element (i.e. not before the first element and not after the |
|
3324 |
last element), evaluate sepBlock. |
|
3325 |
This supports printing of collections with elements separated by some |
|
3326 |
string." |
|
3327 |
||
3328 |
|stop "{ Class:SmallInteger }"| |
|
3329 |
||
3330 |
stop := self size. |
|
3331 |
stop < 1 ifTrue:[^ self]. |
|
3332 |
aBlock value:(self at:1). |
|
3333 |
2 to:stop do:[:index | |
|
8874 | 3334 |
sepBlock value. |
3335 |
aBlock value:(self at:index) |
|
2927 | 3336 |
]. |
3337 |
||
3338 |
" |
|
3339 |
#(one two three four five six) |
|
8874 | 3340 |
do:[:each | Transcript show:each] |
3341 |
separatedBy:[Transcript show:' , '] |
|
2927 | 3342 |
" |
3343 |
! |
|
3003 | 3344 |
|
3044 | 3345 |
doWithIndex:aBlock |
3346 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
3347 |
the index as second argument." |
|
3348 |
||
3349 |
self keysAndValuesDo:[:index :value | |
|
8874 | 3350 |
aBlock value:value value:index |
3044 | 3351 |
]. |
3352 |
||
3353 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
3354 |
! |
|
3355 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3356 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3357 |
"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
|
3358 |
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
|
3359 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3360 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3361 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3362 |
" |
8874 | 3363 |
#(one two three four five six) |
3364 |
from:2 |
|
3365 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3366 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3367 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3368 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3369 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3370 |
|
602 | 3371 |
from:startIndex do:aBlock |
3372 |
"evaluate the argument, aBlock for the elements starting with the |
|
3373 |
element at startIndex to the end." |
|
3374 |
||
3375 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 3376 |
|
95 | 3377 |
" |
8874 | 3378 |
#(one two three four five six) |
3379 |
from:3 |
|
3380 |
do:[:element | Transcript showCR:element] |
|
602 | 3381 |
" |
3382 |
! |
|
3383 |
||
7490 | 3384 |
from:startIndex keysAndValuesDo:aBlock |
3385 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
3386 |
element at startIndex to the end." |
|
3387 |
||
3388 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
3389 |
||
3390 |
" |
|
8874 | 3391 |
#(one two three four five six) |
3392 |
from:3 |
|
3393 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 3394 |
" |
3395 |
! |
|
3396 |
||
7337 | 3397 |
from:index1 to:index2 by:stepArg do:aBlock |
3398 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
3399 |
||
3400 |
|start "{ Class:SmallInteger }" |
|
3401 |
stop "{ Class:SmallInteger }" |
|
3402 |
step "{ Class:SmallInteger }" | |
|
3403 |
||
3404 |
start := index1. "/ these assignments force type checking... |
|
3405 |
stop := index2. "/ and guarantee inline loop code below. |
|
3406 |
step := stepArg. |
|
3407 |
||
3408 |
start to:stop by:step do:[:index | |
|
8874 | 3409 |
aBlock value:(self at:index). |
7337 | 3410 |
] |
3411 |
||
3412 |
" |
|
8874 | 3413 |
#(one two three four five six seven eight nine ten) |
3414 |
from:2 |
|
3415 |
to:10 |
|
3416 |
by:2 |
|
3417 |
do:[:element | Transcript showCR:element] |
|
7337 | 3418 |
" |
3419 |
! |
|
3420 |
||
602 | 3421 |
from:start to:stop collect:aBlock |
3422 |
"evaluate the argument, aBlock for the elements indexed by start |
|
3423 |
to stop in the collection and return a collection of the results" |
|
3424 |
||
3425 |
|newCollection sz |
|
3426 |
idx "{ Class:SmallInteger }"| |
|
3427 |
||
3428 |
sz := stop - start + 1. |
|
3429 |
newCollection := self copyEmptyAndGrow:sz. |
|
3430 |
idx := 1. |
|
3431 |
start to:stop do:[:index | |
|
8874 | 3432 |
newCollection at:idx put:(aBlock value:(self at:index)). |
3433 |
idx := idx + 1 |
|
602 | 3434 |
]. |
3435 |
^ newCollection |
|
3436 |
||
3437 |
" |
|
8874 | 3438 |
#(one two three four five six) |
3439 |
from:2 |
|
3440 |
to:4 |
|
3441 |
collect:[:element | element asUppercase] |
|
602 | 3442 |
" |
3443 |
! |
|
3444 |
||
3445 |
from:index1 to:index2 do:aBlock |
|
3446 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3447 |
index2 in the collection" |
|
3448 |
||
3449 |
|start "{ Class:SmallInteger }" |
|
3450 |
stop "{ Class:SmallInteger }" | |
|
3451 |
||
7337 | 3452 |
start := index1. "/ these assignments force type checking... |
3453 |
stop := index2. "/ and guarantee inline loop code below. |
|
3454 |
||
602 | 3455 |
start to:stop do:[:index | |
8874 | 3456 |
aBlock value:(self at:index). |
602 | 3457 |
] |
3458 |
||
3459 |
" |
|
8874 | 3460 |
#(one two three four five six) |
3461 |
from:3 |
|
3462 |
to:5 |
|
3463 |
do:[:element | Transcript showCR:element] |
|
602 | 3464 |
" |
3465 |
! |
|
3466 |
||
6868 | 3467 |
from:index1 to:index2 keysAndValuesDo:aBlock |
3468 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3469 |
index2 in the collection; pass both index and value." |
|
3470 |
||
3471 |
|start "{ Class:SmallInteger }" |
|
3472 |
stop "{ Class:SmallInteger }" | |
|
3473 |
||
7337 | 3474 |
start := index1. "/ these assignments force type checking... |
3475 |
stop := index2. "/ and guarantee inline loop code below. |
|
3476 |
||
6868 | 3477 |
start to:stop do:[:index | |
8874 | 3478 |
aBlock value:index value:(self at:index). |
6868 | 3479 |
] |
3480 |
||
3481 |
" |
|
8874 | 3482 |
#(one two three four five six) |
3483 |
from:3 |
|
3484 |
to:5 |
|
3485 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 3486 |
" |
3487 |
! |
|
3488 |
||
602 | 3489 |
from:index1 to:index2 reverseDo:aBlock |
3490 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3491 |
index2 in the collection. Step in reverse order" |
|
3492 |
||
3493 |
|start "{ Class:SmallInteger }" |
|
3494 |
stop "{ Class:SmallInteger }" | |
|
3495 |
||
7337 | 3496 |
start := index1. "/ these assignments force type checking... |
3497 |
stop := index2. "/ and guarantee inline loop code below. |
|
3498 |
||
602 | 3499 |
stop to:start by:-1 do:[:index | |
8874 | 3500 |
aBlock value:(self at:index). |
602 | 3501 |
] |
3502 |
||
3503 |
" |
|
8874 | 3504 |
#(one two three four five six) |
3505 |
from:3 |
|
3506 |
to:5 |
|
3507 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 3508 |
" |
61 | 3509 |
! |
3510 |
||
6027 | 3511 |
from:startIndex to:stopIndex select:aBlock |
3512 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
3513 |
and return a collection of those elements for which the block return true." |
|
3514 |
||
3515 |
|element newColl species needCopy |
|
3516 |
n "{ Class:SmallInteger }"| |
|
3517 |
||
3518 |
n := stopIndex - startIndex + 1. |
|
3519 |
species := self species. |
|
3520 |
species growIsCheap ifFalse:[ |
|
8874 | 3521 |
newColl := OrderedCollection new:n. |
3522 |
needCopy := true |
|
6027 | 3523 |
] ifTrue:[ |
8874 | 3524 |
newColl := self copyEmpty:n. |
3525 |
needCopy := false |
|
6027 | 3526 |
]. |
3527 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3528 |
element := self at:index. |
3529 |
(aBlock value:element) ifTrue:[ |
|
3530 |
newColl add:element |
|
3531 |
]. |
|
6027 | 3532 |
]. |
3533 |
needCopy ifTrue:[ |
|
8874 | 3534 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 3535 |
]. |
3536 |
^ newColl |
|
3537 |
||
3538 |
" |
|
8874 | 3539 |
#(faba one two three four five six) |
3540 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 3541 |
" |
3542 |
! |
|
3543 |
||
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3544 |
inGroupsOf:n collect:aBlock |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3545 |
"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
|
3546 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3547 |
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
|
3548 |
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
|
3549 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3550 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3551 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3552 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3553 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3554 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3555 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3556 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3557 |
n == 2 ifTrue:[ |
8874 | 3558 |
1 to:stop by:n do:[:index | |
3559 |
rslt := aBlock value:(self at:index) value:(self at:index+1). |
|
3560 |
newCollection at:dstIdx put:rslt. |
|
3561 |
dstIdx := dstIdx + 1. |
|
3562 |
]. |
|
3563 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3564 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3565 |
n == 3 ifTrue:[ |
8874 | 3566 |
1 to:stop by:n do:[:index | |
3567 |
rslt := aBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3568 |
newCollection at:dstIdx put:rslt. |
|
3569 |
dstIdx := dstIdx + 1. |
|
3570 |
]. |
|
3571 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3572 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3573 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3574 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3575 |
1 to:stop by:n do:[:index | |
8874 | 3576 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3577 |
rslt := aBlock valueWithArguments:argVector. |
|
3578 |
newCollection at:dstIdx put:rslt. |
|
3579 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3580 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3581 |
^ newCollection |
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 |
"for groups of 2, this is the same as: |
8874 | 3584 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3585 |
pairWiseCollect:[:num :sym | num->sym] |
|
3586 |
||
3587 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
3588 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3589 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3590 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
8874 | 3591 |
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
|
3592 |
" |
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 |
inGroupsOf:n do:aBlock |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3596 |
"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
|
3597 |
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
|
3598 |
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
|
3599 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3600 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3601 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3602 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3603 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3604 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3605 |
n == 2 ifTrue:[ |
8874 | 3606 |
1 to:stop by:n do:[:index | |
3607 |
aBlock value:(self at:index) value:(self at:index+1). |
|
3608 |
]. |
|
3609 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3610 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3611 |
n == 3 ifTrue:[ |
8874 | 3612 |
1 to:stop by:n do:[:index | |
3613 |
aBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3614 |
]. |
|
3615 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3616 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3617 |
n == 4 ifTrue:[ |
8874 | 3618 |
1 to:stop by:n do:[:index | |
3619 |
aBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
3620 |
]. |
|
3621 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3622 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3623 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3624 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3625 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3626 |
1 to:stop by:n do:[:index | |
8874 | 3627 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3628 |
aBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3629 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3630 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3631 |
"for groups of 2, this is the same as: |
8874 | 3632 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3633 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
|
3634 |
||
3635 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
3636 |
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
|
3637 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3638 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
8874 | 3639 |
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
|
3640 |
" |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3641 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3642 |
|
8853 | 3643 |
keysAndValuesCollect:aTwoArgBlock |
3644 |
"evaluate the argument, aBlock for every element in the collection, |
|
3645 |
passing both index and element as arguments. |
|
3646 |
Collect the returned values and return them." |
|
3647 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3648 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3649 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3650 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3651 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3652 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3653 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 3654 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3655 |
|
8853 | 3656 |
^ newCollection |
3657 |
||
3658 |
" |
|
8874 | 3659 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3660 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3661 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 3662 |
|
3663 |
||
3664 |
|d| |
|
3665 |
||
3666 |
d := Dictionary new. |
|
3667 |
d at:'a' put:'A'. |
|
3668 |
d at:'b' put:'B'. |
|
3669 |
d at:'c' put:'C'. |
|
3670 |
d at:'d' put:'D'. |
|
3671 |
d at:'e' put:'E'. |
|
8874 | 3672 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3673 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3674 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3675 |
ifFalse:[element asLowercase]] |
8853 | 3676 |
" |
3677 |
! |
|
3678 |
||
602 | 3679 |
keysAndValuesDo:aTwoArgBlock |
3680 |
"evaluate the argument, aBlock for every element in the collection, |
|
3681 |
passing both index and element as arguments." |
|
3682 |
||
3683 |
|stop "{ Class:SmallInteger }"| |
|
3684 |
||
3685 |
stop := self size. |
|
3686 |
1 to:stop do:[:index | |
|
8874 | 3687 |
aTwoArgBlock value:index value:(self at:index). |
602 | 3688 |
] |
3689 |
" |
|
8874 | 3690 |
#(one two three four five six) |
3691 |
keysAndValuesDo:[:key :element | |
|
3692 |
Transcript show:key; space; showCR:element |
|
3693 |
] |
|
602 | 3694 |
" |
3695 |
! |
|
3696 |
||
1359 | 3697 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 3698 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 3699 |
in the collection, passing both index and element as arguments." |
3700 |
||
3701 |
|stop "{ Class:SmallInteger }"| |
|
3702 |
||
3703 |
stop := self size. |
|
3704 |
stop to:1 by:-1 do:[:index | |
|
8874 | 3705 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 3706 |
] |
3707 |
||
3708 |
" |
|
8874 | 3709 |
#(one two three four five six) |
3710 |
keysAndValuesReverseDo:[:key :element | |
|
3711 |
Transcript show:key; space; showCR:element |
|
3712 |
] |
|
1359 | 3713 |
" |
3714 |
||
3715 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
3716 |
! |
|
3717 |
||
602 | 3718 |
nonNilElementsDo:aBlock |
3719 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
3720 |
||
3721 |
|stop "{ Class:SmallInteger }" |
|
3722 |
element| |
|
3723 |
||
3724 |
stop := self size. |
|
3725 |
1 to:stop do:[:index | |
|
8874 | 3726 |
(element := self at:index) notNil ifTrue:[ |
3727 |
aBlock value:element. |
|
3728 |
] |
|
602 | 3729 |
] |
3730 |
" |
|
1422 | 3731 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 3732 |
" |
3733 |
! |
|
3734 |
||
3735 |
pairWiseCollect:aBlock |
|
3736 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
3737 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
3738 |
An error will be reported, if the number of elements in the receiver |
|
3739 |
is not a multiple of 2. |
|
3740 |
Collect the results and return a new collection containing those." |
|
3741 |
||
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3742 |
^ self inGroupsOf:2 collect:aBlock |
44 | 3743 |
|
95 | 3744 |
" |
8874 | 3745 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3746 |
pairWiseCollect:[:num :sym | sym->num] |
|
3747 |
||
3748 |
||
3749 |
#(1 1 1 2 1 3 1 4 1 5) |
|
3750 |
pairWiseCollect:[:x :y | x@y] |
|
602 | 3751 |
" |
3752 |
! |
|
3753 |
||
3754 |
pairWiseDo:aBlock |
|
3755 |
"evaluate the argument, aBlock for every pair of elements in the collection. |
|
3756 |
The block is called with 2 arguments for each 2 elements in the receiver. |
|
3757 |
An error will be reported, if the number of elements in the receiver |
|
3758 |
is not a multiple of 2." |
|
3759 |
||
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3760 |
^ self inGroupsOf:2 do:aBlock |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3761 |
|
602 | 3762 |
" |
8874 | 3763 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
3764 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
|
3765 |
||
3766 |
||
3767 |
#(1 1 1 2 1 3 1 4 1 5) |
|
3768 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
|
95 | 3769 |
" |
61 | 3770 |
! |
3771 |
||
602 | 3772 |
reverseDo:aBlock |
3773 |
"evaluate the argument, aBlock for every element in the collection |
|
3774 |
in reverse order" |
|
3775 |
||
3776 |
|sz "{ Class:SmallInteger }"| |
|
3777 |
||
3778 |
sz := self size. |
|
3779 |
sz to:1 by:-1 do:[:index | |
|
8874 | 3780 |
aBlock value:(self at:index). |
602 | 3781 |
] |
3782 |
||
3783 |
" |
|
1422 | 3784 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 3785 |
" |
3786 |
! |
|
3787 |
||
3788 |
select:aBlock |
|
3789 |
"evaluate the argument, aBlock for every element in the collection |
|
3790 |
and return a collection of all elements for which the block return |
|
3791 |
true" |
|
3792 |
||
3793 |
|element newColl species needCopy |
|
3794 |
sz "{ Class:SmallInteger }"| |
|
3795 |
||
3796 |
sz := self size. |
|
3797 |
species := self species. |
|
3798 |
species growIsCheap ifFalse:[ |
|
8874 | 3799 |
newColl := OrderedCollection new:sz. |
3800 |
needCopy := true |
|
602 | 3801 |
] ifTrue:[ |
8874 | 3802 |
newColl := self copyEmpty:sz. |
3803 |
needCopy := false |
|
602 | 3804 |
]. |
3805 |
1 to:sz do:[:index | |
|
8874 | 3806 |
element := self at:index. |
3807 |
(aBlock value:element) ifTrue:[ |
|
3808 |
newColl add:element |
|
3809 |
]. |
|
602 | 3810 |
]. |
3811 |
needCopy ifTrue:[ |
|
8874 | 3812 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 3813 |
]. |
61 | 3814 |
^ newColl |
3815 |
||
95 | 3816 |
" |
8874 | 3817 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
3818 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
3819 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
3820 |
(1 to:9) select:[:element | element odd] |
|
3821 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 3822 |
" |
186 | 3823 |
! |
3824 |
||
602 | 3825 |
with:aSequenceableCollection do:aTwoArgBlock |
3826 |
"evaluate the argument, aBlock for successive elements from |
|
3827 |
each the receiver and the argument, aSequenceableCollection. |
|
3828 |
The second argument, aBlock must be a two-argument block. |
|
3829 |
The collection argument must implement access via a numeric key." |
|
3830 |
||
3831 |
|stop "{ Class: SmallInteger }" | |
|
3832 |
||
3833 |
stop := self size. |
|
3834 |
1 to:stop do:[:index | |
|
8874 | 3835 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 3836 |
] |
186 | 3837 |
" |
8874 | 3838 |
#(one two three four five six) |
3839 |
with:(1 to:10) |
|
3840 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 3841 |
" |
1 | 3842 |
! ! |
3843 |
||
4154 | 3844 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 3845 |
|
602 | 3846 |
atAllPut:anObject |
3847 |
"replace all elements of the collection by the argument, anObject. |
|
3848 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3849 |
therefore the change may affect all others referencing the receiver." |
|
3850 |
||
3851 |
self from:1 to:(self size) put:anObject |
|
3852 |
||
3853 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3854 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3855 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3856 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3857 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3858 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3859 |
" |
602 | 3860 |
(Array new:10) atAllPut:1 |
3861 |
(String new:10) atAllPut:$a |
|
3862 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3863 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3864 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 3865 |
! |
3866 |
||
1 | 3867 |
from:index1 to:index2 put:anObject |
3868 |
"replace the elements from index1 to index2 of the collection |
|
44 | 3869 |
by the argument, anObject. |
3870 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3871 |
therefore the change may affect all others referencing the receiver." |
|
1 | 3872 |
|
3873 |
|index "{ Class: SmallInteger }" |
|
3874 |
end "{ Class: SmallInteger }"| |
|
3875 |
||
3876 |
index := index1. |
|
3877 |
end := index2. |
|
3878 |
[index <= end] whileTrue:[ |
|
8874 | 3879 |
self at:index put:anObject. |
3880 |
index := index + 1 |
|
1 | 3881 |
] |
44 | 3882 |
|
95 | 3883 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3884 |
args: index1 : <integer> |
8874 | 3885 |
index2 : <integer> |
3886 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3887 |
|
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 |
" |
95 | 3892 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
3893 |
'abcdefghijkl' from:3 to:6 put:$X |
|
3894 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3895 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3896 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 3897 |
! |
3898 |
||
3003 | 3899 |
replaceAll:oldObject by:newObject |
3900 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
3901 |
Pleace do no longer use this one; |
|
3902 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
3903 |
||
5872 | 3904 |
<resource:#obsolete> |
3905 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3906 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 3907 |
^ self replaceAll:oldObject with:newObject |
3908 |
||
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 |
args: oldObject : <object> |
8874 | 3911 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3912 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3913 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3914 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3915 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3916 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 3917 |
! |
3918 |
||
3919 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
3920 |
"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
|
3921 |
Please do no longer use this one; |
3003 | 3922 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
3923 |
||
5872 | 3924 |
<resource:#obsolete> |
3925 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3926 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 3927 |
^ 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
|
3928 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3929 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3930 |
args: oldObject : <object> |
8874 | 3931 |
newObject : <object> |
3932 |
startIndex : <integer> |
|
3933 |
stopIndex : <integer> |
|
3934 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3935 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3936 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3937 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3938 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 3939 |
! |
3940 |
||
2927 | 3941 |
replaceAll:oldObject with:newObject |
44 | 3942 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3943 |
|
44 | 3944 |
Notice: This operation modifies the receiver, NOT a copy; |
3945 |
therefore the change may affect all others referencing the receiver." |
|
1 | 3946 |
|
8874 | 3947 |
^ self |
3948 |
replaceAll:oldObject |
|
3949 |
with:newObject |
|
3950 |
from:1 |
|
3951 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3952 |
|
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 |
args: oldObject : <object> |
8874 | 3955 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3956 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3957 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3958 |
" |
362 | 3959 |
|
3960 |
" |
|
8874 | 3961 |
'123123abc123' replaceAll:$1 with:$* |
3962 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 3963 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3964 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3965 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 3966 |
! |
293 | 3967 |
|
2927 | 3968 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 3969 |
"replace all oldObjects found between startIndex and endIndex, |
3970 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3971 |
|
362 | 3972 |
Notice: This operation modifies the receiver, NOT a copy; |
3973 |
therefore the change may affect all others referencing the receiver." |
|
3974 |
||
3975 |
|start "{ Class: SmallInteger }" |
|
3976 |
stop "{ Class: SmallInteger }"| |
|
3977 |
||
3978 |
start := startIndex. |
|
3979 |
stop := stopIndex. |
|
3980 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3981 |
(self at:index) = oldObject ifTrue:[ |
3982 |
self at:index put:newObject |
|
3983 |
] |
|
1 | 3984 |
] |
44 | 3985 |
|
95 | 3986 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3987 |
args: oldObject : <object> |
8874 | 3988 |
newObject : <object> |
3989 |
startIndex : <integer> |
|
3990 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3991 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3992 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3993 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3994 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3995 |
" |
2980 | 3996 |
'123123abc123' replaceAll:$1 with:$* |
3997 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
3998 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 3999 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4000 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4001 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4002 |
! |
4003 |
||
7615 | 4004 |
replaceAllForWhich:aConditionBlock with:newObject |
4005 |
"replace all elements for which aConditionBlock returns true |
|
4006 |
between startIndex and endIndex, by newObject in the receiver. |
|
4007 |
||
4008 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4009 |
therefore the change may affect all others referencing the receiver." |
|
4010 |
||
8874 | 4011 |
self |
4012 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4013 |
from:1 to:(self size) |
|
7615 | 4014 |
|
4015 |
" |
|
4016 |
args: aConditionBlock : <block> |
|
8874 | 4017 |
newObject : <object> |
7615 | 4018 |
|
4019 |
returns: self |
|
4020 |
" |
|
4021 |
! |
|
4022 |
||
4023 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4024 |
"replace all elements for which aConditionBlock returns true |
|
4025 |
between startIndex and endIndex, by newObject in the receiver. |
|
4026 |
||
4027 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4028 |
therefore the change may affect all others referencing the receiver." |
|
4029 |
||
4030 |
|start "{ Class: SmallInteger }" |
|
4031 |
stop "{ Class: SmallInteger }"| |
|
4032 |
||
4033 |
start := startIndex. |
|
4034 |
stop := stopIndex. |
|
4035 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4036 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4037 |
self at:index put:newObject |
|
4038 |
] |
|
7615 | 4039 |
] |
4040 |
||
4041 |
" |
|
4042 |
args: aConditionBlock : <block> |
|
8874 | 4043 |
newObject : <object> |
4044 |
startIndex : <integer> |
|
4045 |
stopIndex : <integer> |
|
7615 | 4046 |
|
4047 |
returns: self |
|
4048 |
" |
|
4049 |
! |
|
4050 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4051 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4052 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4053 |
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
|
4054 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4055 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4056 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4057 |
|
8874 | 4058 |
^ self |
4059 |
replaceAllIdentical:oldObject |
|
4060 |
with:newObject |
|
4061 |
from:1 |
|
4062 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4063 |
|
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 |
args: oldObject : <object> |
8874 | 4066 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4067 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4068 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4069 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4070 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4071 |
" |
8874 | 4072 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4073 |
#(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
|
4074 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4075 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4076 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4077 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4078 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4079 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4080 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4081 |
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
|
4082 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4083 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4084 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4085 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4086 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4087 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4088 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4089 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4090 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4091 |
startIndex to:stopIndex do:[:index | |
8874 | 4092 |
(self at:index) == oldObject ifTrue:[ |
4093 |
self at:index put:newObject |
|
4094 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4095 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4096 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4097 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4098 |
args: oldObject : <object> |
8874 | 4099 |
newObject : <object> |
4100 |
startIndex : <integer> |
|
4101 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4102 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4103 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4104 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4105 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4106 |
" |
8874 | 4107 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4108 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4109 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4110 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4111 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4112 |
|
3003 | 4113 |
replaceAny:aCollection by:newObject |
4114 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4115 |
Pleace do no longer use this one; |
3003 | 4116 |
instead use #replaceAny:with: for naming consistence." |
4117 |
||
5872 | 4118 |
<resource:#obsolete> |
4119 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4120 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4121 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4122 |
|
4123 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4124 |
args: aCollection : <colleciton of <object> > |
8874 | 4125 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4126 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4127 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4128 |
" |
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 |
" |
3003 | 4131 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4132 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4133 |
" |
|
4134 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4135 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4136 |
! |
4137 |
||
3003 | 4138 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4139 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4140 |
Pleace do no longer use this one; |
3003 | 4141 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4142 |
|
5872 | 4143 |
<resource:#obsolete> |
4144 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4145 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4146 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4147 |
|
4148 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4149 |
args: aCollection : <colleciton of <object> > |
8874 | 4150 |
newObject : <object> |
4151 |
startIndex : <integer> |
|
4152 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4153 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4154 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4155 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4156 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4157 |
" |
8874 | 4158 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4159 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4160 |
" |
4161 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4162 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4163 |
! |
4164 |
||
4165 |
replaceAny:aCollection with:newObject |
|
4166 |
"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
|
4167 |
|
2927 | 4168 |
Notice: This operation modifies the receiver, NOT a copy; |
4169 |
therefore the change may affect all others referencing the receiver." |
|
4170 |
||
8874 | 4171 |
^ self |
4172 |
replaceAny:aCollection |
|
4173 |
with:newObject |
|
4174 |
from:1 |
|
4175 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4176 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4177 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4178 |
args: aCollection : <colleciton of <object> > |
8874 | 4179 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4180 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4181 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4182 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4183 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4184 |
"Modified: / 20.5.1998 / 15:25:58 / cg" |
602 | 4185 |
! |
4186 |
||
2927 | 4187 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4188 |
"replace all elements within a range, |
4189 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4190 |
|
362 | 4191 |
Notice: This operation modifies the receiver, NOT a copy; |
4192 |
therefore the change may affect all others referencing the receiver." |
|
4193 |
||
4194 |
|start "{ Class: SmallInteger }" |
|
4195 |
stop "{ Class: SmallInteger }"| |
|
4196 |
||
4197 |
start := startIndex. |
|
4198 |
stop := stopIndex. |
|
4199 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4200 |
(aCollection includes:(self at:index)) ifTrue:[ |
4201 |
self at:index put:newObject |
|
4202 |
] |
|
95 | 4203 |
] |
4204 |
||
4205 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4206 |
args: aCollection : <colleciton of <object> > |
8874 | 4207 |
newObject : <object> |
4208 |
startIndex : <integer> |
|
4209 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4210 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4211 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4212 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4213 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4214 |
" |
8874 | 4215 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4216 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4217 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4218 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4219 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4220 |
! |
4221 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4222 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4223 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4224 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4225 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4226 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4227 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4228 |
|
8874 | 4229 |
^ self |
4230 |
replaceFrom:startIndex |
|
4231 |
to:(startIndex + numberOfElements - 1) |
|
4232 |
with:replacementCollection |
|
4233 |
startingAt:1 |
|
4234 |
||
4235 |
" |
|
4236 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4237 |
#($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
|
4238 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4239 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4240 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4241 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4242 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4243 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4244 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4245 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4246 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4247 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4248 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4249 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4250 |
^ self |
8874 | 4251 |
replaceFrom:startIndex |
4252 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4253 |
with:replacementCollection |
|
4254 |
startingAt:repStartIndex |
|
4255 |
||
4256 |
" |
|
4257 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4258 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4259 |
#($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
|
4260 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4261 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4262 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4263 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4264 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4265 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4266 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 4267 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4268 |
|
44 | 4269 |
Notice: This operation modifies the receiver, NOT a copy; |
4270 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4271 |
|
8874 | 4272 |
^ self |
4273 |
replaceFrom:startIndex |
|
4274 |
to:stopIndex |
|
4275 |
with:replacementCollection |
|
4276 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4277 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4278 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4279 |
args: startIndex : <integer> |
8874 | 4280 |
stopIndex : <integer> |
4281 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4282 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4283 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4284 |
" |
61 | 4285 |
|
95 | 4286 |
" |
4287 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
4288 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
4289 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4290 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4291 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 4292 |
! |
4293 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4294 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4295 |
"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
|
4296 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 4297 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4298 |
|
44 | 4299 |
Notice: This operation modifies the receiver, NOT a copy; |
4300 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4301 |
|
4302 |
|srcIndex "{ Class: SmallInteger }" |
|
4303 |
dstIndex "{ Class: SmallInteger }" |
|
4304 |
end "{ Class: SmallInteger }" | |
|
4305 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4306 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 4307 |
" must do reverse copy " |
4308 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
4309 |
dstIndex := stopIndex. |
|
4310 |
end := startIndex. |
|
4311 |
[dstIndex >= end] whileTrue:[ |
|
4312 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
4313 |
srcIndex := srcIndex - 1. |
|
4314 |
dstIndex := dstIndex - 1 |
|
4315 |
]. |
|
4316 |
^ self |
|
1 | 4317 |
]. |
4318 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4319 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4320 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4321 |
end := stopIndex. |
1 | 4322 |
[dstIndex <= end] whileTrue:[ |
8874 | 4323 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
4324 |
srcIndex := srcIndex + 1. |
|
4325 |
dstIndex := dstIndex + 1 |
|
1 | 4326 |
] |
61 | 4327 |
|
95 | 4328 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4329 |
args: startIndex : <integer> |
8874 | 4330 |
stopIndex : <integer> |
4331 |
replacementCollection : <colleciton of <object> > |
|
4332 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4333 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4334 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4335 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4336 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4337 |
" |
8874 | 4338 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
4339 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
4340 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 4341 |
|
4342 |
|c| |
|
4343 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4344 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 4345 |
|
4346 |
|c| |
|
4347 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4348 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 4349 |
" |
3234 | 4350 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4351 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 4352 |
! |
4353 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4354 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4355 |
"replace elements in the receiver starting at startIndex, |
602 | 4356 |
with elements taken from replacementCollection starting at 1 |
4357 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4358 |
|
602 | 4359 |
Notice: This operation modifies the receiver, NOT a copy; |
4360 |
therefore the change may affect all others referencing the receiver." |
|
4361 |
||
8874 | 4362 |
^ self |
4363 |
replaceFrom:startIndex |
|
4364 |
to:(startIndex + replacementCollection size - 1) |
|
4365 |
with:replacementCollection |
|
4366 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4367 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4368 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4369 |
args: startIndex : <integer> |
8874 | 4370 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4371 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4372 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4373 |
" |
602 | 4374 |
|
4375 |
" |
|
4376 |
'1234567890' replaceFrom:5 with:'abc' |
|
4377 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
4378 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4379 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4380 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 4381 |
! |
4382 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4383 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4384 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4385 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 4386 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4387 |
|
602 | 4388 |
Notice: This operation modifies the receiver, NOT a copy; |
4389 |
therefore the change may affect all others referencing the receiver." |
|
4390 |
||
8874 | 4391 |
^ self |
4392 |
replaceFrom:startIndex |
|
4393 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
4394 |
with:replacementCollection |
|
4395 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4396 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4397 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4398 |
args: startIndex : <integer> |
8874 | 4399 |
replacementCollection : <colleciton of <object> > |
4400 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4401 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4402 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4403 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4404 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4405 |
" |
8874 | 4406 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 4407 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
4408 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4409 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4410 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 4411 |
! |
4412 |
||
61 | 4413 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
4414 |
"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
|
4415 |
|
61 | 4416 |
Notice: This operation modifies the destination collection, NOT a copy; |
4417 |
therefore the change may affect all others referencing this object." |
|
4418 |
||
8874 | 4419 |
destColl |
4420 |
replaceFrom:destStartIndex |
|
4421 |
to:destEndIndex |
|
4422 |
with:self |
|
4423 |
startingAt:sourceStart |
|
61 | 4424 |
|
4425 |
" |
|
4426 |
|s| |
|
4427 |
s := 'abcdefghijklmnop'. |
|
4428 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
4429 |
s'123defghijklmnop' |
|
4430 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4431 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4432 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 4433 |
! ! |
4434 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4435 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4436 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4437 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4438 |
"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
|
4439 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4440 |
Elements on the left are filled with padElement. |
8874 | 4441 |
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
|
4442 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4443 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4444 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4445 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4446 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4447 |
(len < size) ifTrue:[ |
8874 | 4448 |
s := self species new:size withAll:padElement. |
4449 |
s replaceFrom:(size - len + 1) with:self. |
|
4450 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4451 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4452 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4453 |
" |
8874 | 4454 |
'foo' leftPaddedTo:10 with:$. |
4455 |
'fooBar' leftPaddedTo:5 with:$. |
|
4456 |
123 printString leftPaddedTo:10 with:$. |
|
4457 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
4458 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
4459 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
4460 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
4461 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
4462 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
4463 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4464 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4465 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4466 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4467 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4468 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4469 |
plus pad elements up to length. |
8874 | 4470 |
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
|
4471 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4472 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4473 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4474 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4475 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4476 |
len < newSize ifTrue:[ |
8874 | 4477 |
s := self species new:newSize withAll:padElement. |
4478 |
s replaceFrom:1 to:len with:self. |
|
4479 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4480 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4481 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4482 |
" |
8874 | 4483 |
'foo' paddedTo:10 with:$. |
4484 |
123 printString paddedTo:10 with:$* |
|
4485 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
4486 |
(Float pi printString) paddedTo:15 with:$- |
|
4487 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
4488 |
#[1 2 3 4] paddedTo:6 with:0 |
|
4489 |
#[1 2 3 4] paddedTo:10 with:99 |
|
4490 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4491 |
" |
7116 | 4492 |
! |
4493 |
||
4494 |
paddedToMultipleOf:sizeModulu with:padElement |
|
4495 |
|mySize mod numPad| |
|
4496 |
||
4497 |
mySize := self size. |
|
4498 |
mod := (mySize \\ sizeModulu). |
|
4499 |
mod == 0 ifTrue:[^ self]. |
|
4500 |
||
4501 |
numPad := sizeModulu - mod. |
|
4502 |
^ self , (self species new:numPad withAll:padElement). |
|
4503 |
||
4504 |
" |
|
8874 | 4505 |
#[] paddedToMultipleOf:3 with:0 |
4506 |
#[1] paddedToMultipleOf:3 with:0 |
|
4507 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
4508 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 4509 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
4510 |
||
4511 |
#() paddedToMultipleOf:3 with:nil |
|
4512 |
#(1) paddedToMultipleOf:3 with:nil |
|
4513 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
4514 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
4515 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
4516 |
||
4517 |
'' paddedToMultipleOf:3 with:Character space |
|
4518 |
'a' paddedToMultipleOf:3 with:Character space |
|
4519 |
'ab' paddedToMultipleOf:3 with:Character space |
|
4520 |
'abc' paddedToMultipleOf:3 with:Character space |
|
4521 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
4522 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4523 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4524 |
|
7260 | 4525 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 4526 |
|
5358 | 4527 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 4528 |
"Private!! |
5358 | 4529 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
4530 |
||
4531 |
| i1 i2 val1 val2 out | |
|
4532 |
||
4533 |
i1 := first. |
|
4534 |
i2 := middle + 1. |
|
4535 |
val1 := self at: i1. |
|
4536 |
val2 := self at: i2. |
|
4537 |
out := first - 1. "will be pre-incremented" |
|
4538 |
||
4539 |
"select 'lower' half of the elements based on comparator" |
|
4540 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 4541 |
(aBlock value: val1 value: val2) |
4542 |
ifTrue: [ |
|
4543 |
dst at: (out := out + 1) put: val1. |
|
4544 |
val1 := self at: (i1 := i1 + 1)] |
|
4545 |
ifFalse: [ |
|
4546 |
dst at: (out := out + 1) put: val2. |
|
4547 |
i2 := i2 + 1. |
|
4548 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 4549 |
|
4550 |
"copy the remaining elements" |
|
4551 |
i1 <= middle |
|
8874 | 4552 |
ifTrue: [ |
4553 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
4554 |
ifFalse: [ |
|
4555 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 4556 |
|
4557 |
! |
|
4558 |
||
4559 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
4560 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
4561 |
||
4562 |
| middle | |
|
4563 |
||
4564 |
first = last ifTrue: [^ self]. |
|
4565 |
middle := (first + last) // 2. |
|
4566 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
4567 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
4568 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
4569 |
||
4570 |
||
4571 |
! |
|
4572 |
||
2069 | 4573 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4574 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4575 |
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
|
4576 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4577 |
|
8874 | 4578 |
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
|
4579 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4580 |
|
4581 |
|begin "{ Class: SmallInteger }" |
|
4582 |
end "{ Class: SmallInteger }" |
|
4583 |
b "{ Class: SmallInteger }" |
|
4584 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4585 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4586 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4587 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4588 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4589 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4590 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4591 |
[stack notEmpty] whileTrue:[ |
8874 | 4592 |
end := stack removeLast. |
4593 |
begin := stack removeLast. |
|
4594 |
||
4595 |
b := begin. |
|
4596 |
e := end. |
|
4597 |
middleElement := self at:((b + e) // 2). |
|
4598 |
||
4599 |
[b < e] whileTrue:[ |
|
4600 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4601 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4602 |
||
4603 |
(b <= e) ifTrue:[ |
|
4604 |
(b == e) ifFalse:[ |
|
4605 |
temp1 := self at:b. temp2 := self at:e. |
|
4606 |
self at:b put:temp2. self at:e put:temp1 |
|
4607 |
]. |
|
4608 |
b := b + 1. |
|
4609 |
e := e - 1 |
|
4610 |
] |
|
4611 |
]. |
|
4612 |
(begin < e) ifTrue:[ |
|
4613 |
stack add:begin. |
|
4614 |
stack add:e. |
|
4615 |
]. |
|
4616 |
(b < end) ifTrue:[ |
|
4617 |
stack add:b. |
|
4618 |
stack add:end. |
|
4619 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4620 |
] |
2069 | 4621 |
! |
4622 |
||
4623 |
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
|
4624 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4625 |
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
|
4626 |
with degenerated collections." |
2069 | 4627 |
|
4628 |
|begin "{ Class: SmallInteger }" |
|
4629 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4630 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4631 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4632 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4633 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4634 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4635 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4636 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4637 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4638 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4639 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4640 |
[stack notEmpty] whileTrue:[ |
8874 | 4641 |
end := stack removeLast. |
4642 |
begin := stack removeLast. |
|
4643 |
||
4644 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4645 |
elB := self at:begin. |
|
4646 |
elE := self at:end. |
|
4647 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4648 |
temp := elB. elB := elE. elE := temp. |
|
4649 |
self at:begin put:elB. |
|
4650 |
self at:end put:elE. |
|
4651 |
]. |
|
4652 |
||
4653 |
nToSort > 2 ifTrue:[ |
|
4654 |
m := (begin + end) // 2. |
|
4655 |
elM := self at:m. |
|
4656 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4657 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4658 |
temp := self at:end. |
|
4659 |
self at:end put:(self at:m). |
|
4660 |
self at:m put:temp. |
|
4661 |
elM := elE |
|
4662 |
] |
|
4663 |
] ifFalse:[ |
|
4664 |
temp := self at:begin. |
|
4665 |
self at:begin put:(self at:m). |
|
4666 |
self at:m put:temp. |
|
4667 |
elM := elB |
|
4668 |
]. |
|
4669 |
nToSort > 3 ifTrue:[ |
|
4670 |
bRun := begin. |
|
4671 |
eRun := end. |
|
4672 |
[ |
|
4673 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4674 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4675 |
bRun <= eRun |
|
4676 |
] whileTrue:[ |
|
4677 |
temp := self at:bRun. |
|
4678 |
self at:bRun put:(self at:eRun). |
|
4679 |
self at:eRun put:temp. |
|
4680 |
]. |
|
4681 |
stack add:begin. |
|
4682 |
stack add:eRun. |
|
4683 |
||
4684 |
stack add:bRun. |
|
4685 |
stack add:end. |
|
4686 |
] |
|
4687 |
] |
|
4688 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4689 |
] |
2069 | 4690 |
! |
4691 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4692 |
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
|
4693 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4694 |
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
|
4695 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4696 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4697 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4698 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4699 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4700 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4701 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4702 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4703 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4704 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4705 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4706 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4707 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4708 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4709 |
[stack notEmpty] whileTrue:[ |
8874 | 4710 |
end := stack removeLast. |
4711 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4712 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4713 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 4714 |
elB := self at:begin. |
4715 |
elE := self at:end. |
|
4716 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
4717 |
temp := elB. elB := elE. elE := temp. |
|
4718 |
self at:begin put:elB. |
|
4719 |
self at:end put:elE. |
|
4720 |
]. |
|
4721 |
||
4722 |
nToSort > 2 ifTrue:[ |
|
4723 |
m := (begin + end) // 2. |
|
4724 |
elM := self at:m. |
|
4725 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
4726 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
4727 |
temp := self at:end. |
|
4728 |
self at:end put:(self at:m). |
|
4729 |
self at:m put:temp. |
|
4730 |
elM := elE |
|
4731 |
] |
|
4732 |
] ifFalse:[ |
|
4733 |
temp := self at:begin. |
|
4734 |
self at:begin put:(self at:m). |
|
4735 |
self at:m put:temp. |
|
4736 |
elM := elB |
|
4737 |
]. |
|
4738 |
nToSort > 3 ifTrue:[ |
|
4739 |
bRun := begin. |
|
4740 |
eRun := end. |
|
4741 |
[ |
|
4742 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
4743 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
4744 |
bRun <= eRun |
|
4745 |
] whileTrue:[ |
|
4746 |
temp := self at:bRun. |
|
4747 |
self at:bRun put:(self at:eRun). |
|
4748 |
self at:eRun put:temp. |
|
4749 |
]. |
|
4750 |
stack add:begin. |
|
4751 |
stack add:eRun. |
|
4752 |
||
4753 |
stack add:bRun. |
|
4754 |
stack add:end. |
|
4755 |
] |
|
4756 |
] |
|
4757 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4758 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4759 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4760 |
|
2069 | 4761 |
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
|
4762 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4763 |
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
|
4764 |
with degenerated collections." |
2069 | 4765 |
|
4766 |
|begin "{ Class: SmallInteger }" |
|
4767 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4768 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4769 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4770 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4771 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4772 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4773 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4774 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4775 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4776 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4777 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4778 |
[stack notEmpty] whileTrue:[ |
8874 | 4779 |
end := stack removeLast. |
4780 |
begin := stack removeLast. |
|
4781 |
||
4782 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4783 |
||
4784 |
elB := self at:begin. |
|
4785 |
elE := self at:end. |
|
4786 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4787 |
temp := elB. elB := elE. elE := temp. |
|
4788 |
self at:begin put:elB. |
|
4789 |
self at:end put:elE. |
|
4790 |
||
4791 |
temp := aCollection at:end. |
|
4792 |
aCollection at:end put:(aCollection at:begin). |
|
4793 |
aCollection at:begin put:temp. |
|
4794 |
]. |
|
4795 |
||
4796 |
nToSort > 2 ifTrue:[ |
|
4797 |
m := (begin + end) // 2. |
|
4798 |
elM := self at:m. |
|
4799 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4800 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4801 |
temp := self at:end. |
|
4802 |
self at:end put:(self at:m). |
|
4803 |
self at:m put:temp. |
|
4804 |
||
4805 |
temp := aCollection at:end. |
|
4806 |
aCollection at:end put:(aCollection at:m). |
|
4807 |
aCollection at:m put:temp. |
|
4808 |
||
4809 |
elM := elE |
|
4810 |
] |
|
4811 |
] ifFalse:[ |
|
4812 |
temp := self at:begin. |
|
4813 |
self at:begin put:(self at:m). |
|
4814 |
self at:m put:temp. |
|
4815 |
||
4816 |
temp := aCollection at:begin. |
|
4817 |
aCollection at:begin put:(aCollection at:m). |
|
4818 |
aCollection at:m put:temp. |
|
4819 |
elM := elB |
|
4820 |
]. |
|
4821 |
nToSort > 3 ifTrue:[ |
|
4822 |
bRun := begin. |
|
4823 |
eRun := end. |
|
4824 |
[ |
|
4825 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4826 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4827 |
bRun <= eRun |
|
4828 |
] whileTrue:[ |
|
4829 |
temp := self at:bRun. |
|
4830 |
self at:bRun put:(self at:eRun). |
|
4831 |
self at:eRun put:temp. |
|
4832 |
||
4833 |
temp := aCollection at:bRun. |
|
4834 |
aCollection at:bRun put:(aCollection at:eRun). |
|
4835 |
aCollection at:eRun put:temp. |
|
4836 |
]. |
|
4837 |
stack add:begin. |
|
4838 |
stack add:eRun. |
|
4839 |
||
4840 |
stack add:bRun. |
|
4841 |
stack add:end. |
|
4842 |
] |
|
4843 |
] |
|
4844 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4845 |
] |
2069 | 4846 |
! |
4847 |
||
4848 |
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
|
4849 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4850 |
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
|
4851 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4852 |
|
8874 | 4853 |
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
|
4854 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4855 |
|
4856 |
|begin "{ Class: SmallInteger }" |
|
4857 |
end "{ Class: SmallInteger }" |
|
4858 |
b "{ Class: SmallInteger }" |
|
4859 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4860 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4861 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4862 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4863 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4864 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4865 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4866 |
[stack notEmpty] whileTrue:[ |
8874 | 4867 |
end := stack removeLast. |
4868 |
begin := stack removeLast. |
|
4869 |
||
4870 |
b := begin. |
|
4871 |
e := end. |
|
4872 |
middleElement := self at:((b + e) // 2). |
|
4873 |
||
4874 |
[b < e] whileTrue:[ |
|
4875 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4876 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4877 |
||
4878 |
(b <= e) ifTrue:[ |
|
4879 |
(b == e) ifFalse:[ |
|
4880 |
temp1 := self at:b. temp2 := self at:e. |
|
4881 |
self at:b put:temp2. self at:e put:temp1. |
|
4882 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
4883 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
4884 |
]. |
|
4885 |
b := b + 1. |
|
4886 |
e := e - 1 |
|
4887 |
] |
|
4888 |
]. |
|
4889 |
(begin < e) ifTrue:[ |
|
4890 |
stack add:begin. |
|
4891 |
stack add:e. |
|
4892 |
]. |
|
4893 |
(b < end) ifTrue:[ |
|
4894 |
stack add:b. |
|
4895 |
stack add:end. |
|
4896 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4897 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4898 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4899 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4900 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4901 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4902 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4903 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4904 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4905 |
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
|
4906 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4907 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4908 |
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
|
4909 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4910 |
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
|
4911 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4912 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4913 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4914 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4915 |
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
|
4916 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4917 |
"/ 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
|
4918 |
^ 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
|
4919 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4920 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4921 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4922 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4923 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4924 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4925 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4926 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4927 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4928 |
"/ 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
|
4929 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4930 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4931 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4932 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4933 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4934 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4935 |
"/ "/ 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
|
4936 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4937 |
"/ 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
|
4938 |
"/ 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
|
4939 |
"/ 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
|
4940 |
"/ aCollection notNil ifTrue:[ |
8874 | 4941 |
"/ 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
|
4942 |
"/ 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
|
4943 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4944 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4945 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4946 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4947 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4948 |
"/ 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
|
4949 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4950 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4951 |
"/ [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
|
4952 |
"/ [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
|
4953 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4954 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4955 |
"/ (b == e) ifFalse:[ |
8874 | 4956 |
"/ 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
|
4957 |
"/ 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
|
4958 |
"/ aCollection notNil ifTrue:[ |
8874 | 4959 |
"/ 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
|
4960 |
"/ 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
|
4961 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4962 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4963 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4964 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4965 |
"/ ] |
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 |
"/ (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
|
4968 |
"/ (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
|
4969 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4970 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4971 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4972 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4973 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4974 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4975 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4976 |
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
|
4977 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4978 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4979 |
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
|
4980 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4981 |
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
|
4982 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4983 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4984 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4985 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4986 |
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
|
4987 |
"/ 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
|
4988 |
^ 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
|
4989 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4990 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4991 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4992 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4993 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4994 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4995 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4996 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4997 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4998 |
"/ 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
|
4999 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5000 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5001 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5002 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5003 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5004 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5005 |
"/ "/ 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
|
5006 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5007 |
"/ 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
|
5008 |
"/ 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
|
5009 |
"/ 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
|
5010 |
"/ aCollection notNil ifTrue:[ |
8874 | 5011 |
"/ 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
|
5012 |
"/ 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
|
5013 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5014 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5015 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5016 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5017 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5018 |
"/ 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
|
5019 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5020 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5021 |
"/ [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
|
5022 |
"/ [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
|
5023 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5024 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5025 |
"/ (b == e) ifFalse:[ |
8874 | 5026 |
"/ 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
|
5027 |
"/ 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
|
5028 |
"/ aCollection notNil ifTrue:[ |
8874 | 5029 |
"/ 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
|
5030 |
"/ 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
|
5031 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5032 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5033 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5034 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5035 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5036 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5037 |
"/ (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
|
5038 |
"/ (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
|
5039 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5040 |
"/ "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
|
5041 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5042 |
! ! |
5043 |
||
602 | 5044 |
!SequenceableCollection methodsFor:'queries'! |
5045 |
||
5046 |
firstIndex |
|
5047 |
"return the first elements index" |
|
5048 |
||
5049 |
^ 1 |
|
5050 |
! |
|
5051 |
||
5052 |
isSequenceable |
|
5053 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5054 |
||
5055 |
^ true |
|
1 | 5056 |
! |
5057 |
||
602 | 5058 |
isSequenceableCollection |
5059 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5060 |
This method is a historic leftover and will be removed." |
|
5061 |
||
5872 | 5062 |
<resource:#obsolete> |
5063 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5064 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5065 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5066 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5067 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5068 |
! |
5069 |
||
5358 | 5070 |
isSorted |
5071 |
"return true. if my elements are sorted (already)" |
|
5072 |
||
5073 |
|lastEl el| |
|
5074 |
||
5075 |
lastEl := self first. |
|
5076 |
2 to:self size do:[:i | |
|
8874 | 5077 |
el := self at:i. |
5078 |
el >= lastEl ifFalse: [^ false]. |
|
5079 |
lastEl := el |
|
5358 | 5080 |
]. |
5081 |
^ true |
|
5082 |
||
5083 |
" |
|
8874 | 5084 |
#(1 2 3 5 10 100) isSorted |
5085 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5086 |
" |
5087 |
! |
|
5088 |
||
5089 |
isSortedBy:aBlock |
|
5090 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5091 |
||
5092 |
|lastEl el| |
|
5093 |
||
5094 |
lastEl := self first. |
|
5095 |
2 to:self size do:[:i | |
|
8874 | 5096 |
el := self at:i. |
5097 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5098 |
lastEl := el |
|
5358 | 5099 |
]. |
5100 |
^ true |
|
5101 |
||
5102 |
" |
|
8874 | 5103 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5104 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5105 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5106 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5107 |
" |
5108 |
||
5109 |
! |
|
5110 |
||
602 | 5111 |
keys |
5112 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5113 |
||
5114 |
|sz| |
|
5115 |
||
1 | 5116 |
sz := self size. |
602 | 5117 |
sz == 0 ifTrue:[ |
8874 | 5118 |
^ #() |
61 | 5119 |
]. |
602 | 5120 |
^ 1 to:sz |
1 | 5121 |
! |
5122 |
||
602 | 5123 |
lastIndex |
5124 |
"return the last elements index" |
|
5125 |
||
5126 |
^ self size |
|
61 | 5127 |
! |
5128 |
||
602 | 5129 |
size |
5130 |
"return the number of elements in the collection. |
|
5131 |
concrete implementations must define this" |
|
5132 |
||
5133 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5134 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5135 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5136 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5137 |
"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
|
5138 |
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
|
5139 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5140 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5141 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5142 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5143 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5144 |
! ! |
5145 |
||
5146 |
!SequenceableCollection methodsFor:'searching'! |
|
5147 |
||
3887 | 5148 |
detect:aBlock startingAt:startIndex |
5149 |
"find the first element, for which evaluation of the argument, aBlock |
|
5150 |
return true, starting the search with startIndex. |
|
5151 |
If none does so, report an error" |
|
5152 |
||
5153 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5154 |
||
5155 |
" |
|
8874 | 5156 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5157 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5158 |
" |
5159 |
||
5160 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5161 |
! |
|
5162 |
||
5163 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5164 |
"find the first element, for which evaluation of the argument, aBlock |
|
5165 |
return true, starting the search with startIndex. |
|
5717 | 5166 |
If none does so, return the evaluation of exceptionBlock" |
5167 |
||
5168 |
|stop| |
|
3887 | 5169 |
|
5170 |
stop := self size. |
|
5717 | 5171 |
self from:startIndex to:stop do:[:el | |
8874 | 5172 |
(aBlock value:el) ifTrue:[ |
5173 |
^ el |
|
5174 |
]. |
|
3887 | 5175 |
]. |
5176 |
^ exceptionBlock value |
|
5177 |
||
5178 |
" |
|
8874 | 5179 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5180 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5181 |
" |
5182 |
||
5183 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5184 |
! |
|
5185 |
||
602 | 5186 |
findFirst:aBlock |
5187 |
"find the first element, for which evaluation of the argument, aBlock |
|
3887 | 5188 |
returns true; return its index or 0 if none detected. |
5189 |
This is much like #detect, however, here an INDEX is returned, |
|
5190 |
while #detect returns the element." |
|
602 | 5191 |
|
5192 |
|stop "{ Class: SmallInteger }" | |
|
5193 |
||
32 | 5194 |
stop := self size. |
602 | 5195 |
1 to:stop do:[:index | |
8874 | 5196 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5197 |
]. |
5198 |
^ 0 |
|
159 | 5199 |
|
95 | 5200 |
" |
602 | 5201 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
5202 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
8874 | 5203 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
602 | 5204 |
'one.two.three' findFirst:[:c | (c == $.)] |
8874 | 5205 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
5206 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
95 | 5207 |
" |
3887 | 5208 |
|
5209 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
1 | 5210 |
! |
5211 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5212 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5213 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5214 |
returns true; return its index or 0 if none detected. |
5215 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5216 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5217 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5218 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5219 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5220 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5221 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5222 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5223 |
start to:stop do:[:index | |
8874 | 5224 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
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 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5227 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5228 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5229 |
#(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
|
5230 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5231 |
" |
3887 | 5232 |
|
5233 |
"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
|
5234 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5235 |
|
602 | 5236 |
findLast:aBlock |
5237 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5238 |
returns true. |
5239 |
Return its index or 0 if none detected." |
|
602 | 5240 |
|
5241 |
|start "{ Class: SmallInteger }"| |
|
5242 |
||
5243 |
start := self size. |
|
5244 |
start to:1 by:-1 do:[:index | |
|
8874 | 5245 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5246 |
]. |
5247 |
^ 0 |
|
5248 |
||
5249 |
" |
|
602 | 5250 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5251 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5252 |
" |
5253 |
! |
|
5254 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5255 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5256 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5257 |
returns true. Start the search at startIndex. |
5258 |
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
|
5259 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5260 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5261 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5262 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5263 |
start to:1 by:-1 do:[:index | |
8874 | 5264 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5265 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5266 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5267 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5268 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5269 |
#(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
|
5270 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5271 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5272 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5273 |
|
8193 | 5274 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
5275 |
"find the last element, for which evaluation of the argument, aBlock |
|
5276 |
returns true. Start the search at startIndex. |
|
5277 |
End the search at endIndex or when an element is found. |
|
5278 |
Return its index or 0 if none detected." |
|
5279 |
||
5280 |
|start "{ Class: SmallInteger }"| |
|
5281 |
||
5282 |
start := startIndex. |
|
5283 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 5284 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 5285 |
]. |
5286 |
^ 0 |
|
5287 |
||
5288 |
" |
|
5289 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
5290 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
5291 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
5292 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
5293 |
" |
|
5294 |
! |
|
5295 |
||
159 | 5296 |
indexOfAny:aCollection |
5297 |
"search the collection for an element in aCollection. |
|
5298 |
if found, return the index otherwise return 0. |
|
8874 | 5299 |
The comparison is done using = |
159 | 5300 |
(i.e. equality test - not identity test). |
5301 |
||
5302 |
Notice, that for big collections, the runtime of this search |
|
5303 |
grows proportional to size(receiver) * size(aCollection). |
|
5304 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5305 |
||
5306 |
^ self indexOfAny:aCollection startingAt:1 |
|
5307 |
||
5308 |
" |
|
5309 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
5310 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
5311 |
" |
|
5312 |
! |
|
5313 |
||
5314 |
indexOfAny:aCollection startingAt:start |
|
5315 |
"search the collection for an element in aCollection, |
|
5316 |
starting the search at index start; |
|
5317 |
if found, return the index otherwise return 0. |
|
8874 | 5318 |
The comparison is done using = |
159 | 5319 |
(i.e. equality test - not identity test). |
5320 |
||
5321 |
Notice, that for big collections, the runtime of this search |
|
5322 |
grows proportional to size(receiver) * size(aCollection). |
|
5323 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5324 |
||
5325 |
||
5326 |
|startIndex "{ Class: SmallInteger }" |
|
5327 |
stop "{ Class: SmallInteger }" | |
|
5328 |
||
5329 |
startIndex := start. |
|
5330 |
stop := self size. |
|
5331 |
startIndex to:stop do:[:index | |
|
8874 | 5332 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 5333 |
]. |
5334 |
^ 0 |
|
5335 |
" |
|
8874 | 5336 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
5337 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 5338 |
" |
1 | 5339 |
! |
5340 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5341 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5342 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5343 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5344 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 5345 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5346 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5347 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5348 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5349 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5350 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5351 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5352 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5353 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5354 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5355 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5356 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5357 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5358 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5359 |
" |
8874 | 5360 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5361 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
5362 |
#(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
|
5363 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5364 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5365 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5366 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5367 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5368 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5369 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5370 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5371 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5372 |
#(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
|
5373 |
#(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
|
5374 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5375 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5376 |
|
8874 | 5377 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5378 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5379 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5380 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5381 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5382 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5383 |
#(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
|
5384 |
#(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
|
5385 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5386 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5387 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5388 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5389 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5390 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5391 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5392 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5393 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5394 |
|
368 | 5395 |
indexOfSubCollection:aCollection |
9234 | 5396 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 5397 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5398 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 5399 |
|
5400 |
" |
|
8874 | 5401 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
5402 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
5403 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
5404 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
5405 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
5406 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
5407 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
5408 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 5409 |
" |
5410 |
! |
|
5411 |
||
602 | 5412 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
5413 |
"find a subcollection. If found, return the index; |
|
5414 |
if not found, return the result of evaluating exceptionBlock." |
|
5415 |
||
5416 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
5417 |
! |
|
5418 |
||
368 | 5419 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5420 |
"find a subcollection starting at index. |
368 | 5421 |
If found, return the index; if not found, return 0." |
5422 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5423 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 5424 |
! |
5425 |
||
5426 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
5427 |
"find a subcollection, starting at index. If found, return the index; |
|
5428 |
if not found, return the result of evaluating exceptionBlock. |
|
5429 |
This is a q&d hack - not very efficient" |
|
5430 |
||
8874 | 5431 |
|same first |
368 | 5432 |
mySize "{Class: SmallInteger }" |
5433 |
checkIndex "{Class: SmallInteger }" |
|
5434 |
cmpIndex "{Class: SmallInteger }" |
|
5435 |
sz "{Class: SmallInteger }"| |
|
5436 |
||
5437 |
mySize := self size. |
|
5438 |
sz := aCollection size. |
|
5439 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
5440 |
first := aCollection at:1. |
|
5441 |
||
8874 | 5442 |
checkIndex := startIndex - 1. |
368 | 5443 |
[true] whileTrue:[ |
8874 | 5444 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
5445 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5446 |
||
5447 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
5448 |
^ 0 |
|
5449 |
]. |
|
5450 |
same := true. |
|
5451 |
cmpIndex := 1. |
|
5452 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5453 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
5454 |
same := false |
|
5455 |
] ifTrue:[ |
|
5456 |
cmpIndex := cmpIndex + 1 |
|
5457 |
] |
|
5458 |
]. |
|
5459 |
same ifTrue:[^ checkIndex]. |
|
5460 |
] |
|
5461 |
||
5462 |
" |
|
5463 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
5464 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
5465 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
5466 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
5467 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
5468 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 5469 |
" |
602 | 5470 |
! |
5471 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5472 |
lastIndexOfSubCollection:aCollection |
8874 | 5473 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5474 |
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
|
5475 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5476 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5477 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5478 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5479 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5480 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5481 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5482 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5483 |
"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
|
5484 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5485 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5486 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5487 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5488 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5489 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5490 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5491 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5492 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5493 |
"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
|
5494 |
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
|
5495 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5496 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5497 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5498 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5499 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5500 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5501 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5502 |
"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
|
5503 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5504 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5505 |
|
8874 | 5506 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5507 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5508 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5509 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5510 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5511 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5512 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5513 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5514 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5515 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5516 |
|
8874 | 5517 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5518 |
checkIndex > mySize ifTrue:[ |
8874 | 5519 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5520 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5521 |
[true] whileTrue:[ |
8874 | 5522 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
5523 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5524 |
||
5525 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
5526 |
^ 0 |
|
5527 |
]. |
|
5528 |
same := true. |
|
5529 |
cmpIndex := 1. |
|
5530 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5531 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
5532 |
same := false |
|
5533 |
] ifTrue:[ |
|
5534 |
cmpIndex := cmpIndex + 1 |
|
5535 |
] |
|
5536 |
]. |
|
5537 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
5538 |
] |
|
5539 |
||
5540 |
" |
|
5541 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
5542 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
5543 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
5544 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
5545 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
5546 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
5547 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
5548 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
5549 |
#(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
|
5550 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5551 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5552 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
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 |
|
7264 | 5555 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
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 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5558 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5559 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5560 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5561 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5562 |
((self indexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5563 |
^ true |
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 |
" |
8874 | 5566 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5567 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5568 |
returns: true - if found |
8874 | 5569 |
false - if not found |
5570 |
" |
|
5571 |
||
5572 |
" |
|
5573 |
#(10 20 30 40 50 60 70) includes:99 |
|
5574 |
#(10 20 30 40 50 60 70) includes:40 |
|
5575 |
#(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
|
5576 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5577 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5578 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5579 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5580 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5581 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5582 |
"search the collection for anElement; |
602 | 5583 |
if found, return the index otherwise return 0. |
8874 | 5584 |
The comparison is done using = |
602 | 5585 |
(i.e. equality test - not identity test)." |
5586 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5587 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5588 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5589 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5590 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5591 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5592 |
returns: elementIndex - if found |
8874 | 5593 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5594 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5597 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5598 |
#(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
|
5599 |
" |
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 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5604 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5605 |
"search the collection for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5606 |
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
|
5607 |
exceptionBlock. |
8874 | 5608 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5609 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5610 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5611 |
|index| |
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 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5614 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5615 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5616 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5617 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5618 |
args: anElement : <object> |
8874 | 5619 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 5622 |
exceptionBlock value - if not found |
5623 |
" |
|
5624 |
||
5625 |
" |
|
5626 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
5627 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
5628 |
#(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
|
5629 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5630 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5631 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5634 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 5635 |
"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
|
5636 |
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
|
5637 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5638 |
If not found, return 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 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5641 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5642 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5643 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 5644 |
self at:idx put:replacement. |
5645 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5646 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5647 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5648 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5649 |
" |
8874 | 5650 |
args: elementToFind : <object> |
5651 |
replacement : <integer> |
|
5652 |
start : <integer> |
|
5653 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found (and replaced) |
8874 | 5656 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5657 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5658 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5659 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5660 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5661 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5662 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5663 |
(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
|
5664 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5665 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5666 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5667 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5668 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5669 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5670 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5671 |
"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
|
5672 |
if found, return the index otherwise return 0. |
8874 | 5673 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5674 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5675 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5676 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5677 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5678 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5679 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5680 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5681 |
startIndex to:stop do:[:index | |
8874 | 5682 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5683 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5684 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5685 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5686 |
" |
8874 | 5687 |
args: anElement : <object> |
5688 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5689 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5690 |
returns: elementIndex - if found |
8874 | 5691 |
0 - if not found |
5692 |
" |
|
5693 |
||
5694 |
" |
|
5695 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
5696 |
#(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
|
5697 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5698 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5699 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5700 |
! |
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 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5703 |
"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
|
5704 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5705 |
If found (within the range), return the index, otherwise return 0. |
8874 | 5706 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5707 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5708 |
|
602 | 5709 |
|startIndex "{ Class: SmallInteger }" |
5710 |
stopIndex "{ Class: SmallInteger }" | |
|
5711 |
||
5712 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5713 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5714 |
startIndex to:stopIndex do:[:index | |
8874 | 5715 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5716 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5717 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5718 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5719 |
" |
8874 | 5720 |
args: anElement : <object> |
5721 |
start : <integer> |
|
5722 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 5725 |
exceptionBlock value - if not found |
5726 |
" |
|
5727 |
||
5728 |
" |
|
5729 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
5730 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
5731 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
5732 |
#(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
|
5733 |
" |
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 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5736 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5737 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5738 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5739 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5740 |
"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
|
5741 |
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
|
5742 |
exceptionBlock. |
8874 | 5743 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5744 |
(i.e. equality test - not identity test)." |
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 |
|index| |
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 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5749 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5750 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5751 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5752 |
" |
8874 | 5753 |
args: anElement : <object> |
5754 |
start : <integer> |
|
5755 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5756 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5757 |
returns: elementIndex - if found |
8874 | 5758 |
exceptionBlock value - if not found |
5759 |
" |
|
5760 |
||
5761 |
" |
|
5762 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
5763 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
5764 |
#(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
|
5765 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5766 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5767 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
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 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5771 |
"search the collection backwards for anElement; |
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 |
|
8874 | 5776 |
^ self lastIndexOf:anElement startingAt:self size |
3468
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5779 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5780 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5781 |
returns: elementIndex - if found |
8874 | 5782 |
0 - if not found |
5783 |
" |
|
5784 |
||
5785 |
" |
|
5786 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
5787 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
5788 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
5789 |
#(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
|
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 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5793 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5794 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5795 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5796 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5797 |
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
|
5798 |
exceptionBlock. |
8874 | 5799 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5800 |
(i.e. equality test - not identity test)." |
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| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5803 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5804 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5805 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5806 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5807 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5808 |
" |
8874 | 5809 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
5810 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
5811 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
5812 |
#(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
|
5813 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5814 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5815 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5816 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5817 |
"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
|
5818 |
if found, return the index otherwise return 0. |
8874 | 5819 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5820 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5821 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5822 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5823 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5824 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5825 |
startIndex to:1 by:-1 do:[:index | |
8874 | 5826 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 5827 |
]. |
5828 |
^ 0 |
|
5829 |
||
5830 |
" |
|
8874 | 5831 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
5832 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
5833 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
5834 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
5835 |
#(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
|
5836 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5837 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5838 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5839 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 5840 |
"search the collection backwards for anElement starting the search at |
5841 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5842 |
otherwise return the value of the exceptionBlock. |
8874 | 5843 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5844 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5845 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5846 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5847 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5848 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5849 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5850 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5851 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5852 |
" |
8874 | 5853 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
5854 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
5855 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
5856 |
#(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
|
5857 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5858 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5859 |
|
4788 | 5860 |
lastIndexOfAny:aCollection |
5861 |
"search the collection backwards for any in aCollection; |
|
5862 |
if found, return the index, otherwise return 0. |
|
8874 | 5863 |
The comparison is done using = |
4788 | 5864 |
(i.e. equality test - not identity test)." |
5865 |
||
8874 | 5866 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 5867 |
|
5868 |
" |
|
5869 |
args: aCollection : <collection> |
|
5870 |
||
5871 |
returns: elementIndex - if found |
|
8874 | 5872 |
0 - if not found |
5873 |
" |
|
5874 |
||
5875 |
" |
|
5876 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 5877 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 5878 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
5879 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 5880 |
" |
5881 |
||
5882 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
5883 |
! |
|
5884 |
||
5885 |
lastIndexOfAny:aCollection startingAt:start |
|
5886 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
5887 |
if found, return the index, otherwise return 0. |
|
8874 | 5888 |
The comparison is done using = |
4788 | 5889 |
(i.e. equality test - not identity test)." |
5890 |
||
5891 |
|startIndex "{ Class: SmallInteger }"| |
|
5892 |
||
5893 |
startIndex := start. |
|
5894 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 5895 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 5896 |
]. |
5897 |
^ 0 |
|
5898 |
||
5899 |
" |
|
8874 | 5900 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
5901 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 5902 |
" |
5903 |
! |
|
5904 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5905 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5906 |
"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
|
5907 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5908 |
if found, return the index otherwise return nil. |
8874 | 5909 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5910 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5911 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5912 |
NOTICE: nil is returned here if the element is not found, |
8874 | 5913 |
although this is somewhat strange (the indexOf-search |
5914 |
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
|
5915 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5916 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5917 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5918 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5919 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5920 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5921 |
startIndex to:stop do:[:index | |
8874 | 5922 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5923 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5924 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5925 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5926 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5927 |
args: anElement : <object> |
8874 | 5928 |
start : <integer> |
5929 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5930 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5931 |
returns: elementIndex - if found |
8874 | 5932 |
nil - if not found |
5933 |
" |
|
5934 |
||
5935 |
" |
|
5936 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
5937 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
5938 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 5939 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5940 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5941 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 5942 |
! |
5943 |
||
5944 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
5945 |
"search the collection for anElement, starting the search at index start |
|
5946 |
and stopping at stop; |
|
5947 |
if found, return the index otherwise return the value of the |
|
5948 |
exceptionBlock. |
|
8874 | 5949 |
The comparison is done using = |
602 | 5950 |
(i.e. equality test - not identity test)." |
5951 |
||
5952 |
|index| |
|
5953 |
||
5954 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5955 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 5956 |
^ index |
5957 |
||
5958 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5959 |
args: anElement : <object> |
8874 | 5960 |
start : <integer> |
5961 |
stop : <integer> |
|
5962 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5963 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5964 |
returns: elementIndex - if found |
8874 | 5965 |
valueImplementor value - if not found |
5966 |
" |
|
5967 |
||
5968 |
" |
|
5969 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
5970 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
5971 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 5972 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5973 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5974 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 5975 |
! |
5976 |
||
5977 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
5978 |
"search the collection for anElement, starting the search at index start; |
|
5979 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5980 |
If found (within the range), return the index, otherwise return nil. |
8874 | 5981 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5982 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5983 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5984 |
NOTICE: nil is returned here if the element is not found, |
8874 | 5985 |
although this is somewhat strange (the indexOf-search |
5986 |
methods return 0), this is done for VW compatibility." |
|
2275 | 5987 |
|
5988 |
|startIndex "{ Class: SmallInteger }" |
|
5989 |
stopIndex "{ Class: SmallInteger }" | |
|
5990 |
||
5991 |
startIndex := startSearchIndex. |
|
5992 |
stopIndex := endSearchIndex max:1. |
|
5993 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 5994 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 5995 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5996 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5997 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5998 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5999 |
args: anElement : <object> |
8874 | 6000 |
start : <integer> |
6001 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6002 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6003 |
returns: elementIndex - if found |
8874 | 6004 |
nil - if not found |
6005 |
" |
|
6006 |
||
6007 |
" |
|
6008 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6009 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6010 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6011 |
" |
6012 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6013 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6014 |
! |
6015 |
||
6016 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6017 |
"search the collection for anElement, starting the search at index start |
|
6018 |
and stopping at stop, doing a reverse search; |
|
6019 |
if found, return the index otherwise return the value of the |
|
6020 |
exceptionBlock. |
|
8874 | 6021 |
The comparison is done using = |
2275 | 6022 |
(i.e. equality test - not identity test)." |
6023 |
||
6024 |
|index| |
|
6025 |
||
6026 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6027 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6028 |
^ index |
|
6029 |
||
6030 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6031 |
args: anElement : <object> |
8874 | 6032 |
start : <integer> |
6033 |
stop : <integer> |
|
6034 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6035 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6036 |
returns: elementIndex - if found |
8874 | 6037 |
valueImplementor value - if not found |
6038 |
" |
|
6039 |
||
6040 |
" |
|
6041 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6042 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6043 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6044 |
" |
6045 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6046 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
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 |
|
7264 | 6049 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6050 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6051 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6052 |
"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
|
6053 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6054 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6055 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6056 |
|
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 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6059 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6060 |
returns: elementIndex - if found |
8874 | 6061 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6062 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6063 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6064 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6065 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6066 |
#(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
|
6067 |
#(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
|
6068 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6069 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6070 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6071 |
#(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
|
6072 |
#(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
|
6073 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6074 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6075 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6076 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6077 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6078 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6079 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6080 |
"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
|
6081 |
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
|
6082 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6083 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6084 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6085 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6086 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6087 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6088 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6089 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6090 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6091 |
args: anElement : <object> |
8874 | 6092 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6093 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6094 |
returns: elementIndex - if found |
8874 | 6095 |
exceptionBlock value - if not found |
6096 |
" |
|
6097 |
||
6098 |
" |
|
6099 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6100 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6101 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6102 |
#(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
|
6103 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6104 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6105 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6106 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6107 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6108 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6109 |
"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
|
6110 |
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
|
6111 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6112 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6113 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6114 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6115 |
args: anElement : <object> |
8874 | 6116 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6117 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6118 |
returns: elementIndex - if found |
8874 | 6119 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6120 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6121 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6122 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6123 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6124 |
"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
|
6125 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6126 |
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
|
6127 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6128 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6129 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6130 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6131 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6132 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6133 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6134 |
startIndex to:stop do:[:index | |
8874 | 6135 |
el := self at:index. |
6136 |
anElement == el ifTrue:[^ index]. |
|
6137 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6138 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6139 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6140 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6141 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6142 |
args: anElement : <object> |
8874 | 6143 |
anotherElement : <object> |
6144 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6145 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6146 |
returns: elementIndex - if found |
8874 | 6147 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6148 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6149 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6150 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6151 |
#(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
|
6152 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6153 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6154 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6155 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6156 |
"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
|
6157 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6158 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6159 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6160 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6161 |
stop "{ Class: SmallInteger }" | |
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 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6164 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6165 |
startIndex to:stop do:[:index | |
8874 | 6166 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6167 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6168 |
^ 0 |
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 |
args: anElement : <object> |
8874 | 6172 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6173 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6174 |
returns: elementIndex - if found |
8874 | 6175 |
0 - if not found |
3468
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 |
|
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 |
#(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
|
6180 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6181 |
#(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
|
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 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6185 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6186 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6187 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6188 |
"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
|
6189 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6190 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6191 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6192 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6193 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6194 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6195 |
stopIndex "{ Class: SmallInteger }" | |
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 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6198 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6199 |
startIndex to:stopIndex do:[:index | |
8874 | 6200 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6201 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6202 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6203 |
|
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 |
args: anElement : <object> |
8874 | 6206 |
start : <integer> |
6207 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6208 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6209 |
returns: elementIndex - if found |
8874 | 6210 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6211 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6212 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6213 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6214 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6215 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6216 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6217 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6218 |
"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
|
6219 |
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
|
6220 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6221 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6222 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6223 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6224 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6225 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6226 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6227 |
^ index |
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 |
args: anElement : <object> |
8874 | 6231 |
start : <integer> |
6232 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6233 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6234 |
returns: elementIndex - if found |
8874 | 6235 |
exceptionBlock value - if not found |
6236 |
" |
|
6237 |
||
6238 |
" |
|
6239 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6240 |
#(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
|
6241 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6242 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6243 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6244 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6245 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6246 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6247 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6248 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6249 |
See #includes: if equality is asked for." |
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 |
((self identityIndexOf:anElement startingAt:1) == 0) ifTrue:[^ false]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6252 |
^ true |
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 |
" |
8874 | 6255 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6256 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6257 |
returns: true - if found |
8874 | 6258 |
false - if not found |
6259 |
" |
|
6260 |
||
6261 |
" |
|
6262 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6263 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6264 |
#(10 20 30 40 50 60 70) includes:40 |
|
6265 |
#(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
|
6266 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6267 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6268 |
|
8874 | 6269 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6270 |
#(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
|
6271 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6272 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6273 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6274 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6275 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6276 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6277 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6278 |
if found, return the index otherwise return 0. |
8874 | 6279 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6280 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6281 |
|
8874 | 6282 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6283 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6284 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6285 |
args: anElement : <object> |
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 |
returns: elementIndex - if found |
8874 | 6288 |
0 - if not found |
6289 |
" |
|
6290 |
||
6291 |
" |
|
6292 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
6293 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
6294 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
6295 |
#(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
|
6296 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6297 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6298 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6299 |
! |
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 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6302 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6303 |
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
|
6304 |
exceptionBlock. |
8874 | 6305 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6306 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6307 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6308 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6309 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6310 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6311 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6312 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6313 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6314 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6315 |
args: anElement : <object> |
8874 | 6316 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6317 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6318 |
returns: elementIndex - if found |
8874 | 6319 |
exceptionBlock value - if not found |
6320 |
" |
|
6321 |
||
6322 |
" |
|
6323 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
6324 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
6325 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
6326 |
#(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
|
6327 |
" |
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 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6331 |
! |
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 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
"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
|
6335 |
if found, return the index otherwise return 0. |
8874 | 6336 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6337 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6338 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6339 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6340 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6341 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6342 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6343 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6344 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6345 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6346 |
|
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 |
args: anElement : <object> |
8874 | 6349 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6350 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6351 |
returns: elementIndex - if found |
8874 | 6352 |
0 - if not found |
6353 |
" |
|
6354 |
||
6355 |
" |
|
6356 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
6357 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
6358 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
6359 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
6360 |
#(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
|
6361 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6362 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6363 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6364 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6365 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6366 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6367 |
"search the collection backwards for anElement starting the search at |
6368 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6369 |
otherwise return the value of the exceptionBlock. |
8874 | 6370 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6371 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6372 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6373 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6374 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6375 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6376 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6377 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6378 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6379 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6380 |
args: anElement : <object> |
8874 | 6381 |
start : <integer> |
6382 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6383 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6384 |
returns: elementIndex - if found |
8874 | 6385 |
exceptionBlock value - if not found |
6386 |
" |
|
6387 |
||
6388 |
" |
|
6389 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
6390 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6391 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6392 |
#(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
|
6393 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6394 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6395 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6396 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 6397 |
! ! |
6398 |
||
6399 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
6400 |
||
6958 | 6401 |
detectFirstInOrder:sortBlock |
6402 |
"find the first element of the collection sorted with sortBlock" |
|
6403 |
||
6404 |
|selectedForNow start| |
|
6405 |
||
6406 |
start := true. |
|
6407 |
self do:[:element| |
|
8874 | 6408 |
start ifTrue:[ |
6409 |
start := false. |
|
6410 |
selectedForNow := element. |
|
6411 |
] ifFalse:[ |
|
6412 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
6413 |
selectedForNow := element. |
|
6414 |
]. |
|
6415 |
]. |
|
6958 | 6416 |
]. |
6417 |
||
6418 |
^ selectedForNow |
|
6419 |
||
6420 |
" |
|
8874 | 6421 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
6422 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
6423 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
6424 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 6425 |
" |
6426 |
! |
|
6427 |
||
5358 | 6428 |
mergeSort |
8874 | 6429 |
"sort the collection using a mergeSort algorithm. |
6430 |
The elements are compared using'<' |
|
5358 | 6431 |
i.e. they should offer a magnitude-like protocol. |
6432 |
||
6433 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 6434 |
the best possible for all situations |
5358 | 6435 |
See also #quickSort and #randomizedSort for other sort algorithms |
6436 |
with different worst- and average case behavior)" |
|
6437 |
||
6438 |
|stop| |
|
6439 |
||
6440 |
stop := self size. |
|
6441 |
(stop > 1) ifTrue:[ |
|
8874 | 6442 |
self mergeSort:[:a :b | a < b] from:1 to:stop |
5358 | 6443 |
] |
6444 |
||
6445 |
" |
|
6446 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
6447 |
||
6448 |
|data| |
|
6449 |
data := Random new next:100000. |
|
6450 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
6451 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 6452 |
data reverse. |
5358 | 6453 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
6454 |
||
6455 |
data := Random new next:100000. |
|
6456 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6457 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6458 |
data reverse. |
5358 | 6459 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6460 |
||
6461 |
data := Random new next:100000. |
|
6462 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
6463 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 6464 |
data reverse. |
5358 | 6465 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
6466 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6467 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6468 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6469 |
mergeSort:sortBlock |
8874 | 6470 |
"sort the collection using a mergeSort algorithm. |
6471 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6472 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6473 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6474 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 6475 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6476 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6477 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6478 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6479 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6480 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6481 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6482 |
(stop > 1) ifTrue:[ |
8874 | 6483 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6484 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6485 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6486 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6487 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6488 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6489 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6490 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6491 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6492 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6493 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6494 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6495 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6496 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6497 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6498 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 6499 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6500 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6501 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6502 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6503 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6504 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 6505 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6506 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6510 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 6511 |
"Sort the given range of indices using the mergesort algorithm. |
6512 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 6513 |
as many comparisons as heapsort or quicksort." |
6514 |
||
8874 | 6515 |
"Details: recursively split the range to be sorted into two halves, |
6516 |
mergesort each half, then merge the two halves together. |
|
6517 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
6518 |
and forth between the receiver and this copy. |
|
6519 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 6520 |
resulting in the receiver being completely sorted." |
6521 |
||
6522 |
| temp | |
|
6523 |
||
6524 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
6525 |
startIndex = stopIndex ifTrue: [^ self]. |
|
6526 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 6527 |
ifFalse: [self error: 'bad start index']. |
5358 | 6528 |
stopIndex <= self size |
8874 | 6529 |
ifFalse: [self error: 'bad stop index']. |
5358 | 6530 |
temp := self clone. |
6531 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
6532 |
||
6533 |
" |
|
8874 | 6534 |
#(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
|
6535 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6536 |
|
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 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6539 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6540 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6541 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6542 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6543 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6544 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6545 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6546 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6547 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6548 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6549 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 6550 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6551 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6552 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6553 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6554 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6555 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6556 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6557 |
'<' i.e. they should offer a magnitude-like protocol. |
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 |
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
|
6560 |
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
|
6561 |
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
|
6562 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6563 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6564 |
|stop| |
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 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6567 |
(stop > 1) ifTrue:[ |
8874 | 6568 |
self quickSortFrom:1 to:stop |
7636
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6571 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6572 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 6573 |
|
6574 |
|data| |
|
6575 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6576 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6577 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 6578 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6579 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6580 |
" |
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 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6583 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6584 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6585 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6586 |
"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
|
6587 |
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
|
6588 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6589 |
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
|
6590 |
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
|
6591 |
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
|
6592 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6593 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6594 |
|stop| |
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 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6597 |
(stop > 1) ifTrue:[ |
8874 | 6598 |
sortBlock numArgs == 3 ifTrue:[ |
6599 |
"/ TODO: pass a collating policy to aBlock |
|
6600 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
6601 |
] ifFalse:[ |
|
6602 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
6603 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6604 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6605 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6606 |
" |
8874 | 6607 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6608 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6609 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6610 |
#(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
|
6611 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6612 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6613 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
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 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6617 |
"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
|
6618 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6619 |
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
|
6620 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6621 |
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
|
6622 |
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
|
6623 |
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
|
6624 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6625 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6626 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6627 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6628 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6629 |
(stop > 1) ifTrue:[ |
8874 | 6630 |
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
|
6631 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6632 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6633 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6634 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6635 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6636 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6637 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6638 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6639 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6640 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6641 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6642 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6643 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6644 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6645 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6646 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6647 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6648 |
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
|
6649 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6650 |
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
|
6651 |
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
|
6652 |
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
|
6653 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6654 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6655 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6656 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6657 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6658 |
(stop > 1) ifTrue:[ |
8874 | 6659 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6660 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6661 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6662 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 6663 |
! |
6664 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6665 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6666 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6667 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6668 |
|
8874 | 6669 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6670 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6671 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6672 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6673 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6674 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6675 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6676 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6677 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6678 |
"/ 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
|
6679 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6680 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6681 |
"/ 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
|
6682 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6683 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6684 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6685 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6686 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6687 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6688 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6689 |
|
8874 | 6690 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6691 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6692 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6693 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6694 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6695 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6696 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6697 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6698 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6699 |
"/ 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
|
6700 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6701 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6702 |
"/ 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
|
6703 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6704 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6705 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6706 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6707 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6708 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6709 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6710 |
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
|
6711 |
|
8874 | 6712 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6713 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6714 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6715 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6716 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6717 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6718 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6719 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6720 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6721 |
"/ 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
|
6722 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6723 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6724 |
"/ 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
|
6725 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6726 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6727 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6728 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6729 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6730 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6731 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6732 |
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
|
6733 |
|
8874 | 6734 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6735 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6736 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6737 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6738 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6739 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6740 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6741 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6742 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6743 |
"/ 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
|
6744 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6745 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6746 |
"/ 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
|
6747 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6748 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6749 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6750 |
|
602 | 6751 |
reverse |
7335 | 6752 |
"destructively reverse the order of the elements inplace. |
6753 |
WARNING: this is a destructive operation, which modifies the receiver." |
|
602 | 6754 |
|
6755 |
|lowIndex "{ Class: SmallInteger }" |
|
6756 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6757 |
t1 t2| |
602 | 6758 |
|
6759 |
hiIndex := self size. |
|
6760 |
lowIndex := 1. |
|
6761 |
[lowIndex < hiIndex] whileTrue:[ |
|
8874 | 6762 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
6763 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
6764 |
||
6765 |
lowIndex := lowIndex + 1. |
|
6766 |
hiIndex := hiIndex - 1 |
|
602 | 6767 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6768 |
|
69 | 6769 |
" |
602 | 6770 |
#(4 5 6 7 7) reverse |
6771 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 6772 |
" |
1 | 6773 |
! |
6774 |
||
5004 | 6775 |
reversed |
6776 |
"return a copy with elements in reverse order" |
|
6777 |
||
6778 |
^ self copy reverse |
|
6779 |
||
6780 |
" |
|
8874 | 6781 |
#(4 5 6 7 7) reversed |
5004 | 6782 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
6783 |
" |
|
6784 |
! |
|
6785 |
||
1 | 6786 |
sort |
6787 |
"sort the collection inplace. The elements are compared using |
|
2069 | 6788 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6789 |
|
159 | 6790 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6791 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6792 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6793 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 6794 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6795 |
self quickSort |
1 | 6796 |
|
95 | 6797 |
" |
6798 |
#(1 16 7 98 3 19 4 0) sort |
|
6799 |
||
6800 |
|data| |
|
6801 |
data := Random new next:100000. |
|
6802 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6803 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6804 |
data reverse. |
95 | 6805 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6806 |
" |
|
2069 | 6807 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6808 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 6809 |
! |
6810 |
||
6811 |
sort:sortBlock |
|
6812 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6813 |
for comparison. This allows any sort criteria to be implemented. |
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)" |
1 | 6819 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6820 |
self quickSort:sortBlock |
1 | 6821 |
|
95 | 6822 |
" |
8874 | 6823 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6824 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6825 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6826 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 6827 |
" |
2069 | 6828 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
6829 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 6830 |
! |
6831 |
||
6832 |
sort:sortBlock with:aCollection |
|
6833 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 6834 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6835 |
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
|
6836 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6837 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6838 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6839 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6840 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6841 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6842 |
self quickSort:sortBlock with:aCollection |
1 | 6843 |
|
69 | 6844 |
" |
6845 |
|c1 c2| |
|
1 | 6846 |
c1 := #(1 16 7 9). |
6847 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
6848 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 6849 |
c1 printNL. |
6850 |
c2 printNL |
|
69 | 6851 |
" |
2069 | 6852 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6853 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 6854 |
! |
6855 |
||
602 | 6856 |
sortWith:aCollection |
2069 | 6857 |
"sort the receiver collection inplace, using '<' to compare elements. |
6858 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6859 |
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
|
6860 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6861 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6862 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6863 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6864 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 6865 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6866 |
self quickSortWith:aCollection |
424 | 6867 |
! |
6868 |
||
9072 | 6869 |
sortedBy: aBlock |
6870 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
|
6871 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
6872 |
||
6873 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
6874 |
! |
|
6875 |
||
602 | 6876 |
topologicalSort:sortBlock |
6877 |
"sort the collection inplace using a sloooow sort algorithm. |
|
6878 |
This algorithm has O-square runtime behavior and should be used only |
|
6879 |
in special situations. |
|
6880 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
6881 |
a>c (for example, to sort classes by inheritance) |
|
6882 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6883 |
In all other situations, use #sort; which implements the quicksort algorithm. |
77 | 6884 |
" |
602 | 6885 |
|
6886 |
|smallestIndex "{ Class: SmallInteger }" |
|
6887 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6888 |
index2 "{ Class: SmallInteger }" |
602 | 6889 |
smallest thisOne| |
6890 |
||
6891 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
6892 |
||
6893 |
end := self size. |
|
862 | 6894 |
1 to:end-1 do:[:index | |
8874 | 6895 |
smallest := self at:index. |
6896 |
smallestIndex := index. |
|
6897 |
||
6898 |
index2 := index + 1. |
|
6899 |
[ |
|
6900 |
thisOne := self at:index2. |
|
6901 |
(index2 ~~ smallestIndex |
|
6902 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
6903 |
smallestIndex := index2. |
|
6904 |
smallest := thisOne. |
|
6905 |
index2 := index + 1. |
|
6906 |
] ifFalse:[ |
|
6907 |
index2 := index2 + 1. |
|
6908 |
]. |
|
6909 |
] doWhile:[index2 <= end]. |
|
862 | 6910 |
|
6911 |
"/ (index + 1) to:end do:[:index2 | |
|
6912 |
"/ thisOne := self at:index2. |
|
6913 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
6914 |
"/ smallestIndex := index2. |
|
6915 |
"/ smallest := thisOne |
|
6916 |
"/ ]. |
|
6917 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6918 |
|
8874 | 6919 |
(smallestIndex ~~ index) ifTrue:[ |
6920 |
thisOne := self at:index. |
|
6921 |
self at:index put:smallest. |
|
6922 |
self at:smallestIndex put:thisOne |
|
6923 |
]. |
|
1 | 6924 |
] |
95 | 6925 |
|
77 | 6926 |
" |
8874 | 6927 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
6928 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
6929 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
6930 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 6931 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6932 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6933 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 6934 |
! ! |
602 | 6935 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6936 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6937 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6938 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6939 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6940 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 6941 |
however, having a common protocol with indexed collections |
6942 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6943 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6944 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6945 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6946 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6947 |
" |
8874 | 6948 |
#(1 2 3) includesKey:4 |
6949 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6950 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6951 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6952 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6953 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6954 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6955 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6956 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6957 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6958 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
6959 |
|
2069 | 6960 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 6961 |
|
6962 |
version |
|
9358 | 6963 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.235 2006-05-09 09:44:30 cg Exp $' |
628 | 6964 |
! ! |
6820 | 6965 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
6966 |
SequenceableCollection initialize! |