author | Stefan Vogel <sv@exept.de> |
Tue, 13 Oct 2009 11:55:18 +0200 | |
changeset 12252 | 93c323c8a166 |
parent 12187 | d372059ced22 |
child 12289 | 7fcbb93d365e |
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 |
" |
|
5358 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Collection subclass:#SequenceableCollection |
6183 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'MissingClassInLiteralArrayErrorSignal' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Abstract' |
|
1 | 19 |
! |
20 |
||
2069 | 21 |
!SequenceableCollection class methodsFor:'documentation'! |
88 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
8874 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
11272 | 39 |
SequenceableCollections have ordered elements which can be accessed via a numeric index. |
40 |
SequenceableCollection is an abstract class - there are no direct instances of it in the system. |
|
1385 | 41 |
|
42 |
The methods found here assume that implementations of (at least) |
|
11272 | 43 |
#at:/#at:put: are implemented (which are always found in Object for indexable objects). |
44 |
||
45 |
For performance, some subclasses redefine more methods, |
|
46 |
knowing the detail of how elements are stored. |
|
47 |
||
8874 | 48 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
11272 | 49 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this kind of tuneup. |
50 |
||
51 |
The most heavily used SequentialCollections in the system are probably Array, String, ByteArray |
|
52 |
OrderedCollection and SortedCollection. |
|
1289 | 53 |
|
54 |
[author:] |
|
11272 | 55 |
Claus Gittinger |
1385 | 56 |
|
57 |
[see also:] |
|
11272 | 58 |
Array ByteArray OrderedCollection SortedCollection |
59 |
CharacterArray String |
|
88 | 60 |
" |
61 |
! ! |
|
1 | 62 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
63 |
!SequenceableCollection class methodsFor:'initialization'! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
64 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
65 |
initialize |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
66 |
MissingClassInLiteralArrayErrorSignal isNil ifTrue:[ |
8874 | 67 |
MissingClassInLiteralArrayErrorSignal := Error newSignalMayProceed:true. |
68 |
MissingClassInLiteralArrayErrorSignal nameClass:self message:#missingClassInLiteralArrayErrorSignal. |
|
69 |
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
|
70 |
] |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
71 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
72 |
"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
|
73 |
! ! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
74 |
|
2069 | 75 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 76 |
|
4807 | 77 |
decodeFromLiteralArray:anArray |
78 |
"create & return a new instance from information encoded in anArray. |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
79 |
Redefined for faster creation." |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
80 |
|
8874 | 81 |
|collection |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
82 |
sz "{ Class: SmallInteger }"| |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
83 |
|
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
84 |
sz := anArray size. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
85 |
collection := self withSize:sz-1. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
86 |
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
|
87 |
^ collection |
4807 | 88 |
|
8797 | 89 |
" |
90 |
Array |
|
8874 | 91 |
decodeFromLiteralArray:#(Array 1 2 3) |
8797 | 92 |
" |
4807 | 93 |
! |
94 |
||
1 | 95 |
new:size withAll:element |
360 | 96 |
"return a new collection of size, where all elements are |
97 |
initialized to element." |
|
1 | 98 |
|
99 |
|newCollection| |
|
100 |
||
101 |
newCollection := self new:size. |
|
102 |
newCollection atAllPut:element. |
|
103 |
^ newCollection |
|
360 | 104 |
! |
105 |
||
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
106 |
newWithConcatenationOfAll:aCollectionOfCollections |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
107 |
"this creates and returns a new collection consisting |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
by avoiding repeated garbage allocations. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
112 |
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
|
113 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
114 |
|totalSize newColl idx| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
115 |
|
8874 | 116 |
totalSize := aCollectionOfCollections |
117 |
inject:0 |
|
118 |
into:[:sumSoFar :el | sumSoFar + el size]. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
119 |
newColl := self new:totalSize. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
120 |
idx := 1. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
121 |
aCollectionOfCollections do:[:el | |
8874 | 122 |
|sz| |
123 |
||
124 |
sz := el size. |
|
125 |
newColl replaceFrom:idx to:(idx+sz-1) with:el startingAt:1. |
|
126 |
idx := idx + sz |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
127 |
]. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
128 |
^ newColl |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
129 |
|
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 |
this method optimizes the following (common) operation: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
132 |
|
8874 | 133 |
|s| |
134 |
||
135 |
s := ''. |
|
136 |
#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') do:[:e| |
|
137 |
s := s , e. |
|
138 |
]. |
|
139 |
s |
|
140 |
||
141 |
String |
|
142 |
newWithConcatenationOfAll:#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') |
|
143 |
||
144 |
String |
|
145 |
newWithConcatenationOfAll:#() |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
146 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
147 |
Array |
8874 | 148 |
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
|
149 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
150 |
timing: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
151 |
------- |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
152 |
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
|
153 |
for more elements, #newWithConcatenationOfAll: is much faster. |
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 |
|arr1 arr2 arr3 t| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
156 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
157 |
arr1 := #('hello' ' ' 'world' ' ' 'this is ' 'ST/X'). |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
158 |
arr2 := Array new:1000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
159 |
arr3 := Array new:10000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
160 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
161 |
(Array with:arr1 with:arr2 with:arr3) with:#(10000 100 10) do:[:arr :cnt | |
8874 | 162 |
t := Time millisecondsToRun:[ |
163 |
cnt timesRepeat:[ |
|
164 |
String |
|
165 |
newWithConcatenationOfAll:arr |
|
166 |
] |
|
167 |
]. |
|
168 |
Transcript showCR:(arr size printString , ' elements - time for #newWithConcatenationOfAll :' , (t/cnt) asFloat printString , 'mS'). |
|
169 |
Transcript endEntry. |
|
170 |
||
171 |
t := Time millisecondsToRun:[ |
|
172 |
cnt timesRepeat:[ |
|
173 |
|s| |
|
174 |
||
175 |
s := ''. |
|
176 |
arr do:[:e| |
|
177 |
s := s , e. |
|
178 |
]. |
|
179 |
s |
|
180 |
] |
|
181 |
]. |
|
182 |
Transcript showCR:(arr size printString , ' elements - time for loop over #, :' , (t/cnt) asFloat printString , 'mS'). |
|
183 |
Transcript endEntry. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
184 |
] |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
185 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
186 |
! |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
187 |
|
360 | 188 |
withSize:size |
189 |
"return a new collection of size. |
|
190 |
For variable size collections, this is different from #new:, |
|
191 |
in that #new: creates an empty collection with preallocated size, |
|
192 |
while #withSize: creates a non empty one." |
|
193 |
||
9162 | 194 |
^ (self new:size) grow:size. |
360 | 195 |
|
196 |
" |
|
197 |
(OrderedCollection new:10) inspect. |
|
198 |
(OrderedCollection withSize:10) inspect. |
|
199 |
(Array new:10) inspect. |
|
200 |
(Array withSize:10) inspect. |
|
201 |
" |
|
1 | 202 |
! ! |
203 |
||
7304 | 204 |
!SequenceableCollection class methodsFor:'Compatibility-Squeak'! |
4370 | 205 |
|
6820 | 206 |
writeStream |
207 |
"create a write-stream on an instance of the receiver-class" |
|
208 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
209 |
^ WriteStream on:(self new:50). |
8220 | 210 |
|
211 |
" |
|
11242 | 212 |
OrderedCollection writeStream |
8220 | 213 |
" |
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
214 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
215 |
"Modified: / 29-03-2007 / 15:20:02 / cg" |
4370 | 216 |
! ! |
217 |
||
5188 | 218 |
!SequenceableCollection class methodsFor:'Signal constants'! |
219 |
||
220 |
missingClassInLiteralArrayErrorSignal |
|
221 |
^ MissingClassInLiteralArrayErrorSignal |
|
222 |
||
223 |
"Created: / 18.5.1999 / 14:50:04 / cg" |
|
224 |
! ! |
|
225 |
||
7304 | 226 |
!SequenceableCollection class methodsFor:'instance creation-multiDimensional'! |
7255 | 227 |
|
228 |
_at:nIndices |
|
229 |
"this is a synthetic selector, generated by the compiler, |
|
230 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 231 |
I.e. |
232 |
Array[n] |
|
7255 | 233 |
generates |
8874 | 234 |
Array _at: n |
7255 | 235 |
" |
236 |
||
237 |
^ self new:nIndices |
|
238 |
! |
|
239 |
||
240 |
_at:dim1 at:dim2 |
|
241 |
"this is a synthetic selector, generated by the compiler, |
|
242 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 243 |
I.e. |
244 |
Array[n,m] |
|
7255 | 245 |
generates |
8874 | 246 |
Array _at:n at:m |
7255 | 247 |
" |
248 |
||
249 |
|data| |
|
250 |
||
251 |
data := self withSize:(dim1 * dim2). |
|
252 |
^ MultiDimensionalArrayAccessor |
|
8874 | 253 |
collection:data |
254 |
dimensions:(Array with:dim1 with:dim2) |
|
7255 | 255 |
! |
256 |
||
257 |
_at:dim1 at:dim2 at:dim3 |
|
258 |
"this is a synthetic selector, generated by the compiler, |
|
259 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 260 |
I.e. |
261 |
Array[n,m,o] |
|
7255 | 262 |
generates |
8874 | 263 |
Array _at:n at:m at:o |
7255 | 264 |
" |
265 |
||
266 |
|data| |
|
267 |
||
268 |
data := self withSize:(dim1 * dim2 * dim3). |
|
8874 | 269 |
^ MultiDimensionalArrayAccessor |
270 |
collection:data |
|
271 |
dimensions:(Array with:dim1 with:dim2 with:dim3) |
|
7255 | 272 |
! ! |
273 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
274 |
!SequenceableCollection class methodsFor:'instance creation-streaming'! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
275 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
276 |
streamContents:blockWithArg |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
277 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
278 |
evaluate blockWithArg, passing that stream, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
279 |
extract and return the streams contents." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
280 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
281 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
282 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
283 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
284 |
blockWithArg value:stream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
285 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
286 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
287 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
288 |
|rslt| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
289 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
290 |
rslt := String streamContents:[:s | s nextPutAll:'hello'; space; nextPutAll:'world'] |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
291 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
292 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
293 |
"Modified: / 29-03-2007 / 15:20:20 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
294 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
295 |
|
10590 | 296 |
streamContents:blockWithArg limitedTo:limit |
297 |
"create a limited write-stream on an instance of the receiver-class, |
|
298 |
evaluate blockWithArg, passing that stream, |
|
299 |
extract and return the streams contents (possibly truncated)." |
|
300 |
||
301 |
|stream| |
|
302 |
||
303 |
stream := self writeStream. |
|
304 |
stream writeLimit:limit. |
|
305 |
blockWithArg value:stream. |
|
306 |
^ stream contents |
|
307 |
||
308 |
"Created: / 04-06-2007 / 17:22:38 / cg" |
|
309 |
! |
|
310 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
311 |
streamContentsEnumerating:aReceiver using:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
312 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
313 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
314 |
and return the collected values. Especially useful, if you have a do-like |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
315 |
enumerator somewhere, and you want this as a collection." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
316 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
317 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
318 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
319 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
320 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
321 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
322 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
323 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
324 |
String streamContentsEnumerating:'hello' using:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
325 |
Array streamContentsEnumerating:Method using:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
326 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
327 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
328 |
"Created: / 29-03-2007 / 15:21:12 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
329 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
330 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
331 |
streamContentsOf:aReceiver enumerator:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
332 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
333 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
334 |
and return the collected values. Especially useful, if you have a do-like |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
335 |
enumerator somewhere, and you want this as a collection." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
336 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
337 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
338 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
339 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
340 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
341 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
342 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
343 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
344 |
String streamContentsOf:'hello' enumerator:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
345 |
Array streamContentsOf:Method enumerator:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
346 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
347 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
348 |
"Created: / 29-03-2007 / 15:05:30 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
349 |
! ! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
350 |
|
8893 | 351 |
!SequenceableCollection class methodsFor:'queries'! |
352 |
||
353 |
isAbstract |
|
11219 | 354 |
"Return if this class is an abstract class. |
355 |
True is returned for SequenceableCollection here; false for subclasses. |
|
356 |
Abstract subclasses must redefine again." |
|
357 |
||
8893 | 358 |
^ self == SequenceableCollection |
359 |
! ! |
|
360 |
||
7261 | 361 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 362 |
|
7796 | 363 |
allButFirst |
8874 | 364 |
"Return a copy of the receiver containing all but the first element. |
7796 | 365 |
Raise an error if there are not enough elements." |
366 |
||
367 |
^ self copyFrom:2 |
|
8571 | 368 |
|
369 |
" |
|
370 |
'1234567890' allButFirst |
|
371 |
" |
|
372 |
! |
|
373 |
||
374 |
allButFirst:n |
|
8874 | 375 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 376 |
Raise an error if there are not enough elements." |
377 |
||
378 |
^ self copyFrom:n+1 |
|
379 |
||
380 |
" |
|
381 |
'1234567890' allButFirst:5 |
|
382 |
" |
|
7796 | 383 |
! |
384 |
||
6149 | 385 |
allButLast |
8874 | 386 |
"Return a copy of the receiver containing all but the last element. |
7796 | 387 |
Raise an error if there are not enough elements." |
6183 | 388 |
|
389 |
^ self allButLast:1 |
|
390 |
||
8571 | 391 |
" |
392 |
'1234567890' allButLast |
|
393 |
" |
|
394 |
||
6183 | 395 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
396 |
! |
|
397 |
||
398 |
allButLast:n |
|
8874 | 399 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 400 |
Raise an error if there are not enough elements." |
6183 | 401 |
|
402 |
^ self copyFrom: 1 to: self size - n |
|
403 |
||
8571 | 404 |
" |
405 |
'1234567890' allButLast:5 |
|
406 |
" |
|
407 |
||
6183 | 408 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 409 |
! |
410 |
||
8874 | 411 |
atPin: index |
4414 | 412 |
"Return the index'th element of me if possible. |
413 |
Return the first or last element if index is out of bounds." |
|
414 |
||
415 |
index < 1 ifTrue:[^ self first]. |
|
416 |
index > self size ifTrue:[^ self last]. |
|
417 |
^ self at:index |
|
418 |
||
419 |
" |
|
8874 | 420 |
#(1 2 3) atPin:4 |
421 |
#(1 2 3) atPin:0 |
|
422 |
#(1 2 3) atPin:1 |
|
423 |
#(1 2 3) atPin:3 |
|
424 |
" |
|
425 |
! |
|
426 |
||
427 |
atRandom |
|
4459 | 428 |
"Return any random element from the receiver" |
429 |
||
8573 | 430 |
^ self atRandom:Random |
431 |
||
432 |
" |
|
8874 | 433 |
#(1 2 3) atRandom |
434 |
" |
|
435 |
! |
|
436 |
||
437 |
atRandom:aRandomGenerator |
|
8573 | 438 |
"Return any random element from the receiver" |
439 |
||
4459 | 440 |
|max idx| |
441 |
||
442 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 443 |
self emptyCollectionError |
4459 | 444 |
]. |
8573 | 445 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 446 |
^ self at:idx |
447 |
||
448 |
" |
|
8874 | 449 |
#(1 2 3) atRandom:(Random new) |
450 |
" |
|
451 |
! |
|
452 |
||
453 |
atWrap:index |
|
4975 | 454 |
"Return the index'th element of me if possible. |
455 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
456 |
||
457 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
458 |
||
459 |
" |
|
8874 | 460 |
#(1 2 3) atWrap:1 |
461 |
#(1 2 3) atWrap:2 |
|
462 |
#(1 2 3) atWrap:3 |
|
463 |
#(1 2 3) atWrap:4 |
|
464 |
#(1 2 3) atWrap:5 |
|
465 |
#(1 2 3) atWrap:6 |
|
466 |
#(1 2 3) atWrap:7 |
|
467 |
#(1 2 3) atWrap:0 |
|
468 |
#(1 2 3) atWrap:-1 |
|
4975 | 469 |
" |
5372 | 470 |
! |
471 |
||
472 |
beginsWith:aCollection |
|
6183 | 473 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 474 |
|
475 |
^ self startsWith:aCollection |
|
6183 | 476 |
|
477 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 478 |
! |
479 |
||
8611 | 480 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 481 |
"squeak uses a different naming here - sigh. |
482 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
483 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
484 |
this method first takes a slice (from:to:) and then collects over that. |
|
485 |
I.e. it is equivalent to: |
|
8874 | 486 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 487 |
|
488 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
489 |
! |
|
490 |
||
7012 | 491 |
copyAfter: anElement |
8874 | 492 |
"Answer a copy of the receiver from after the first occurrence |
493 |
of anElement up to the end. If no such element exists, answer |
|
494 |
an empty copy." |
|
7012 | 495 |
|idx| |
496 |
||
497 |
idx := self indexOf:anElement. |
|
498 |
idx == 0 ifTrue:[idx := self size]. |
|
499 |
^ self copyFrom:idx + 1 |
|
500 |
||
501 |
" |
|
8874 | 502 |
'hello world' copyAfter:$l |
503 |
'123456123456' copyAfter:$2 |
|
504 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
505 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
506 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 507 |
" |
508 |
! |
|
509 |
||
6982 | 510 |
copyAfterLast:element |
8874 | 511 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 512 |
of element to the end; uses = for comparison" |
513 |
||
514 |
|idx| |
|
515 |
||
516 |
idx := self lastIndexOf:element. |
|
517 |
idx == 0 ifTrue:[idx := self size]. |
|
518 |
^ self copyFrom:idx + 1 |
|
519 |
||
520 |
" |
|
8874 | 521 |
'hello world' copyAfterLast:$l |
522 |
'123456123456' copyAfterLast:$2 |
|
523 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
524 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
525 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 526 |
" |
527 |
! |
|
528 |
||
6980 | 529 |
copyUpToLast:element |
8874 | 530 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 531 |
of element; uses = for comparison" |
532 |
||
533 |
|idx| |
|
534 |
||
535 |
idx := self lastIndexOf:element. |
|
536 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
537 |
^ self copyTo:idx - 1 |
|
538 |
||
539 |
" |
|
8874 | 540 |
'hello world' copyUpToLast:$l |
541 |
'123456123456' copyUpToLast:$2 |
|
542 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
543 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
544 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 545 |
" |
546 |
! |
|
547 |
||
8276 | 548 |
copyWithoutAll:elementsToSkip |
549 |
"return a new collection consisting of a copy of the receiver, with |
|
550 |
ALL elements equal to any in elementsToSkip are left out. |
|
551 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
552 |
||
553 |
|n "{ Class: SmallInteger }" |
|
554 |
sz "{ Class: SmallInteger }" |
|
555 |
srcIndex "{ Class: SmallInteger }" |
|
556 |
dstIndex "{ Class: SmallInteger }" |
|
557 |
skipIndex "{ Class: SmallInteger }" |
|
558 |
copy l| |
|
559 |
||
8874 | 560 |
"the code below may look like overkill, |
8276 | 561 |
however, for big collections its better to move data |
562 |
around in big chunks" |
|
563 |
||
564 |
n := self occurrencesOfAny:elementsToSkip. |
|
565 |
n == 0 ifTrue:[^ self copy]. |
|
566 |
||
567 |
sz := self size. |
|
568 |
copy := self copyEmptyAndGrow:(sz - n). |
|
569 |
||
570 |
srcIndex := 1. |
|
571 |
dstIndex := 1. |
|
572 |
||
573 |
n timesRepeat:[ |
|
8874 | 574 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
575 |
l := skipIndex - srcIndex. |
|
576 |
l ~~ 0 ifTrue:[ |
|
577 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
578 |
with:self startingAt:srcIndex. |
|
579 |
dstIndex := dstIndex + l |
|
580 |
]. |
|
581 |
srcIndex := skipIndex + 1 |
|
8276 | 582 |
]. |
583 |
l := sz - srcIndex. |
|
584 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 585 |
with:self startingAt:srcIndex. |
8276 | 586 |
^ copy |
587 |
||
588 |
" |
|
589 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 590 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 591 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
592 |
" |
|
593 |
! |
|
594 |
||
7304 | 595 |
copyWithoutFirst |
596 |
^ self copyFrom:2 |
|
597 |
! |
|
598 |
||
12051 | 599 |
shuffled |
600 |
"return a randomly shuffled copy of the receiver" |
|
601 |
||
602 |
^ self shuffledBy:Random |
|
603 |
||
604 |
" |
|
605 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
606 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
607 |
" |
|
608 |
! |
|
609 |
||
610 |
shuffledBy:aRandom |
|
611 |
"return a randomly shuffled copy of the receiver, using the given random generator" |
|
612 |
||
613 |
|copy| |
|
614 |
||
615 |
copy := self shallowCopy. |
|
616 |
copy size to: 1 by: -1 do:[:i | |
|
617 |
copy swap: i with: ((1 to: i) atRandom: aRandom) |
|
618 |
]. |
|
619 |
^ copy |
|
620 |
||
621 |
" |
|
622 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
623 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
624 |
" |
|
625 |
! |
|
626 |
||
11279 | 627 |
sortBy:aBlock |
628 |
"Sort inplace - destructive" |
|
629 |
||
630 |
self sort:aBlock |
|
631 |
||
632 |
"Created: / 22-10-2008 / 21:25:35 / cg" |
|
4414 | 633 |
! ! |
634 |
||
7261 | 635 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 636 |
|
637 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
638 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
639 |
! ! |
|
640 |
||
1 | 641 |
!SequenceableCollection methodsFor:'accessing'! |
642 |
||
1366 | 643 |
after:anObject |
644 |
"return the element, after anObject. |
|
5231 | 645 |
If anObject is not in the receiver, report an error; |
646 |
if anObject is the last in the receiver, return nil. |
|
1366 | 647 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
648 |
||
649 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
650 |
||
651 |
" |
|
8874 | 652 |
#(4 3 2 1) asOrderedCollection after:3. |
653 |
#(4 3 2 1) asOrderedCollection after:5 |
|
654 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 655 |
" |
656 |
||
657 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
658 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
659 |
! |
|
660 |
||
661 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 662 |
"return the element, after anObject. |
5231 | 663 |
If anObject is the last in the receiver, return nil. |
664 |
If there is no such element (anObject is not found), |
|
1366 | 665 |
return the value from exceptionBlock." |
666 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
667 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
668 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
669 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
670 |
idx ~~ 0 ifTrue:[ |
8874 | 671 |
idx == self size ifTrue:[^ nil]. |
672 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
673 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
674 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
675 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
676 |
" |
8874 | 677 |
#(4 3 2 1) after:3 ifAbsent:nil. |
678 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
679 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
680 |
(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
|
681 |
" |
1366 | 682 |
! |
683 |
||
6967 | 684 |
anElement |
8874 | 685 |
"return any element from the collection, |
6967 | 686 |
report an error if there is none" |
687 |
||
688 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
689 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
690 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
691 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
692 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
693 |
" |
6967 | 694 |
! |
695 |
||
32 | 696 |
at:index ifAbsent:exceptionBlock |
8874 | 697 |
"return the element at index if valid. |
698 |
If the index is invalid, return the result of evaluating |
|
302 | 699 |
the exceptionblock. |
8874 | 700 |
NOTICE: |
701 |
in ST-80, this message is only defined for Dictionaries, |
|
702 |
however, having a common protocol with indexed collections |
|
703 |
often simplifies things." |
|
32 | 704 |
|
8579 | 705 |
(index between:1 and:self size) ifFalse:[ |
8874 | 706 |
^ exceptionBlock value |
32 | 707 |
]. |
708 |
^ self at:index |
|
709 |
||
95 | 710 |
" |
711 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 712 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
713 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 714 |
|
95 | 715 |
(Dictionary with:(#foo -> #bar) |
8874 | 716 |
with:(#frob -> #baz)) |
717 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 718 |
" |
362 | 719 |
! |
720 |
||
1366 | 721 |
before:anObject |
722 |
"return the element before the argument, anObject. |
|
5231 | 723 |
If anObject is not in the receiver, report an error; |
724 |
if anObject is the first in the receiver, return nil. |
|
1366 | 725 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
726 |
||
727 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
728 |
||
729 |
" |
|
8874 | 730 |
#(4 3 2 1) asOrderedCollection before:3. |
731 |
#(4 3 2 1) asOrderedCollection before:4 |
|
732 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 733 |
" |
734 |
||
735 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
736 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
737 |
! |
|
738 |
||
739 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 740 |
"return the element, before anObject. |
5231 | 741 |
If anObject is the first in the receiver, return nil. |
742 |
If there is no such element (anObject is not found), |
|
1366 | 743 |
return the value from exceptionBlock." |
744 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
745 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
746 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
747 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
748 |
idx ~~ 0 ifTrue:[ |
8874 | 749 |
idx == 1 ifTrue:[^ nil]. |
750 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
751 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
752 |
^ exceptionBlock value |
1366 | 753 |
|
5231 | 754 |
" |
8874 | 755 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
756 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
757 |
#(4 3 2 1) asOrderedCollection before:5. |
|
758 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
759 |
#(4 3 2 1) asOrderedCollection before:4. |
|
760 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
761 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 762 |
" |
763 |
||
1366 | 764 |
! |
765 |
||
602 | 766 |
first |
8874 | 767 |
"return the first element; |
5231 | 768 |
report an error, if the collection is empty." |
602 | 769 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
770 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 771 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
772 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
773 |
" |
8874 | 774 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
775 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
776 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
777 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
778 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 779 |
! |
780 |
||
5887 | 781 |
first:n |
782 |
"return the n first elements of the collection. |
|
783 |
Raises an error if there are not enough elements in the receiver." |
|
784 |
||
785 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
786 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
787 |
||
788 |
^ self copyFirst:n |
|
789 |
||
790 |
" |
|
8874 | 791 |
#(1 2 3 4 5) first:3 |
792 |
#(1 2 3 4 5) first:6 |
|
793 |
#(1 2 3 4 5) asSet first:3 |
|
794 |
'hello world' first:5 |
|
5887 | 795 |
" |
796 |
! |
|
797 |
||
11878 | 798 |
indexOfNth:n occurrenceOf:what |
799 |
"return the index of the nTh occurence of a value, or 0 if there are not that many" |
|
800 |
||
801 |
|idx cnt| |
|
802 |
||
803 |
n > self size ifTrue:[^ 0]. |
|
804 |
cnt := 0. |
|
805 |
idx := 0. |
|
806 |
[ |
|
807 |
idx := self indexOf:what startingAt:idx+1. |
|
808 |
idx == 0 ifTrue:[^ 0]. |
|
809 |
cnt := cnt + 1. |
|
810 |
cnt = n ifTrue:[ ^ idx]. |
|
811 |
] loop. |
|
812 |
||
813 |
" |
|
814 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:1 |
|
815 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:9 |
|
816 |
" |
|
817 |
! |
|
818 |
||
362 | 819 |
keyAtEqualValue:value |
820 |
"return the index of a value. |
|
821 |
This is normally not used (use indexOf:), but makes the |
|
822 |
protocol more compatible with dictionaries." |
|
823 |
||
824 |
^ self indexOf:value |
|
825 |
! |
|
826 |
||
827 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
828 |
"return the index of a value. |
|
829 |
This is normally not used (use indexOf:), but makes the |
|
830 |
protocol more compatible with dictionaries." |
|
831 |
||
832 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 833 |
! |
834 |
||
835 |
keyAtValue:value |
|
836 |
"return the index of a value. |
|
837 |
This is normally not used (use indexOf:), but makes the |
|
838 |
protocol more compatible with dictionaries." |
|
839 |
||
840 |
^ self identityIndexOf:value |
|
841 |
! |
|
842 |
||
843 |
keyAtValue:value ifAbsent:exceptionBlock |
|
844 |
"return the index of a value. |
|
845 |
This is normally not used (use indexOf:), but makes the |
|
846 |
protocol more compatible with dictionaries." |
|
847 |
||
848 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
849 |
! |
|
850 |
||
851 |
last |
|
5231 | 852 |
"return the last element; |
853 |
report an error, if the collection is empty." |
|
602 | 854 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
855 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
856 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
857 |
(sz := self size) == 0 ifFalse:[ |
8874 | 858 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
859 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
860 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
861 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
862 |
|
5231 | 863 |
" |
864 |
#(1 2 3 4 5) last |
|
865 |
#() last |
|
866 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
867 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
868 |
|
5887 | 869 |
last:n |
870 |
"return the n last elements of the collection. |
|
871 |
Raises an error if there are not enough elements in the receiver." |
|
872 |
||
873 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
874 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
875 |
||
876 |
^ self copyLast:n |
|
877 |
||
878 |
" |
|
8874 | 879 |
#(1 2 3 4 5) last:3 |
880 |
#(1 2 3 4 5) last:6 |
|
881 |
#(1 2 3 4 5) asSet last:3 |
|
882 |
'hello world' last:5 |
|
5887 | 883 |
" |
884 |
! |
|
885 |
||
4986 | 886 |
nth:n |
5231 | 887 |
"return the nth element; |
888 |
report an error, if the collections size is smaller than n." |
|
4986 | 889 |
|
890 |
^ self at:n |
|
891 |
||
892 |
" |
|
8874 | 893 |
args: |
4986 | 894 |
returns: nth element <object> |
895 |
" |
|
896 |
||
5231 | 897 |
" |
8874 | 898 |
#(10 20 30 40 50) nth:4 |
5231 | 899 |
#(10 20 30 40 50) nth:6 |
900 |
#() nth:1 |
|
901 |
" |
|
902 |
||
4986 | 903 |
! |
904 |
||
11915 | 905 |
order |
906 |
"the order is the set of keys " |
|
907 |
||
908 |
^ 1 to:self size |
|
909 |
! |
|
910 |
||
4855 | 911 |
second |
5231 | 912 |
"return the second element; |
913 |
report an error, if the collections size is smaller than 2." |
|
4855 | 914 |
|
915 |
^ self at:2 |
|
916 |
||
917 |
" |
|
8874 | 918 |
args: |
4855 | 919 |
returns: secondElement <object> |
920 |
" |
|
921 |
||
922 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
923 |
! |
|
924 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
925 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
926 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
927 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
928 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
929 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
930 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
931 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
932 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
933 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
934 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 935 |
! ! |
936 |
||
602 | 937 |
!SequenceableCollection methodsFor:'adding & removing'! |
938 |
||
939 |
add:anObject |
|
940 |
"append the argument, anObject to the collection. |
|
941 |
Return the argument, anObject. |
|
942 |
||
943 |
Notice, that this is modifies the receiver NOT a copy. |
|
944 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 945 |
due to the grow:-message, which is inefficient for fixed size |
602 | 946 |
collections (i.e. for Strings and Arrays it is not recommened)." |
947 |
||
948 |
|newSize| |
|
949 |
||
950 |
newSize := self size + 1. |
|
951 |
self grow:newSize. |
|
952 |
self at:newSize put:anObject. |
|
953 |
^ anObject |
|
954 |
||
955 |
" |
|
956 |
|a| |
|
957 |
a := #(1 2 3 4 5 6 7 8). |
|
958 |
a add:'hello'. |
|
959 |
a |
|
960 |
" |
|
961 |
" |
|
962 |
|c| |
|
963 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
964 |
c add:'hello'. |
|
965 |
c |
|
966 |
" |
|
967 |
! |
|
968 |
||
969 |
add:anElement beforeIndex:index |
|
970 |
"insert the first argument, anObject into the collection before slot index. |
|
971 |
Return the receiver (sigh - ST-80 compatibility). |
|
972 |
||
973 |
Notice, that this is modifies the receiver NOT a copy. |
|
974 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 975 |
due to the grow:-message, which is inefficient for fixed size |
602 | 976 |
collections (i.e. for Strings and Arrays it is not recommened)." |
977 |
||
978 |
|newSize| |
|
979 |
||
980 |
newSize := self size + 1. |
|
981 |
self grow:newSize. |
|
982 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
983 |
self at:index put:anElement |
|
984 |
||
985 |
" |
|
986 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
987 |
" |
|
202 | 988 |
! |
989 |
||
602 | 990 |
addFirst:anObject |
991 |
"prepend the argument, anObject to the collection. |
|
992 |
Return the argument, anObject. |
|
993 |
||
994 |
Notice, that this is modifies the receiver NOT a copy. |
|
995 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 996 |
due to the grow:-message, which is inefficient for fixed size |
602 | 997 |
collections (i.e. for Strings and Arrays it is not recommened)." |
998 |
||
999 |
|newSize| |
|
1000 |
||
1001 |
newSize := self size + 1. |
|
1002 |
self grow:newSize. |
|
1003 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
1004 |
self at:1 put:anObject. |
|
1005 |
^ anObject |
|
1006 |
||
1007 |
" |
|
8874 | 1008 |
|a| |
1009 |
a:= #(1 2 3 4 5 6 7 8). |
|
1010 |
a addFirst:'hello'. |
|
1011 |
a |
|
602 | 1012 |
" |
1013 |
" |
|
1014 |
|c| |
|
1015 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1016 |
c addFirst:'hello'. |
|
1017 |
c |
|
1018 |
" |
|
69 | 1019 |
! |
1020 |
||
602 | 1021 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1022 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1023 |
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
|
1024 |
Use equality compare (=) to search for an occurrence. |
602 | 1025 |
|
1026 |
Notice, that this is modifies the receiver NOT a copy. |
|
1027 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1028 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1029 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1030 |
||
8874 | 1031 |
|any |
602 | 1032 |
dstIndex "{ Class: SmallInteger }" |
1033 |
sz "{ Class: SmallInteger }"| |
|
1034 |
||
1035 |
dstIndex := 1. |
|
1036 |
any := false. |
|
1037 |
sz := self size. |
|
1038 |
1 to:sz do:[:srcIndex | |
|
8874 | 1039 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1040 |
any := true |
|
1041 |
] ifFalse:[ |
|
1042 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1043 |
self at:dstIndex put:(self at:srcIndex) |
|
1044 |
]. |
|
1045 |
dstIndex := dstIndex + 1 |
|
1046 |
] |
|
602 | 1047 |
]. |
1048 |
any ifTrue:[ |
|
8874 | 1049 |
self grow:dstIndex - 1. |
1050 |
^ anElement |
|
602 | 1051 |
]. |
1052 |
^ aBlock value |
|
1053 |
||
1054 |
" |
|
1422 | 1055 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1056 |
#(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
|
1057 |
#(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
|
1058 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1059 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1060 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1061 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1062 |
! |
1063 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1064 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1065 |
"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
|
1066 |
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
|
1067 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1068 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1069 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1070 |
"/ 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
|
1071 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1072 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1073 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1074 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1075 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1076 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1077 |
[runIndex <= self size] whileTrue:[ |
8874 | 1078 |
element := self at:runIndex. |
1079 |
(aBlock value:element) ifTrue:[ |
|
1080 |
removed add:element. |
|
1081 |
self removeAtIndex:runIndex |
|
1082 |
] ifFalse:[ |
|
1083 |
runIndex := runIndex + 1 |
|
1084 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1085 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1086 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1087 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1088 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1089 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1090 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1091 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1092 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1093 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1094 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1095 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1096 |
"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
|
1097 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1098 |
|
602 | 1099 |
removeAtIndex:anIndex |
1100 |
"remove the element stored at anIndex. Return the removed object. |
|
1101 |
||
1102 |
Notice, that this is modifies the receiver NOT a copy. |
|
1103 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1104 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1105 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1106 |
||
1107 |
|element| |
|
1108 |
||
1109 |
element := self at:anIndex. |
|
1110 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1111 |
^ element |
|
1112 |
||
1113 |
" |
|
1114 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1115 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1116 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1117 |
" |
|
13 | 1118 |
! |
1119 |
||
602 | 1120 |
removeFirst |
1121 |
"remove the first element of the receiver and return it. |
|
1122 |
||
1123 |
Notice, that this is modifies the receiver NOT a copy. |
|
1124 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1125 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1126 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1127 |
||
1128 |
^ self removeAtIndex:1 |
|
1129 |
||
1130 |
" |
|
1131 |
|a| |
|
1132 |
a := #(1 2 3 4 5 6). |
|
1133 |
a removeFirst. |
|
8874 | 1134 |
a |
602 | 1135 |
" |
13 | 1136 |
! |
1137 |
||
2294 | 1138 |
removeFromIndex:startIndex |
1139 |
"remove the elements stored at indexes from startIndex to the end. |
|
1140 |
Return the receiver. |
|
1141 |
Returning the receiver is a historic leftover - it may at one |
|
1142 |
time return a collection of the removed elements. |
|
1143 |
||
8874 | 1144 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1145 |
therefore any other users of the receiver will also see this change. |
1146 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1147 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1148 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1149 |
||
1150 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1151 |
||
1152 |
" |
|
8874 | 1153 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1154 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1155 |
" |
|
1156 |
||
1157 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1158 |
! |
|
1159 |
||
602 | 1160 |
removeFromIndex:startIndex toIndex:endIndex |
1161 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1162 |
Return the receiver. |
|
1163 |
Returning the receiver is a historic leftover - it may at one |
|
1164 |
time return a collection of the removed elements. |
|
1165 |
||
8874 | 1166 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1167 |
therefore any other users of the receiver will also see this change. |
1168 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1169 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1170 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1171 |
||
1172 |
|size newSize| |
|
1173 |
||
1174 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1175 |
self grow:(startIndex - 1) |
602 | 1176 |
] ifFalse:[ |
8874 | 1177 |
newSize := size - endIndex + startIndex - 1. |
1178 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1179 |
self grow:newSize |
|
602 | 1180 |
] |
1181 |
||
1182 |
"/ |newSize| |
|
1183 |
"/ |
|
1184 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1185 |
"/ newSize <= 0 ifTrue:[ |
|
1186 |
"/ self grow:0 |
|
1187 |
"/ ] ifFalse:[ |
|
1188 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1189 |
"/ self grow:newSize |
|
1190 |
"/ ] |
|
1191 |
||
1192 |
" |
|
8874 | 1193 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1194 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1195 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1196 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1197 |
" |
1198 |
! |
|
1199 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1200 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1201 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1202 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1203 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1204 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1205 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1206 |
Also note, that it may be a slow operation for some collections, |
8874 | 1207 |
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
|
1208 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1209 |
|
2363 | 1210 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1211 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1212 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1213 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1214 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1215 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1216 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1217 |
1 to:sz do:[:srcIndex | |
8874 | 1218 |
el := self at:srcIndex. |
1219 |
(anElement == el) ifTrue:[ |
|
1220 |
any := true |
|
1221 |
] ifFalse:[ |
|
1222 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1223 |
self at:dstIndex put:el |
|
1224 |
]. |
|
1225 |
dstIndex := dstIndex + 1 |
|
1226 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1227 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1228 |
any ifTrue:[ |
8874 | 1229 |
self grow:dstIndex - 1. |
1230 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1231 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1232 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1233 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1234 |
" |
8874 | 1235 |
#(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
|
1236 |
#(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
|
1237 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1238 |
|
2363 | 1239 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1240 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1241 |
|
602 | 1242 |
removeIndex:index |
1243 |
"remove the argument stored at index. Return the receiver. |
|
1244 |
||
1245 |
Notice, that this is modifies the receiver NOT a copy. |
|
1246 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1247 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1248 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1249 |
||
1250 |
self removeFromIndex:index toIndex:index |
|
1251 |
||
1252 |
" |
|
1253 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1254 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1255 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1256 |
" |
|
77 | 1257 |
! |
1258 |
||
602 | 1259 |
removeLast |
1260 |
"remove the last element of the receiver and return it. |
|
1261 |
||
1262 |
Notice, that this is modifies the receiver NOT a copy. |
|
1263 |
Also note, that it may be a slow operation for some collections, |
|
1264 |
due to the grow:-message, which is inefficient for fixed size |
|
1265 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1266 |
||
8874 | 1267 |
^ self removeAtIndex:(self size) |
602 | 1268 |
|
1269 |
" |
|
1270 |
|a| |
|
1271 |
a := #(1 2 3 4 5 6). |
|
1272 |
a removeLast. |
|
8874 | 1273 |
a |
602 | 1274 |
" |
1275 |
! ! |
|
1276 |
||
11407 | 1277 |
!SequenceableCollection methodsFor:'combinatoric'! |
1278 |
||
1279 |
combinationsDo: aBlock |
|
11463 | 1280 |
"Repeatly evaluate aBlock with all combinations of elements from the receivers elements. |
1281 |
The receivers elements must be collections of the individuals to be taken for the combinations" |
|
11407 | 1282 |
|
1283 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1284 |
||
1285 |
" |
|
1286 |
(Array |
|
11410 | 1287 |
with:($a to:$d)) |
1288 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1289 |
" |
|
1290 |
" |
|
1291 |
(Array |
|
11407 | 1292 |
with:($a to:$d) |
1293 |
with:(1 to: 4)) |
|
1294 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1295 |
" |
|
1296 |
" |
|
1297 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1298 |
" |
|
11408 | 1299 |
"all combinations of two letters are generated with: |
1300 |
(Array |
|
1301 |
with:($a to:$z) |
|
1302 |
with:($a to:$z)) |
|
1303 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1304 |
" |
|
11432 | 1305 |
" |
1306 |
(Array |
|
1307 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1308 |
with:#(A)) |
|
1309 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1310 |
" |
|
11407 | 1311 |
! |
1312 |
||
1313 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1314 |
"a helper for combinationsDo:" |
|
1315 |
||
11431 | 1316 |
|loopedElement| |
1317 |
||
1318 |
loopedElement := self at:anInteger. |
|
1319 |
||
11407 | 1320 |
anInteger == self size ifTrue:[ |
11431 | 1321 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1322 |
^ self. |
11431 | 1323 |
]. |
1324 |
||
1325 |
loopedElement do:[:el | |
|
1326 |
|newPrefix| |
|
1327 |
||
1328 |
newPrefix := (prefix copyWith:el). |
|
1329 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1330 |
]. |
1331 |
||
1332 |
" |
|
1333 |
(Array |
|
1334 |
with:($a to:$d) |
|
1335 |
with:(1 to: 4)) |
|
1336 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1337 |
" |
|
11431 | 1338 |
" |
1339 |
(Array |
|
1340 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1341 |
with:#(A)) |
|
1342 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1343 |
" |
|
11407 | 1344 |
! |
1345 |
||
11462 | 1346 |
lexicographicPermutationsDo:aBlock |
1347 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1348 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1349 |
permutations in lexicographical order (i.e. sorted by size). |
|
11932 | 1350 |
|
11462 | 1351 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1352 |
the identical same collection (but with different contents) every time. |
1353 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1354 |
of permuted copies. The user of this function must be careful in the block, |
|
1355 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1356 |
|
1357 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1358 |
||
1359 |
" |
|
1360 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1361 |
" |
|
1362 |
! |
|
1363 |
||
1364 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1365 |
"a helper for lexicographicPermutationsDo:" |
|
1366 |
||
1367 |
|mySize values indices| |
|
1368 |
||
1369 |
mySize := self size. |
|
1370 |
anInteger > mySize ifTrue: [^ self]. |
|
1371 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1372 |
values := self copyFrom:anInteger to:mySize. |
|
1373 |
indices := (anInteger to:mySize) asArray. |
|
1374 |
values sortWith:indices. |
|
1375 |
indices do:[:i | |
|
1376 |
self swap: anInteger with: i. |
|
1377 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1378 |
self swap: anInteger with: i |
|
1379 |
] |
|
1380 |
||
1381 |
" |
|
1382 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1383 |
" |
|
1384 |
! |
|
1385 |
||
1386 |
permutationsDo:aBlock |
|
1387 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1388 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1389 |
|
11462 | 1390 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1391 |
the identical same collection (but with different contents) every time. |
1392 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1393 |
of permuted copies. The user of this function must be careful in the block, |
|
1394 |
to copy the argument, if it has to be stored somewhere." |
|
11407 | 1395 |
|
1396 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1397 |
||
1398 |
" |
|
11462 | 1399 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1400 |
" |
1401 |
||
1402 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1403 |
! |
|
1404 |
||
1405 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1406 |
"a helper for permutationsDo:" |
11407 | 1407 |
|
1408 |
|mySize| |
|
1409 |
||
1410 |
mySize := self size. |
|
11462 | 1411 |
anInteger > mySize ifTrue: [^ self]. |
1412 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1413 |
anInteger to:mySize do:[:i | |
1414 |
self swap: anInteger with: i. |
|
11462 | 1415 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1416 |
self swap: anInteger with: i |
1417 |
] |
|
1418 |
||
1419 |
" |
|
1420 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1421 |
|
1422 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1423 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1424 |
" |
1425 |
||
1426 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1427 |
! |
1428 |
||
1429 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1430 |
"a helper for permutationsDo:" |
|
1431 |
||
1432 |
|mySize| |
|
1433 |
||
1434 |
mySize := self size. |
|
1435 |
anInteger > mySize ifTrue: [^ self]. |
|
1436 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1437 |
anInteger to:mySize do:[:i | |
|
1438 |
self swap: anInteger with:i. |
|
1439 |
anotherCollection swap: anInteger with:i. |
|
1440 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1441 |
self swap:anInteger with: i. |
|
1442 |
anotherCollection swap:anInteger with:i. |
|
1443 |
] |
|
1444 |
||
1445 |
" |
|
1446 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1447 |
||
1448 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1449 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1450 |
" |
|
1451 |
||
1452 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1453 |
! |
|
1454 |
||
1455 |
permutationsWith:anotherCollection do:aBlock |
|
1456 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1457 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1458 |
|
11462 | 1459 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1460 |
the identical same collection (but with different contents) every time. |
1461 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1462 |
of permuted copies. The user of this function must be careful in the block, |
|
1463 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1464 |
|
1465 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1466 |
||
1467 |
" |
|
1468 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1469 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1470 |
" |
|
1471 |
||
1472 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1473 |
! ! |
1474 |
||
602 | 1475 |
!SequenceableCollection methodsFor:'comparing'! |
1476 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1477 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1478 |
"compare two sequenceable collections" |
8874 | 1479 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1480 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1481 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1482 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1483 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1484 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1485 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1486 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1487 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1488 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1489 |
self error:'cannot compare non-(sequenceable) collection'. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1490 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1491 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1492 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1493 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1494 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1495 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1496 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1497 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1498 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1499 |
1 to: min do: [:i| |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1500 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1501 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1502 |
(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
|
1503 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1504 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1505 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1506 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1507 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1508 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1509 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1510 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1511 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1512 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1513 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1514 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1515 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1516 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1517 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1518 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1519 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1520 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1521 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1522 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1523 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1524 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1525 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1526 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1527 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1528 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1529 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1530 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1531 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1532 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1533 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1534 |
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
|
1535 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1536 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1537 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1538 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1539 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1540 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1541 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1542 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1543 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1544 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1545 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1546 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1547 |
"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
|
1548 |
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
|
1549 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1550 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1551 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1552 |
|
602 | 1553 |
= aCollection |
1554 |
"return true if the receiver and aCollection represent collections |
|
1555 |
with equal contents." |
|
1556 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1557 |
| stop "{ Class: SmallInteger }" | |
602 | 1558 |
|
1559 |
(aCollection == self) ifTrue:[^true]. |
|
1560 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1561 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1562 |
|
1563 |
stop := self size. |
|
1564 |
stop == (aCollection size) ifFalse:[^false]. |
|
1565 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1566 |
1 to:stop do:[:index| |
9366 | 1567 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1568 |
]. |
1569 |
^ true |
|
1570 |
||
1571 |
" |
|
8874 | 1572 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1573 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1574 |
#($1 $2 $3 $4 $5) = '12345' |
|
1575 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1576 |
" |
1577 |
! |
|
1578 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1579 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1580 |
"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
|
1581 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1582 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1583 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1584 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1585 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1586 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1587 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1588 |
"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
|
1589 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1590 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1591 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1592 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1593 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1594 |
|
8874 | 1595 |
commonPrefixWith:aCollection |
6507 | 1596 |
"return the common prefix of myself and the argument, aCollection. |
1597 |
If there is none, an empty collection is returned." |
|
1598 |
||
1599 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1600 |
||
1601 |
" |
|
8874 | 1602 |
'hello' commonPrefixWith:'hello' |
1603 |
'hello' commonPrefixWith:'hElLo' |
|
1604 |
'hello' commonPrefixWith:'hello world' |
|
1605 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1606 |
||
1607 |
'hello world' commonPrefixWith:'hello' |
|
1608 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1609 |
||
1610 |
'abcd' commonPrefixWith:'bcde' |
|
1611 |
||
1612 |
'abcd' commonPrefixWith:'abab' |
|
1613 |
'abcd' commonPrefixWith:'ab' |
|
1614 |
'abcd' commonPrefixWith:'ababab' |
|
1615 |
'abcd' commonPrefixWith:'abcdef' |
|
1616 |
||
1617 |
'abab' commonPrefixWith:'abcd' |
|
1618 |
'ab' commonPrefixWith:'abcd' |
|
1619 |
'ababab' commonPrefixWith:'abcd' |
|
1620 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1621 |
" |
1622 |
! |
|
1623 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1624 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1625 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1626 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1627 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1628 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1629 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1630 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1631 |
1 to:matchLen do:[:idx | |
8874 | 1632 |
|elHere elThere same| |
1633 |
||
1634 |
elHere := self at:idx. |
|
1635 |
elThere := aCollection at:idx. |
|
1636 |
||
1637 |
ignoreCase ifTrue:[ |
|
1638 |
same := elHere sameAs:elThere |
|
1639 |
] ifFalse:[ |
|
1640 |
same := elHere = elThere |
|
1641 |
]. |
|
1642 |
same ifFalse:[ |
|
1643 |
^ self copyTo:(idx - 1). |
|
1644 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1645 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1646 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1647 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1648 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1649 |
" |
8874 | 1650 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1651 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1652 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1653 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1654 |
||
1655 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1656 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1657 |
||
1658 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1659 |
||
1660 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1661 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1662 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1663 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1664 |
||
1665 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1666 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1667 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1668 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1669 |
" |
|
1670 |
! |
|
1671 |
||
1672 |
commonSuffixWith:aCollection |
|
6507 | 1673 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1674 |
If there is none, an empty collection is returned." |
|
1675 |
||
1676 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1677 |
||
1678 |
" |
|
8874 | 1679 |
'hello' commonSuffixWith:'hello' |
1680 |
'hello' commonSuffixWith:'hElLo' |
|
1681 |
'hello' commonSuffixWith:'hello world' |
|
1682 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1683 |
'hello2 world' commonSuffixWith:'hello world' |
|
1684 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1685 |
||
1686 |
'hello world' commonSuffixWith:'world' |
|
1687 |
'hello WoRlD' commonSuffixWith:'world' |
|
1688 |
||
1689 |
'dcba' commonSuffixWith:'edcb' |
|
1690 |
||
1691 |
'dcba' commonSuffixWith:'baba' |
|
1692 |
'dcba' commonSuffixWith:'ba' |
|
1693 |
'dcba' commonSuffixWith:'bababa' |
|
1694 |
'dcba' commonSuffixWith:'fdcba' |
|
1695 |
||
1696 |
'baba' commonSuffixWith:'dcba' |
|
1697 |
'ba' commonSuffixWith:'dcba' |
|
1698 |
'bababa' commonSuffixWith:'dcba' |
|
1699 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1700 |
" |
1701 |
! |
|
1702 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1703 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1704 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1705 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1706 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1707 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1708 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1709 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1710 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1711 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1712 |
1 to:matchLen do:[:idx | |
8874 | 1713 |
|elHere elThere same| |
1714 |
||
1715 |
elHere := self at:(l1 - idx + 1). |
|
1716 |
elThere := aCollection at:(l2 - idx + 1). |
|
1717 |
||
1718 |
ignoreCase ifTrue:[ |
|
1719 |
same := elHere sameAs:elThere |
|
1720 |
] ifFalse:[ |
|
1721 |
same := elHere = elThere |
|
1722 |
]. |
|
1723 |
same ifFalse:[ |
|
1724 |
^ self copyFrom:(l1 - idx + 2). |
|
1725 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1726 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1727 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1728 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1729 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1730 |
" |
8874 | 1731 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1732 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1733 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1734 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1735 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1736 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1737 |
||
1738 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1739 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1740 |
||
1741 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1742 |
||
1743 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1744 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1745 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1746 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1747 |
||
1748 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1749 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1750 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1751 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1752 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1753 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1754 |
|
5813 | 1755 |
deepSameContentsAs:aCollection |
1756 |
"return true, if the receiver and the arg have the same contents |
|
1757 |
in both the named instance vars and any indexed instVars. |
|
1758 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1759 |
||
1760 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1761 |
unused space" |
|
1762 |
||
1763 |
|index "{ Class: SmallInteger }" |
|
1764 |
stop "{ Class: SmallInteger }" | |
|
1765 |
||
1766 |
(aCollection == self) ifTrue:[^true]. |
|
1767 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1768 |
||
1769 |
stop := self size. |
|
1770 |
stop == (aCollection size) ifFalse:[^false]. |
|
1771 |
||
1772 |
index := 1. |
|
1773 |
[index <= stop] whileTrue:[ |
|
8874 | 1774 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1775 |
index := index + 1 |
|
5813 | 1776 |
]. |
1777 |
^ true |
|
1778 |
||
1779 |
" |
|
8874 | 1780 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1781 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1782 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1783 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1784 |
" |
1785 |
! |
|
1786 |
||
602 | 1787 |
endsWith:aCollection |
1788 |
"return true, if the receivers last elements match those |
|
1789 |
of aCollection" |
|
1790 |
||
1791 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1792 |
index2 "{ Class: SmallInteger }" |
1793 |
stop "{ Class: SmallInteger }" |
|
602 | 1794 |
sz "{ Class: SmallInteger }"| |
1795 |
||
1796 |
(aCollection == self) ifTrue:[^ true]. |
|
1797 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1798 |
||
1799 |
stop := aCollection size. |
|
77 | 1800 |
sz := self size. |
602 | 1801 |
stop > sz ifTrue:[^false]. |
1802 |
||
1803 |
index1 := sz. |
|
1804 |
index2 := stop. |
|
1805 |
[index2 > 0] whileTrue:[ |
|
8874 | 1806 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1807 |
index1 := index1 - 1. |
|
1808 |
index2 := index2 - 1 |
|
77 | 1809 |
]. |
602 | 1810 |
^ true |
1811 |
||
1812 |
" |
|
1813 |
'abcde' endsWith:#($d $e) |
|
8874 | 1814 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1815 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1816 |
" |
|
1817 |
! |
|
1818 |
||
7278 | 1819 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1820 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1821 |
|
1822 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1823 |
||
1824 |
" |
|
1825 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1826 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1827 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1828 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1829 |
" |
1830 |
! |
|
1831 |
||
11820 | 1832 |
hasEqualElements: otherCollection |
1833 |
"Answer whether the receiver's size is the same as otherCollection's |
|
1834 |
size, and each of the receiver's elements equal the corresponding |
|
1835 |
element of otherCollection. |
|
1836 |
This should probably replace the current definition of #= ." |
|
1837 |
||
1838 |
| size | |
|
1839 |
(otherCollection isKindOf: SequenceableCollection) ifFalse: [^ false]. |
|
1840 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
|
1841 |
1 to: size do: |
|
1842 |
[:index | |
|
1843 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false]]. |
|
1844 |
^ true |
|
1845 |
! |
|
1846 |
||
602 | 1847 |
hash |
1848 |
"return a hash key for the receiver" |
|
1849 |
||
1850 |
"this hash is stupid - but for larger collections, the hashing |
|
1851 |
time can become much bigger than the time lost in added probing. |
|
1852 |
Time will show ..." |
|
1853 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1854 |
|mySize h| |
3653 | 1855 |
|
1856 |
mySize := self size. |
|
1857 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1858 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1859 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1860 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1861 |
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
|
1862 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1863 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1864 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1865 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1866 |
"/ 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
|
1867 |
"/ ^ ((((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
|
1868 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1869 |
" |
8874 | 1870 |
#(1 2 3 4 5 6) hash |
1871 |
(1 to:6) hash |
|
1872 |
||
1873 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1874 |
#(1 2 3 4 5) hash |
|
602 | 1875 |
" |
3653 | 1876 |
|
1877 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1878 |
! |
1879 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1880 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1881 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1882 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1883 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1884 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1885 |
^ self |
8874 | 1886 |
sameContentsAs:aCollection |
1887 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1888 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1889 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1890 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1891 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1892 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1893 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1894 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1895 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1896 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1897 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1898 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1899 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1900 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1901 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1902 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1903 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1904 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1905 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1906 |
" |
8874 | 1907 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1908 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1909 |
#($1 $2 $3 $4 $5) = '12345' |
|
1910 |
||
1911 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1912 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1913 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1914 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1915 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1916 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1917 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1918 |
|
6118 | 1919 |
isSameSequenceAs: otherCollection |
8874 | 1920 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1921 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1922 |
" |
|
1923 |
||
1924 |
| size | |
|
1925 |
||
1926 |
(size := self size) = otherCollection size ifFalse: [^false]. |
|
1927 |
||
1928 |
1 to: size do:[:index | |
|
8874 | 1929 |
(self at: index) = (otherCollection at: index) ifFalse: [^false] |
6118 | 1930 |
]. |
1931 |
^ true |
|
1932 |
! |
|
1933 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1934 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1935 |
"return true if the receiver and aCollection represent collections |
8874 | 1936 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1937 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1938 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1939 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1940 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1941 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1942 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1943 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1944 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1945 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1946 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1947 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1948 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1949 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1950 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1951 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1952 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1953 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1954 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1955 |
[index <= stop] whileTrue:[ |
8874 | 1956 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1957 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1958 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1959 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1960 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1961 |
" |
8874 | 1962 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
1963 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1964 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1965 |
#(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
|
1966 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1967 |
#('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
|
1968 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1969 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1970 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1971 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1972 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1973 |
|
602 | 1974 |
startsWith:aCollection |
1975 |
"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
|
1976 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1977 |
If the argument is empty, true is returned." |
602 | 1978 |
|
1979 |
|index "{ Class: SmallInteger }" |
|
1980 |
stop "{ Class: SmallInteger }" | |
|
1981 |
||
1982 |
(aCollection == self) ifTrue:[^true]. |
|
1983 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1984 |
||
1985 |
stop := aCollection size. |
|
1986 |
stop > self size ifTrue:[^false]. |
|
1987 |
||
1988 |
index := 1. |
|
1989 |
[index <= stop] whileTrue:[ |
|
8874 | 1990 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1991 |
index := index + 1 |
|
602 | 1992 |
]. |
1993 |
^ true |
|
1994 |
||
1995 |
" |
|
1996 |
'abcde' startsWith:#($a $b $c) |
|
1997 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1998 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1999 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 2000 |
" |
7279 | 2001 |
! |
2002 |
||
2003 |
startsWithAnyOf:aCollectionOfCollections |
|
2004 |
"return true, if the receiver starts with any in aCollection" |
|
2005 |
||
2006 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
2007 |
||
2008 |
" |
|
2009 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
2010 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 2011 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
2012 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 2013 |
" |
13 | 2014 |
! ! |
2015 |
||
260 | 2016 |
!SequenceableCollection methodsFor:'converting'! |
2017 |
||
11879
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2018 |
asCollectionOfSubCollectionsOfSize:pieceSize |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2019 |
"return a collection containing pieces of size pieceSite from the receiver. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2020 |
The last piece may be smaller, if the receivers size is not a multiple of pieceSize." |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2021 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2022 |
|pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2023 |
start "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2024 |
stop "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2025 |
mySize "{ Class:SmallInteger }"| |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2026 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2027 |
pieces := OrderedCollection new. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2028 |
start := 1. stop := start + pieceSize - 1. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2029 |
mySize := self size. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2030 |
[stop <= mySize] whileTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2031 |
pieces add:(self copyFrom:start to:stop). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2032 |
start := start + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2033 |
stop := stop + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2034 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2035 |
(start <= mySize) ifTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2036 |
pieces add:(self copyFrom:start to:mySize). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2037 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2038 |
^ pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2039 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2040 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2041 |
'123123123123123123' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2042 |
'12312312312312312312' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2043 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2044 |
! |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2045 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2046 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 2047 |
"return a collection containing the subcollections (separated by anElement) |
2048 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2049 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2050 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2051 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2052 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2053 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2054 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2055 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2056 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2057 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2058 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2059 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2060 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2061 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2062 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2063 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2064 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2065 |
1 to:numberOfCols do:[:lineNr | |
8874 | 2066 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
2067 |
stopIndex == 0 ifTrue:[ |
|
2068 |
stopIndex := self size |
|
2069 |
] ifFalse: [ |
|
2070 |
stopIndex := stopIndex - 1. |
|
2071 |
]. |
|
2072 |
||
2073 |
(stopIndex < startIndex) ifTrue: [ |
|
2074 |
cols add:(myClass new:0) |
|
2075 |
] ifFalse: [ |
|
2076 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
2077 |
]. |
|
2078 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2079 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2080 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2081 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2082 |
" |
8874 | 2083 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2084 |
'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
|
2085 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2086 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2087 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2088 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2089 |
|
9234 | 2090 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
2091 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2092 |
of the receiver by anElement. |
|
2093 |
If anElement occurs multiple times in a row, |
|
2094 |
the block will be invoked with empty collections as argument. |
|
2095 |
This algorithm uses equality-compare to detect the element." |
|
2096 |
||
2097 |
|subCollection |
|
2098 |
endIndex "{ Class:SmallInteger }" |
|
2099 |
startIndex "{ Class:SmallInteger }" |
|
2100 |
stopIndex "{ Class:SmallInteger }" | |
|
2101 |
||
2102 |
startIndex := 0. |
|
2103 |
endIndex := self size. |
|
2104 |
||
2105 |
[startIndex <= endIndex] whileTrue:[ |
|
2106 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2107 |
stopIndex == 0 ifTrue:[ |
|
2108 |
stopIndex := self size |
|
2109 |
] ifFalse: [ |
|
2110 |
stopIndex := stopIndex - 1. |
|
2111 |
]. |
|
2112 |
||
2113 |
(stopIndex < startIndex) ifTrue: [ |
|
2114 |
subCollection := self species new:0 |
|
2115 |
] ifFalse: [ |
|
2116 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2117 |
]. |
|
2118 |
aBlock value:subCollection. |
|
2119 |
startIndex := stopIndex + 1 |
|
2120 |
]. |
|
2121 |
||
2122 |
" |
|
2123 |
'' |
|
2124 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2125 |
||
2126 |
'1 one' |
|
2127 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2128 |
||
2129 |
'1 one:2 two:3 three:4 four:5 five' |
|
2130 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2131 |
||
2132 |
'a::b' |
|
2133 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2134 |
||
2135 |
':' |
|
2136 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2137 |
||
2138 |
':a' |
|
2139 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2140 |
||
2141 |
'a:' |
|
2142 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2143 |
" |
|
2144 |
! |
|
2145 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2146 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2147 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2148 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2149 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2150 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2151 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2152 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2153 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2154 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2155 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2156 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2157 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2158 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2159 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2160 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2161 |
[done] whileFalse:[ |
8874 | 2162 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2163 |
stopIndex == 0 ifTrue:[ |
|
2164 |
stopIndex := self size. |
|
2165 |
done := true. |
|
2166 |
] ifFalse: [ |
|
2167 |
stopIndex := stopIndex - 1. |
|
2168 |
]. |
|
2169 |
||
2170 |
(stopIndex < startIndex) ifTrue: [ |
|
2171 |
items add:(myClass new:0) |
|
2172 |
] ifFalse: [ |
|
2173 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2174 |
]. |
|
2175 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2176 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2177 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2178 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2179 |
" |
8874 | 2180 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2181 |
#(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
|
2182 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2183 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2184 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2185 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2186 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2187 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2188 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2189 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2190 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2191 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2192 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2193 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2194 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2195 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2196 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2197 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2198 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2199 |
#(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
|
2200 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2201 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2202 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2203 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2204 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2205 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2206 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2207 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2208 |
|words |
8874 | 2209 |
start "{ Class:SmallInteger }" |
2210 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2211 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2212 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2213 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2214 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2215 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2216 |
[start <= mySize] whileTrue:[ |
8874 | 2217 |
"skip multiple separators" |
2218 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2219 |
start := start + 1 . |
|
2220 |
start > mySize ifTrue:[ |
|
2221 |
^ words |
|
2222 |
]. |
|
2223 |
]. |
|
2224 |
||
2225 |
stop := self findFirst:aBlock startingAt:start. |
|
2226 |
stop == 0 ifTrue:[ |
|
2227 |
words add:(self copyFrom:start to:mySize). |
|
2228 |
^ words |
|
2229 |
]. |
|
2230 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2231 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2232 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2233 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2234 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2235 |
" |
8874 | 2236 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2237 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2238 |
#(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
|
2239 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2240 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2241 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2242 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2243 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2244 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2245 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2246 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2247 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2248 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2249 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2250 |
|
1413 | 2251 |
asStringCollection |
2252 |
"return a new string collection containing the elements; |
|
2253 |
these ought to be strings. (i.e. String or Text instances)" |
|
2254 |
||
2255 |
|newColl sz| |
|
2256 |
||
2257 |
sz := self size. |
|
9162 | 2258 |
newColl := StringCollection withSize:sz. |
1413 | 2259 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2260 |
^ newColl |
|
2261 |
||
2262 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2263 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2264 |
! |
|
2265 |
||
1188 | 2266 |
asStringWith:sepChar |
8874 | 2267 |
"return a string generated by concatenating my elements |
1188 | 2268 |
(which must be strings or nil) and embedding sepChar characters in between. |
2269 |
Nil entries and empty strings are counted as empty lines." |
|
2270 |
||
8874 | 2271 |
^ self |
2272 |
from:1 to:(self size) |
|
2273 |
asStringWith:sepChar |
|
2274 |
compressTabs:false |
|
2275 |
final:nil |
|
1413 | 2276 |
|
2277 |
" |
|
2278 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
2279 |
" |
|
2280 |
||
2281 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 2282 |
! |
2283 |
||
2284 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2285 |
"return part of myself as a string with embedded sepCharacters. |
|
2286 |
My elements must be strings or nil; nil entries and empty strings are |
|
2287 |
taken as empty lines." |
|
2288 |
||
8874 | 2289 |
^ self |
2290 |
from:firstLine to:lastLine |
|
2291 |
asStringWith:sepCharacter |
|
2292 |
compressTabs:false |
|
2293 |
final:nil |
|
1188 | 2294 |
" |
2295 |
creating entries for searchpath: |
|
2296 |
||
8874 | 2297 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2298 |
||
2299 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2300 |
" |
2301 |
||
2302 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2303 |
! |
|
2304 |
||
260 | 2305 |
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
|
2306 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2307 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2308 |
taken as empty lines. |
2309 |
If the argument compressTabs is true, leading spaces are converted |
|
2310 |
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
|
2311 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2312 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2313 |
^ self |
8874 | 2314 |
from:firstLine to:lastLine |
2315 |
asStringWith:sepCharacter |
|
2316 |
compressTabs:compressTabs |
|
2317 |
final:endCharacter |
|
2318 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2319 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2320 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2321 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2322 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2323 |
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
|
2324 |
"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
|
2325 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2326 |
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
|
2327 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2328 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2329 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2330 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2331 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2332 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2333 |
any emphasis. If false, a plain string is returned. |
260 | 2334 |
This method is tuned for big collections, in not creating many |
263 | 2335 |
intermediate strings (has linear runtime). For very small collections |
2336 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2337 |
|
5575 | 2338 |
^ self |
8874 | 2339 |
from:firstLine to:lastLine |
2340 |
asStringWith:sepCharacter |
|
2341 |
compressTabs:compressTabs |
|
2342 |
final:endCharacter |
|
2343 |
withEmphasis:withEmphasis |
|
5575 | 2344 |
! |
2345 |
||
2346 |
asStringWithCRs |
|
8874 | 2347 |
"return a string generated by concatenating my elements |
5575 | 2348 |
(which must be strings or nil) and embedding cr characters in between. |
2349 |
Nil entries and empty strings are counted as empty lines." |
|
2350 |
||
2351 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2352 |
||
2353 |
" |
|
8874 | 2354 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2355 |
|
2356 |
(OrderedCollection new |
|
8874 | 2357 |
add:'hello'; |
2358 |
add:'world'; |
|
2359 |
add:'foo'; |
|
2360 |
add:('bar' asText allBold); |
|
2361 |
yourself) asStringWithCRs |
|
5575 | 2362 |
|
2363 |
Transcript showCR: |
|
8874 | 2364 |
(OrderedCollection new |
2365 |
add:'hello'; |
|
2366 |
add:'world'; |
|
2367 |
add:'foo'; |
|
2368 |
add:('bar' asText allBold); |
|
2369 |
yourself) asStringWithCRs |
|
5575 | 2370 |
" |
2371 |
||
2372 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2373 |
! |
|
2374 |
||
2375 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2376 |
"return a string generated by concatenating some of my elements |
5575 | 2377 |
(which must be strings or nil) and embedding cr characters in between. |
2378 |
Nil entries and empty strings are counted as empty lines." |
|
2379 |
||
2380 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2381 |
||
2382 |
" |
|
8874 | 2383 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2384 |
|
2385 |
" |
|
2386 |
||
2387 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2388 |
! |
|
2389 |
||
2390 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2391 |
"return part of myself as a string with embedded cr's. |
|
2392 |
My elements must be strings or nil. |
|
2393 |
If the argument compressTabs is true, leading spaces are converted |
|
2394 |
to tab-characters (8col tabs). |
|
2395 |
Nil entries and empty strings are taken as empty lines." |
|
2396 |
||
2397 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2398 |
! |
|
2399 |
||
2400 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2401 |
"return part of myself as a string with embedded cr's. |
|
2402 |
My elements must be strings or nil; nil entries and empty strings are |
|
2403 |
taken as empty lines. |
|
2404 |
If the argument compressTabs is true, leading spaces are converted |
|
2405 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2406 |
should be followed by a cr or not." |
|
2407 |
||
2408 |
^ self asStringWith:(Character cr) |
|
8874 | 2409 |
from:firstLine |
2410 |
to:lastLine |
|
2411 |
compressTabs:compressTabs |
|
2412 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2413 |
! |
2414 |
||
2415 |
decodeAsLiteralArray |
|
2416 |
"given a literalEncoding in the receiver, |
|
2417 |
create & return the corresponding object. |
|
2418 |
The inverse operation to #literalArrayEncoding." |
|
2419 |
||
9414 | 2420 |
|clsName cls| |
5575 | 2421 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2422 |
self size == 0 ifTrue:[ |
9358 | 2423 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2424 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2425 |
|
9414 | 2426 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2427 |
clsName isSymbol ifFalse:[ |
9358 | 2428 |
^ MissingClassInLiteralArrayErrorSignal |
2429 |
raiseRequestWith:clsName |
|
9414 | 2430 |
errorString:('non-symbol className in literalArray-spec: ', clsName printString) |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2431 |
]. |
9414 | 2432 |
cls := Smalltalk classNamed:clsName. |
5575 | 2433 |
cls isNil ifTrue:[ |
9414 | 2434 |
^ MissingClassInLiteralArrayErrorSignal |
2435 |
raiseRequestWith:clsName |
|
2436 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2437 |
]. |
9414 | 2438 |
|
5575 | 2439 |
^ cls decodeFromLiteralArray:self. |
2440 |
||
2441 |
" |
|
10476 | 2442 |
#(Array 10 20) decodeAsLiteralArray |
2443 |
#(10 20) literalArrayEncoding |
|
2444 |
" |
|
2445 |
||
2446 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2447 |
! |
2448 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2449 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2450 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2451 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2452 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2453 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2454 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2455 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2456 |
#( |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2457 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2458 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2459 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2460 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2461 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2462 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2463 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2464 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2465 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2466 |
|
8874 | 2467 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2468 |
"return part of myself as a string with embedded sepCharacters. |
2469 |
My elements must be strings or nil; nil entries and empty strings are |
|
2470 |
taken as empty lines." |
|
2471 |
||
8874 | 2472 |
^ self |
2473 |
from:firstLine |
|
2474 |
to:lastLine |
|
2475 |
asStringWith:sepCharacter |
|
2476 |
compressTabs:false |
|
2477 |
final:nil |
|
5575 | 2478 |
" |
2479 |
creating entries for searchpath: |
|
2480 |
||
8874 | 2481 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2482 |
||
2483 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2484 |
||
2485 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2486 |
" |
2487 |
||
2488 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2489 |
! |
|
2490 |
||
2491 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2492 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2493 |
My elements must be strings or nil; nil entries and empty strings are |
|
2494 |
taken as empty lines. |
|
2495 |
If the argument compressTabs is true, leading spaces are converted |
|
2496 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2497 |
character (if non-nil)." |
|
2498 |
||
2499 |
^ self |
|
8874 | 2500 |
from:firstLine |
2501 |
to:lastLine |
|
2502 |
asStringWith:sepCharacter |
|
2503 |
compressTabs:compressTabs |
|
2504 |
final:endCharacter |
|
2505 |
withEmphasis:true |
|
5575 | 2506 |
|
2507 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2508 |
! |
|
2509 |
||
2510 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2511 |
"return part of myself as a string or text with embedded sepCharacters |
|
2512 |
and followup endCharacter. |
|
2513 |
My elements must be strings or nil; nil entries and empty strings are |
|
2514 |
taken as empty lines. |
|
2515 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2516 |
If the argument compressTabs is true, leading spaces are converted |
|
2517 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2518 |
character (if non-nil). |
|
2519 |
The withEmphais argument controls if the returned string should preserve |
|
2520 |
any emphasis. If false, a plain string is returned. |
|
2521 |
This method is tuned for big collections, in not creating many |
|
2522 |
intermediate strings (has linear runtime). For very small collections |
|
2523 |
and small strings, it may be faster to use the comma , operation." |
|
2524 |
||
293 | 2525 |
|idx1 "{ Class:SmallInteger }" |
2526 |
idx2 "{ Class:SmallInteger }" |
|
2527 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2528 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2529 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2530 |
newString lineString spaces idx nTabs |
8099 | 2531 |
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
|
2532 |
thisLen anyTab| |
260 | 2533 |
|
2534 |
" |
|
2535 |
first accumulate the size of the string, to avoid |
|
2536 |
countless reallocations. If tabs are compressed, |
|
2537 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2538 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2539 |
" |
8099 | 2540 |
needEmphasis := false. |
2541 |
maxBitsPerCharacter := 8. |
|
1015 | 2542 |
|
260 | 2543 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2544 |
sepCharacter isNil ifTrue:[ |
8874 | 2545 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2546 |
] ifFalse:[ |
8874 | 2547 |
sepCharacter isCharacter ifTrue:[ |
2548 |
sepCnt := 1 |
|
2549 |
] ifFalse:[ |
|
2550 |
sepCnt := sepCharacter size |
|
2551 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2552 |
]. |
260 | 2553 |
|
293 | 2554 |
idx1 := firstLine. |
2555 |
idx2 := lastLine. |
|
2556 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2557 |
lineString := self at:lineIndex. |
2558 |
||
2559 |
lineString notNil ifTrue:[ |
|
2560 |
withEmphasis ifTrue:[ |
|
2561 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2562 |
needEmphasis := true |
|
2563 |
]. |
|
2564 |
]. |
|
2565 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2566 |
totalLength := totalLength + lineString size |
|
2567 |
]. |
|
2568 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2569 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2570 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2571 |
|
8099 | 2572 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2573 |
maxBitsPerCharacter > 16 ifTrue:[ |
2574 |
stringClass := Unicode32String. |
|
2575 |
] ifFalse:[ |
|
2576 |
stringClass := Unicode16String. |
|
2577 |
] |
|
8099 | 2578 |
] ifFalse:[ |
8874 | 2579 |
stringClass := String. |
8099 | 2580 |
]. |
2581 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2582 |
endCharacter notNil ifTrue:[ |
8874 | 2583 |
endCharacter isCharacter ifTrue:[ |
2584 |
totalLength := totalLength + 1 |
|
2585 |
] ifFalse:[ |
|
2586 |
totalLength := totalLength + endCharacter size |
|
2587 |
]. |
|
260 | 2588 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2589 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2590 |
|
260 | 2591 |
spaces := ' '. |
1016 | 2592 |
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
|
2593 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2594 |
needEmphasis ifTrue:[ |
8874 | 2595 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2596 |
]. |
260 | 2597 |
|
2598 |
" |
|
2599 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2600 |
Be careful with runArrays: |
8874 | 2601 |
replacing individual elements is VERY expensive. |
2602 |
Therefore, create a new runArray from scratch. |
|
260 | 2603 |
" |
2604 |
pos := 1. |
|
293 | 2605 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2606 |
lineString := self at:lineIndex. |
2607 |
thisLen := lineString size. |
|
2608 |
thisLen ~~ 0 ifTrue:[ |
|
2609 |
withEmphasis ifFalse:[ |
|
2610 |
lineString := lineString string. |
|
2611 |
]. |
|
2612 |
||
2613 |
(anyTab := compressTabs) ifTrue:[ |
|
2614 |
" |
|
2615 |
mhmh: could use withTabs from String-class here, |
|
2616 |
but we should avoid creating too many temporary strings |
|
2617 |
(especially, since this method is typically used when converting |
|
2618 |
big texts such as when saving in the filebrowser ...). |
|
2619 |
Therefore, we convert tabs inline here doing a direct replace |
|
2620 |
in newString." |
|
2621 |
||
2622 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2623 |
nTabs := (idx-1) // 8. |
|
2624 |
anyTab := (nTabs > 0) |
|
2625 |
]. |
|
2626 |
anyTab ifTrue:[ |
|
2627 |
"any tabs" |
|
2628 |
||
2629 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2630 |
||
2631 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2632 |
newRuns notNil ifTrue:[ |
|
2633 |
newRuns add:nil withOccurrences:nTabs |
|
2634 |
]. |
|
2635 |
pos := pos + nTabs. |
|
2636 |
||
2637 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2638 |
newRuns notNil ifTrue:[ |
|
2639 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2640 |
idx to:lineString size do:[:pos | |
|
2641 |
newRuns add:(lineString emphasisAt:pos) |
|
2642 |
] |
|
2643 |
] ifFalse:[ |
|
2644 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2645 |
] |
|
2646 |
]. |
|
2647 |
pos := pos + thisLen - (nTabs * 8). |
|
2648 |
] ifFalse:[ |
|
2649 |
newString replaceFrom:pos with:lineString. |
|
2650 |
||
2651 |
newRuns notNil ifTrue:[ |
|
2652 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2653 |
newRuns addAll:(lineString emphasis) |
|
2654 |
] ifFalse:[ |
|
2655 |
newRuns add:nil withOccurrences:lineString size |
|
2656 |
] |
|
2657 |
]. |
|
2658 |
pos := pos + thisLen. |
|
2659 |
]. |
|
2660 |
]. |
|
2661 |
||
2662 |
lineIndex ~~ lastLine ifTrue:[ |
|
2663 |
c := sepCharacter |
|
2664 |
] ifFalse:[ |
|
2665 |
c := endCharacter |
|
2666 |
]. |
|
2667 |
||
2668 |
c notNil ifTrue:[ |
|
2669 |
c isCharacter ifTrue:[ |
|
2670 |
newString at:pos put:c. |
|
2671 |
newRuns notNil ifTrue:[ |
|
2672 |
newRuns add:nil. |
|
2673 |
]. |
|
2674 |
pos := pos + 1 |
|
2675 |
] ifFalse:[ |
|
2676 |
newString replaceFrom:pos with:c. |
|
2677 |
newRuns notNil ifTrue:[ |
|
2678 |
newRuns add:nil withOccurrences:c size |
|
2679 |
]. |
|
2680 |
pos := pos + c size |
|
2681 |
] |
|
2682 |
]. |
|
260 | 2683 |
]. |
2684 |
||
2685 |
" |
|
2686 |
in case of tab compression, the result has to be |
|
2687 |
cut to size ... sorry |
|
2688 |
" |
|
2689 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2690 |
newString := newString copyTo:(pos - 1) |
260 | 2691 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2692 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2693 |
newRuns notNil ifTrue:[ |
8874 | 2694 |
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
|
2695 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2696 |
|
260 | 2697 |
^ newString |
2698 |
||
2699 |
" |
|
2700 |
creating entries for searchpath: |
|
2701 |
||
8874 | 2702 |
#('foo' 'bar' 'baz' '/foo/bar') |
2703 |
asStringWith:$: |
|
2704 |
from:1 to:4 |
|
2705 |
compressTabs:false |
|
2706 |
final:nil |
|
260 | 2707 |
|
2708 |
with trailing colon: |
|
2709 |
||
8874 | 2710 |
#('foo' 'bar' 'baz' '/foo/bar') |
2711 |
asStringWith:$: |
|
2712 |
from:1 to:4 |
|
2713 |
compressTabs:false |
|
2714 |
final:$: |
|
260 | 2715 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2716 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2717 |
|
8874 | 2718 |
#('foo' 'bar' 'baz') |
2719 |
asStringWith:nil |
|
2720 |
from:1 to:3 |
|
2721 |
compressTabs:false |
|
2722 |
final:nil |
|
260 | 2723 |
|
2724 |
creating a string from a collection of lines: |
|
2725 |
||
8874 | 2726 |
#('foo' 'bar' 'baz') |
2727 |
asStringWith:(Character cr) |
|
2728 |
from:1 to:3 |
|
2729 |
compressTabs:false |
|
2730 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2731 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2732 |
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
|
2733 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2734 |
(Array |
8874 | 2735 |
with:'foo' |
2736 |
with:('bar' asText allBold) |
|
2737 |
with:'baz' |
|
2738 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2739 |
) |
|
2740 |
asStringWith:(Character cr) |
|
2741 |
from:1 to:4 |
|
2742 |
compressTabs:false |
|
2743 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2744 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2745 |
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
|
2746 |
|
8874 | 2747 |
#('foo' 'bar' 'baz') |
2748 |
asStringWith:'__' |
|
2749 |
from:1 to:3 |
|
2750 |
compressTabs:false |
|
2751 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2752 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2753 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2754 |
|
8874 | 2755 |
#('foo' 'bar' 'baz') |
2756 |
asStringWith:'__' |
|
2757 |
from:1 to:3 |
|
2758 |
compressTabs:false |
|
2759 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2760 |
|
260 | 2761 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2762 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2763 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2764 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
11827 | 2765 |
! |
2766 |
||
2767 |
subCollections:aBlock |
|
2768 |
"Answser an ordered collection of ordered collections |
|
2769 |
where each subcollection is delimited by an element of the receiver |
|
2770 |
for which the given block evaluates to true." |
|
2771 |
||
2772 |
|str answer current e| |
|
2773 |
||
2774 |
str := self readStream. |
|
2775 |
answer := OrderedCollection new. |
|
2776 |
current := OrderedCollection new. |
|
2777 |
[ str atEnd ] whileFalse:[ |
|
2778 |
e := str next. |
|
2779 |
current add:e. |
|
2780 |
(aBlock value:e) ifTrue:[ |
|
2781 |
answer add:current. |
|
2782 |
current := OrderedCollection new |
|
2783 |
] |
|
2784 |
]. |
|
2785 |
current notEmpty ifTrue:[ |
|
2786 |
answer add:current |
|
2787 |
]. |
|
2788 |
^ answer |
|
2789 |
||
2790 |
" |
|
2791 |
#( 1 2 3 nil 4 5 6 nil 7 8 9 nil ) subCollections:[:el | el isNil]. |
|
2792 |
" |
|
260 | 2793 |
! ! |
2794 |
||
7253 | 2795 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2796 |
||
8874 | 2797 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2798 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2799 |
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
|
2800 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2801 |
however, physically, no copy is made). |
9291 | 2802 |
Warning: |
2803 |
The slice SHARES the memory for the element-data with the original, |
|
2804 |
this means that any modifications in the original are visible in the slice |
|
2805 |
and vice versa." |
|
7253 | 2806 |
|
2807 |
^ self |
|
9291 | 2808 |
from:startIndex |
2809 |
to:self size |
|
2810 |
by:1 |
|
8874 | 2811 |
|
2812 |
" |
|
2813 |
#(1 2 3 4 5 6 7) from:3 |
|
2814 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2815 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2816 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2817 |
" |
|
2818 |
! |
|
2819 |
||
2820 |
from:startIndex by:step |
|
9291 | 2821 |
"Create a ReindexedCollection from the receiver. |
2822 |
The new collection represents the elements starting at startIndex to the end. |
|
2823 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2824 |
however, physically, no copy is made). |
|
2825 |
Warning: |
|
2826 |
The slice SHARES the memory for the element-data with the original, |
|
2827 |
this means that any modifications in the original are visible in the slice |
|
2828 |
and vice versa." |
|
7253 | 2829 |
|
2830 |
^ self |
|
9291 | 2831 |
from:startIndex |
2832 |
to:(step > 1 |
|
2833 |
ifTrue: [self size] |
|
2834 |
ifFalse: [1]) |
|
2835 |
by:step |
|
8874 | 2836 |
|
2837 |
" |
|
2838 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2839 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2840 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2841 |
( #(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
|
2842 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2843 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2844 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2845 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2846 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2847 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2848 |
Warning: |
2849 |
The slice SHARES the memory for the element-data with the original, |
|
2850 |
this means that any modifications in the original are visible in the slice |
|
2851 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2852 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2853 |
^ self |
9291 | 2854 |
from:startIndex |
2855 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2856 |
|
2857 |
" |
|
2858 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2859 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2860 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2861 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2862 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2863 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2864 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2865 |
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
|
2866 |
slice := coll from:3 to:7. |
8874 | 2867 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2868 |
coll at:4 put:40. |
8874 | 2869 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2870 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2871 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2872 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2873 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2874 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2875 |
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
|
2876 |
slice := coll from:3 to:7. |
8874 | 2877 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2878 |
slice at:1 put:40. |
8874 | 2879 |
slice. |
2880 |
" |
|
2881 |
! |
|
2882 |
||
2883 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2884 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2885 |
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
|
2886 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2887 |
however, physically, no copy is made). |
9291 | 2888 |
Warning: |
2889 |
The slice SHARES the memory for the element-data with the original, |
|
2890 |
this means that any modifications in the original are visible in the slice |
|
2891 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2892 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2893 |
^ self |
9291 | 2894 |
from:startIndex |
2895 |
to:endIndex |
|
2896 |
by: (startIndex <= endIndex |
|
2897 |
ifTrue: [1] |
|
2898 |
ifFalse: [-1]) |
|
8874 | 2899 |
|
2900 |
" |
|
2901 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2902 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2903 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2904 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2905 |
" |
|
2906 |
! |
|
2907 |
||
2908 |
from:startIndex to:endIndex by:step |
|
9291 | 2909 |
"Create a ReindexedCollection from the receiver. |
2910 |
The new collection represents the receivers elements up to endIndex. |
|
2911 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2912 |
however, physically, no copy is made). |
|
2913 |
Warning: |
|
2914 |
The slice SHARES the memory for the element-data with the original, |
|
2915 |
this means that any modifications in the original are visible in the slice |
|
2916 |
and vice versa." |
|
7253 | 2917 |
|
2918 |
^ ReindexedCollection |
|
9291 | 2919 |
on:self |
2920 |
from:startIndex |
|
2921 |
to:endIndex |
|
2922 |
by:step |
|
8874 | 2923 |
|
2924 |
" |
|
2925 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2926 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2927 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2928 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2929 |
" |
|
2930 |
! |
|
2931 |
||
2932 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2933 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2934 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2935 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2936 |
however, physically, no copy is made). |
9291 | 2937 |
Warning: |
2938 |
The slice SHARES the memory for the element-data with the original, |
|
2939 |
this means that any modifications in the original are visible in the slice |
|
2940 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2941 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2942 |
^ self |
9291 | 2943 |
from:1 |
2944 |
to:endIndex |
|
2945 |
by:1 |
|
8874 | 2946 |
|
2947 |
" |
|
2948 |
#(1 2 3 4 5 6 7) to:4 |
|
2949 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2950 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2951 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2952 |
" |
|
2953 |
! |
|
2954 |
||
2955 |
to:endIndex by:step |
|
9291 | 2956 |
"Create a ReindexedCollection from the receiver. |
2957 |
The new collection represents the receivers elements up to endIndex. |
|
2958 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2959 |
however, physically, no copy is made). |
|
2960 |
Warning: |
|
2961 |
The slice SHARES the memory for the element-data with the original, |
|
2962 |
this means that any modifications in the original are visible in the slice |
|
2963 |
and vice versa." |
|
7253 | 2964 |
|
2965 |
^ self |
|
9291 | 2966 |
from:(step > 0 |
2967 |
ifTrue: [1] |
|
2968 |
ifFalse: [self size]) |
|
2969 |
to:endIndex |
|
2970 |
by:step |
|
8874 | 2971 |
|
2972 |
" |
|
2973 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2974 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2975 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2976 |
( #(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
|
2977 |
" |
7253 | 2978 |
! ! |
2979 |
||
1 | 2980 |
!SequenceableCollection methodsFor:'copying'! |
2981 |
||
9106 | 2982 |
++ aCollection |
2983 |
"return a new collection formed from concatenating the receiver with |
|
2984 |
the argument. The class of the new collection is determined by the |
|
2985 |
receivers class, so mixing classes is possible, if the second collections |
|
2986 |
elements can be stored into instances of the receivers class." |
|
2987 |
||
2988 |
^ self , aCollection |
|
2989 |
||
2990 |
" |
|
2991 |
#($a $b $c) ++ #(1 2 3) |
|
2992 |
#($a $b $c) ++ '123' |
|
2993 |
'abc' ++ '123' |
|
2994 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2995 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2996 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2997 |
" |
|
2998 |
! |
|
2999 |
||
1 | 3000 |
, aCollection |
3001 |
"return a new collection formed from concatenating the receiver with |
|
44 | 3002 |
the argument. The class of the new collection is determined by the |
3003 |
receivers class, so mixing classes is possible, if the second collections |
|
3004 |
elements can be stored into instances of the receivers class." |
|
1 | 3005 |
|
8874 | 3006 |
|newCollection |
1 | 3007 |
mySize "{ Class: SmallInteger }" |
3008 |
newSize "{ Class: SmallInteger }" |
|
3009 |
otherSize "{ Class: SmallInteger }" |
|
3010 |
dstIndex "{ Class: SmallInteger }"| |
|
3011 |
||
3012 |
mySize := self size. |
|
3013 |
otherSize := aCollection size. |
|
3014 |
newSize := mySize + otherSize. |
|
202 | 3015 |
|
252 | 3016 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 3017 |
|
3018 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
3019 |
dstIndex := mySize + 1. |
|
202 | 3020 |
aCollection isSequenceable ifTrue:[ |
8874 | 3021 |
" |
3022 |
yes, aCollection has indexed elements |
|
3023 |
" |
|
3024 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 3025 |
] ifFalse:[ |
8874 | 3026 |
" |
3027 |
no, enumerate aCollection |
|
3028 |
" |
|
3029 |
aCollection do:[:element | |
|
3030 |
newCollection at:dstIndex put:element. |
|
3031 |
dstIndex := dstIndex + 1 |
|
3032 |
] |
|
1 | 3033 |
]. |
3034 |
^ newCollection |
|
44 | 3035 |
|
95 | 3036 |
" |
3037 |
#($a $b $c) , #(1 2 3) |
|
3038 |
#($a $b $c) , '123' |
|
3039 |
'abc' , '123' |
|
3040 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
3041 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3042 |
'abc' asArray , #(1 2 3 4 5) |
|
3043 |
" |
|
1 | 3044 |
! |
3045 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3046 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3047 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3048 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3049 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3050 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3051 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3052 |
" |
8874 | 3053 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3054 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3055 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3056 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3057 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3058 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3059 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3060 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3061 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3062 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3063 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3064 |
" |
8874 | 3065 |
#($a $b $c $d $e $f $g) copyButLast:2 |
3066 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3067 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3068 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3069 |
|
602 | 3070 |
copyFirst:count |
3071 |
"return a new collection consisting of the receivers first count |
|
3072 |
elements - this is just a rename of copyTo: - for compatibility." |
|
3073 |
||
3074 |
^ self copyFrom:1 to:count |
|
3075 |
||
3076 |
" |
|
3077 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
3078 |
'1234567890' copyFirst:4 |
|
3079 |
" |
|
3080 |
! |
|
3081 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3082 |
copyFrom:startIndex |
9234 | 3083 |
"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
|
3084 |
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
|
3085 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3086 |
^ self copyFrom:startIndex to:(self size) |
602 | 3087 |
|
3088 |
" |
|
3089 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
3090 |
'1234567890' copyFrom:2 |
|
8874 | 3091 |
(10 to:19) copyFrom:5 |
602 | 3092 |
" |
3093 |
! |
|
3094 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3095 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3096 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3097 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3098 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3099 |
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
|
3100 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3101 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3102 |
^ self |
8874 | 3103 |
copyFrom:startIndex |
3104 |
to:(startIndex + numberOfElements - 1) |
|
3105 |
||
3106 |
" |
|
3107 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3108 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3109 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3110 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3111 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3112 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3113 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3114 |
copyFrom:startIndex to:stopIndex |
602 | 3115 |
"return a new collection consisting of receivers elements |
4403 | 3116 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3117 |
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
|
3118 |
Raise an error, if stopIndex is out of range." |
602 | 3119 |
|
3120 |
|newCollection newSize| |
|
3121 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3122 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3123 |
newSize < 0 ifTrue:[ |
8874 | 3124 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3125 |
]. |
602 | 3126 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3127 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 3128 |
^ newCollection |
3129 |
||
3130 |
" |
|
3131 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3132 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3133 |
(1 to:10) copyFrom:2 to:5 |
3134 |
||
3135 |
'1234567890' copyFrom:2 to:15 |
|
3136 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 3137 |
" |
3138 |
! |
|
3139 |
||
3140 |
copyLast:count |
|
3141 |
"return a new collection consisting of the receivers last count |
|
3142 |
elements." |
|
3143 |
||
3144 |
|sz| |
|
3145 |
||
3146 |
sz := self size. |
|
3147 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3148 |
||
3149 |
" |
|
3150 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3151 |
'1234567890' copyLast:4 |
|
3152 |
" |
|
3153 |
! |
|
3154 |
||
11841 | 3155 |
copyMapping:map |
3156 |
"return a copy, where elements all replaced via the given mapping. |
|
3157 |
If the element is present as key in map, it is replaced by a corresponding value from the map. |
|
3158 |
Otherwise, it is left unchanged." |
|
3159 |
||
3160 |
|s| |
|
3161 |
||
3162 |
s := WriteStream on:self species new. |
|
3163 |
self do:[:el | |
|
3164 |
s nextPut:(map at:el ifAbsent:el) |
|
3165 |
]. |
|
3166 |
^ s contents |
|
3167 |
||
3168 |
" |
|
3169 |
|map| |
|
3170 |
||
3171 |
map := Dictionary new |
|
3172 |
at:1 put:'one'; |
|
3173 |
at:2 put:'two'; |
|
3174 |
yourself. |
|
3175 |
||
3176 |
#(1 2 3 4 1 2 3 4) copyMapping:map |
|
3177 |
" |
|
3178 |
! |
|
3179 |
||
3180 |
copyMappingFrom:inMap to:outMap |
|
3181 |
"return a copy, where elements all replaced via the given mapping. |
|
3182 |
If the element is present in inmap, it is replaced by a corresponding value from outmap. |
|
3183 |
Otherwise, it is left unchanged." |
|
3184 |
||
3185 |
|d| |
|
3186 |
||
3187 |
d := Dictionary new. |
|
3188 |
inMap keysAndValuesDo:[:k :i | d at:i put:(outMap at:k)]. |
|
3189 |
^ self copyMapping:d |
|
3190 |
||
3191 |
" |
|
3192 |
#(1 2 3 4 1 2 3 4) copyMappingFrom:#(1 2) to:#('one' 'two') |
|
3193 |
'hello' copyMappingFrom:'eo' to:'**' |
|
3194 |
" |
|
3195 |
! |
|
3196 |
||
3653 | 3197 |
copyReplaceAll:oldElement with:newElement |
3198 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3199 |
have been replaced by newElement." |
|
3200 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3201 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3202 |
^ self copy replaceAll:oldElement with:newElement |
3203 |
||
3204 |
" |
|
8874 | 3205 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3206 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3207 |
" |
3208 |
||
3209 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3210 |
! |
|
3211 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3212 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3213 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3214 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3215 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3216 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3217 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3218 |
|
5489 | 3219 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3220 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3221 |
"/ '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
|
3222 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3223 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3224 |
self do:[:el | |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3225 |
el = oldObject ifTrue:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3226 |
s nextPutAll:aCollection |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3227 |
] ifFalse:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3228 |
s nextPut:el |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3229 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3230 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3231 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3232 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3233 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3234 |
args: oldObject : <object> |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3235 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3236 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3237 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3238 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3239 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3240 |
" |
8874 | 3241 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3242 |
#(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
|
3243 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3244 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3245 |
|
602 | 3246 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3247 |
"return a copy of the receiver, where the elements from startIndex to |
|
3248 |
endIndex have been replaced by the elements of aCollection" |
|
3249 |
||
3250 |
|newColl sz replSize| |
|
3251 |
||
3252 |
replSize := aCollection size. |
|
3253 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
3254 |
newColl := self copyEmptyAndGrow:sz. |
|
3255 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3256 |
newColl replaceFrom:startIndex with:aCollection. |
|
3257 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3258 |
^ newColl |
|
3259 |
||
3260 |
" |
|
3261 |
#(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 | 3262 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 3263 |
" |
3264 |
! |
|
3265 |
||
3266 |
copyReplaceFrom:startIndex with:aCollection |
|
3267 |
"return a copy of the receiver, where the elements from startIndex to |
|
3268 |
the end have been replaced by the elements of aCollection" |
|
3269 |
||
3270 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
3271 |
||
3272 |
" |
|
8874 | 3273 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
3274 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 3275 |
" |
3276 |
! |
|
3277 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3278 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3279 |
"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
|
3280 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3281 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3282 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3283 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3284 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3285 |
" |
8874 | 3286 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
3287 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3288 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3289 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3290 |
|
602 | 3291 |
copyThrough:element |
3292 |
"return a new collection consisting of the receiver elements |
|
8874 | 3293 |
up-to (AND including) the first occurrence of element." |
602 | 3294 |
|
3295 |
|idx| |
|
3296 |
||
3297 |
idx := self indexOf:element. |
|
3298 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
3299 |
^ self copyFrom:1 to:idx |
|
3300 |
||
3301 |
" |
|
3302 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
3303 |
'1234567890' copyThrough:$5 |
|
3304 |
'1234567890' copyThrough:$a |
|
3305 |
'1234567890' copyThrough:$1 |
|
3306 |
" |
|
3307 |
! |
|
3308 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3309 |
copyTo:stopIndex |
9234 | 3310 |
"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
|
3311 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3312 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3313 |
^ self copyFrom:1 to:stopIndex |
602 | 3314 |
|
3315 |
" |
|
3316 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
3317 |
'1234567890' copyTo:4 |
|
3318 |
" |
|
3319 |
! |
|
3320 |
||
5526 | 3321 |
copyToMax:stop |
3322 |
"return a new collection consisting of receivers elements |
|
8874 | 3323 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 3324 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
3325 |
||
3326 |
^ self copyFrom:1 to:(self size min:stop) |
|
3327 |
||
3328 |
" |
|
3329 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
3330 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
3331 |
" |
|
3332 |
! |
|
3333 |
||
11844 | 3334 |
copyTransliterating:oldElements to:newElements |
3335 |
"return a copy, where elements all transliterated via the given mapping. |
|
3336 |
The transiteraion works like perl's tr operator. |
|
3337 |
||
3338 |
Transliterates all occurrences of the characters found in the search list with the |
|
11880 | 3339 |
corresponding character in the replacement list. |
11844 | 3340 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
3341 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3342 |
||
3343 |
Note that tr does not do other character escapes such as \n. |
|
3344 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3345 |
||
3346 |
Note also that the whole range idea is rather unportable between character sets, |
|
3347 |
and even within character sets they may cause results you probably didn't expect. |
|
3348 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3349 |
case (a-e, A-E), or digits (0-4). |
|
3350 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3351 |
" |
|
3352 |
||
3353 |
^ self |
|
3354 |
copyTransliterating:oldElements to:newElements |
|
3355 |
complement:false squashDuplicates:false |
|
3356 |
||
3357 |
" |
|
3358 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
|
3359 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3360 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3361 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3362 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11880 | 3363 |
|
3364 |
can also be used to remove character: |
|
3365 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'' |
|
3366 |
||
3367 |
'abcdefg |
|
3368 |
hijkl1 |
|
3369 |
234567890' copyTransliterating:(Array with:Character cr) to:'' |
|
3370 |
||
11844 | 3371 |
" |
3372 |
! |
|
3373 |
||
3374 |
copyTransliterating:oldElements to:newElements |
|
3375 |
complement:complement squashDuplicates:squashDuplicates |
|
3376 |
||
3377 |
"return a copy, where elements all transliterated via the given mapping. |
|
3378 |
The transiteraion works like perl's tr operator. |
|
3379 |
||
3380 |
Transliterates all occurrences of the characters found in the search list with the |
|
3381 |
corresponding character in the replacement list. It returns the number of characters replaced |
|
3382 |
or deleted. |
|
3383 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
|
3384 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3385 |
||
3386 |
Note that tr does not do other character escapes such as \n. |
|
3387 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3388 |
||
3389 |
Note also that the whole range idea is rather unportable between character sets, |
|
3390 |
and even within character sets they may cause results you probably didn't expect. |
|
3391 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3392 |
case (a-e, A-E), or digits (0-4). |
|
3393 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3394 |
||
3395 |
If complement is true, non-matching chars are affected. |
|
3396 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
|
3397 |
" |
|
3398 |
||
11875 | 3399 |
|map s1 s2 range1 range2 c1 c2 outStream prevXlated last inc| |
11844 | 3400 |
|
3401 |
map := Dictionary new. |
|
3402 |
||
3403 |
s1 := oldElements readStream. |
|
3404 |
s2 := newElements readStream. |
|
3405 |
range1 := range2 := nil. |
|
3406 |
[ |
|
3407 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
|
3408 |
range1 notNil or:[ s1 atEnd not ] |
|
3409 |
] whileTrue:[ |
|
3410 |
range1 notNil ifTrue:[ |
|
3411 |
c1 := range1 next. |
|
3412 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3413 |
] ifFalse:[ |
|
3414 |
c1 := s1 next. |
|
3415 |
s1 peek == $\ ifTrue:[ |
|
3416 |
s1 next. |
|
3417 |
] ifFalse:[ |
|
3418 |
s1 peek == $- ifTrue:[ |
|
3419 |
s1 next. |
|
11875 | 3420 |
last := s1 next. |
3421 |
inc := c1 < last ifTrue:[1] ifFalse:[-1]. |
|
3422 |
range1 := (c1 to:last by:inc) readStream. |
|
11844 | 3423 |
c1 := range1 next. |
3424 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3425 |
]. |
|
3426 |
]. |
|
3427 |
]. |
|
3428 |
range2 notNil ifTrue:[ |
|
3429 |
c2 := range2 next. |
|
3430 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3431 |
] ifFalse:[ |
|
3432 |
s2 atEnd ifTrue:[ |
|
3433 |
"/ keep c2 |
|
3434 |
] ifFalse:[ |
|
3435 |
c2 := s2 next. |
|
3436 |
s2 peek == $\ ifTrue:[ |
|
3437 |
s1 next. |
|
3438 |
] ifFalse:[ |
|
3439 |
s2 peek == $- ifTrue:[ |
|
3440 |
s2 next. |
|
11875 | 3441 |
last := s2 next. |
3442 |
inc := c2 < last ifTrue:[1] ifFalse:[-1]. |
|
3443 |
range2 := (c2 to:last by:inc) readStream. |
|
11844 | 3444 |
c2 := range2 next. |
3445 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3446 |
]. |
|
3447 |
]. |
|
3448 |
]. |
|
3449 |
]. |
|
3450 |
map at:c1 put:c2 |
|
3451 |
]. |
|
3452 |
||
3453 |
outStream := WriteStream on:(self species new). |
|
3454 |
self do:[:el | |
|
3455 |
|xLated out| |
|
3456 |
||
3457 |
complement ifTrue:[ |
|
3458 |
(map includesKey:el) ifTrue:[ |
|
3459 |
xLated := out := el. |
|
3460 |
] ifFalse:[ |
|
3461 |
out := newElements at:1 ifAbsent:nil. |
|
3462 |
]. |
|
3463 |
] ifFalse:[ |
|
3464 |
(map includesKey:el) ifTrue:[ |
|
3465 |
xLated := out := map at:el. |
|
3466 |
] ifFalse:[ |
|
3467 |
out := el. |
|
3468 |
]. |
|
3469 |
]. |
|
3470 |
out notNil ifTrue:[ |
|
3471 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
|
3472 |
"/ ignore |
|
3473 |
] ifFalse:[ |
|
3474 |
outStream nextPut:out. |
|
3475 |
]. |
|
3476 |
prevXlated := out. |
|
3477 |
]. |
|
3478 |
]. |
|
3479 |
^ outStream contents |
|
3480 |
||
3481 |
" |
|
11875 | 3482 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
11844 | 3483 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
3484 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3485 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3486 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3487 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11875 | 3488 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
3489 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
|
3490 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
|
11844 | 3491 |
|
3492 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3493 |
complement:false squashDuplicates:false |
|
3494 |
||
3495 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3496 |
complement:false squashDuplicates:true |
|
3497 |
||
3498 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3499 |
complement:false squashDuplicates:false |
|
3500 |
||
3501 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3502 |
complement:true squashDuplicates:false |
|
3503 |
||
3504 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3505 |
complement:true squashDuplicates:false |
|
3506 |
||
3507 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3508 |
complement:false squashDuplicates:false |
|
3509 |
||
3510 |
- delete all a-zA-Z |
|
3511 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3512 |
complement:false squashDuplicates:false |
|
3513 |
||
3514 |
- delete all except a-zA-Z |
|
3515 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3516 |
complement:true squashDuplicates:false |
|
3517 |
||
3518 |
- squash multiple spaces |
|
3519 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
|
3520 |
complement:false squashDuplicates:true |
|
3521 |
||
3522 |
- squash multiple digits |
|
3523 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
|
3524 |
complement:false squashDuplicates:true |
|
3525 |
" |
|
3526 |
! |
|
3527 |
||
602 | 3528 |
copyUpTo:element |
3529 |
"return a new collection consisting of the receiver elements |
|
8874 | 3530 |
up-to (but excluding) the first occurrence of element; |
602 | 3531 |
or to the end, if element is not included" |
3532 |
||
3533 |
|idx| |
|
3534 |
||
3535 |
idx := self indexOf:element. |
|
3536 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3537 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
3538 |
^ self copyFrom:1 to:(idx-1) |
|
3539 |
||
3540 |
" |
|
3541 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
3542 |
'1234567890' copyUpTo:$5 |
|
3543 |
'1234567890' copyUpTo:$a |
|
3544 |
'1234567890' copyUpTo:$1 |
|
3545 |
'123456789' copyUpTo:$0 |
|
3546 |
" |
|
3547 |
! |
|
3548 |
||
1 | 3549 |
copyWith:newElement |
233 | 3550 |
"return a new collection containing the receivers elements |
8874 | 3551 |
and the single new element, newElement. |
233 | 3552 |
This is different from concatentation, which expects another collection |
3553 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3554 |
|
3555 |
|newCollection mySize newSize| |
|
3556 |
||
3557 |
mySize := self size. |
|
3558 |
newSize := mySize + 1. |
|
252 | 3559 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3560 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3561 |
newCollection at:newSize put:newElement. |
|
3562 |
^newCollection |
|
44 | 3563 |
|
95 | 3564 |
" |
3565 |
#(1 2 3 4 5) copyWith:$a |
|
3566 |
'abcdefg' copyWith:$h |
|
3567 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3568 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3569 |
" |
|
1 | 3570 |
! |
3571 |
||
11855 | 3572 |
copyWithFirst:newFirstElement |
3573 |
"return a new collection containing the receivers elements |
|
3574 |
and the single new element, newElement up front. |
|
3575 |
This is different from concatentation, which expects two collections |
|
3576 |
as argument, but equivalent to copy-and-addFirst." |
|
3577 |
||
3578 |
|newCollection mySize newSize| |
|
3579 |
||
3580 |
mySize := self size. |
|
3581 |
newSize := mySize + 1. |
|
3582 |
newCollection := self copyEmptyAndGrow:newSize. |
|
3583 |
newCollection replaceFrom:2 to:mySize+1 with:self startingAt:1. |
|
3584 |
newCollection at:1 put:newFirstElement. |
|
3585 |
^newCollection |
|
3586 |
||
3587 |
" |
|
3588 |
#(1 2 3 4 5) copyWithFirst:$a |
|
3589 |
'abcdefg' copyWithFirst:$h |
|
3590 |
'abcdefg' copyWithFirst:'123' -- will fail: string cannot be stored into string |
|
3591 |
'abcdefg' copyWithFirst:1 -- will fail: integer cannot be stored into string |
|
3592 |
" |
|
3593 |
! |
|
3594 |
||
61 | 3595 |
copyWithout:elementToSkip |
3596 |
"return a new collection consisting of a copy of the receiver, with |
|
3597 |
ALL elements equal to elementToSkip are left out. |
|
3598 |
No error is reported, if elementToSkip is not in the collection." |
|
3599 |
||
293 | 3600 |
|n "{ Class: SmallInteger }" |
3601 |
sz "{ Class: SmallInteger }" |
|
3602 |
srcIndex "{ Class: SmallInteger }" |
|
3603 |
dstIndex "{ Class: SmallInteger }" |
|
3604 |
skipIndex "{ Class: SmallInteger }" |
|
3605 |
copy l| |
|
61 | 3606 |
|
8874 | 3607 |
"the code below may look like overkill, |
61 | 3608 |
however, for big collections its better to move data |
3609 |
around in big chunks" |
|
3610 |
||
3611 |
n := self occurrencesOf:elementToSkip. |
|
3612 |
n == 0 ifTrue:[^ self copy]. |
|
3613 |
||
3614 |
sz := self size. |
|
252 | 3615 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3616 |
|
3617 |
srcIndex := 1. |
|
3618 |
dstIndex := 1. |
|
3619 |
||
3620 |
n timesRepeat:[ |
|
8874 | 3621 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3622 |
l := skipIndex - srcIndex. |
|
3623 |
l ~~ 0 ifTrue:[ |
|
3624 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3625 |
with:self startingAt:srcIndex. |
|
3626 |
dstIndex := dstIndex + l |
|
3627 |
]. |
|
3628 |
srcIndex := skipIndex + 1 |
|
61 | 3629 |
]. |
3630 |
l := sz - srcIndex. |
|
3631 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3632 |
with:self startingAt:srcIndex. |
61 | 3633 |
^ copy |
3634 |
||
95 | 3635 |
" |
3636 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3637 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3638 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3639 |
'abcdefghi' copyWithout:$h |
3640 |
'abcdefg' copyWithout:$h |
|
95 | 3641 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3642 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3643 |
#(90 80 70 60 50) copyWithout:70 |
|
3644 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3645 |
" |
|
61 | 3646 |
! |
3647 |
||
602 | 3648 |
copyWithoutFirst:elementToSkip |
3649 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3650 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3651 |
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
|
3652 |
Do not confuse this with copyButFirst:" |
602 | 3653 |
|
3654 |
|copy skipIndex sz| |
|
3655 |
||
3656 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3657 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3658 |
||
3659 |
sz := self size - 1. |
|
3660 |
copy := self copyEmptyAndGrow:sz. |
|
3661 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3662 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3663 |
^ copy |
|
3664 |
||
3665 |
" |
|
3666 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3667 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3668 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3669 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3670 |
" |
|
3671 |
! |
|
3672 |
||
8673 | 3673 |
copyWithoutIdentical:elementToSkip |
3674 |
"return a new collection consisting of a copy of the receiver, with |
|
3675 |
ALL elements identical to elementToSkip are left out. |
|
3676 |
No error is reported, if elementToSkip is not in the collection." |
|
3677 |
||
3678 |
|n "{ Class: SmallInteger }" |
|
3679 |
sz "{ Class: SmallInteger }" |
|
3680 |
srcIndex "{ Class: SmallInteger }" |
|
3681 |
dstIndex "{ Class: SmallInteger }" |
|
3682 |
skipIndex "{ Class: SmallInteger }" |
|
3683 |
copy l| |
|
3684 |
||
8874 | 3685 |
"the code below may look like overkill, |
8673 | 3686 |
however, for big collections its better to move data |
3687 |
around in big chunks" |
|
3688 |
||
3689 |
n := self count:[:el | el == elementToSkip]. |
|
3690 |
n == 0 ifTrue:[^ self copy]. |
|
3691 |
||
3692 |
sz := self size. |
|
3693 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3694 |
||
3695 |
srcIndex := 1. |
|
3696 |
dstIndex := 1. |
|
3697 |
||
3698 |
n timesRepeat:[ |
|
8874 | 3699 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3700 |
l := skipIndex - srcIndex. |
|
3701 |
l ~~ 0 ifTrue:[ |
|
3702 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3703 |
with:self startingAt:srcIndex. |
|
3704 |
dstIndex := dstIndex + l |
|
3705 |
]. |
|
3706 |
srcIndex := skipIndex + 1 |
|
8673 | 3707 |
]. |
3708 |
l := sz - srcIndex. |
|
3709 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3710 |
with:self startingAt:srcIndex. |
8673 | 3711 |
^ copy |
3712 |
||
3713 |
" |
|
8874 | 3714 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3715 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3716 |
" |
3717 |
! |
|
3718 |
||
61 | 3719 |
copyWithoutIndex:omitIndex |
3720 |
"return a new collection consisting of receivers elements |
|
3721 |
without the argument stored at omitIndex" |
|
3722 |
||
3723 |
|copy sz| |
|
3724 |
||
202 | 3725 |
sz := self size - 1. |
252 | 3726 |
copy := self copyEmptyAndGrow:sz. |
293 | 3727 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3728 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3729 |
^ copy |
3730 |
||
95 | 3731 |
" |
3732 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3733 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3734 |
" |
|
1 | 3735 |
! |
3736 |
||
202 | 3737 |
copyWithoutLast:count |
3738 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3739 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3740 |
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
|
3741 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3742 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3743 |
|
9250 | 3744 |
<resource:#obsolete> |
3745 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3746 |
^ self copyTo:(self size - count) |
202 | 3747 |
|
3748 |
" |
|
8874 | 3749 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3750 |
'1234567890' copyWithoutLast:4 |
|
202 | 3751 |
" |
5231 | 3752 |
! |
3753 |
||
3754 |
restAfter:anElement |
|
8366 | 3755 |
"return a new collection consisting of the receivers elements after |
5231 | 3756 |
(but excluding) anElement. |
8366 | 3757 |
If anElement is not in the receiver, the returned collection |
3758 |
will be empty. |
|
3759 |
See also #upTo:." |
|
5231 | 3760 |
|
3761 |
|pos| |
|
3762 |
||
3763 |
pos := self indexOf:anElement. |
|
8366 | 3764 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3765 |
||
5231 | 3766 |
^ self copyFrom:(pos + 1) |
3767 |
||
3768 |
" |
|
8874 | 3769 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3770 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3771 |
'hello world' upTo:Character space |
|
3772 |
'hello world' restAfter:Character space |
|
3773 |
'1234.5678' upTo:$. |
|
3774 |
'1234.5678' restAfter:$. |
|
8366 | 3775 |
" |
5231 | 3776 |
! |
3777 |
||
12187 | 3778 |
trimForWhich:aCheckBlock |
3779 |
"return a copy of myself without leading and trailing elements for which aCheckBlock returns true. |
|
3780 |
Normally, this is mostly used with string receivers." |
|
3781 |
||
3782 |
|startIndex "{ Class: SmallInteger }" |
|
3783 |
endIndex "{ Class: SmallInteger }" |
|
3784 |
sz| |
|
3785 |
||
3786 |
sz := self size. |
|
3787 |
startIndex := 1. |
|
3788 |
endIndex := sz. |
|
3789 |
||
3790 |
[(startIndex < endIndex) and:[aCheckBlock value:(self at:startIndex)]] whileTrue:[ |
|
3791 |
startIndex := startIndex + 1 |
|
3792 |
]. |
|
3793 |
[(endIndex > 1) and:[aCheckBlock value:(self at:endIndex)]] whileTrue:[ |
|
3794 |
endIndex := endIndex - 1 |
|
3795 |
]. |
|
3796 |
startIndex > endIndex ifTrue:[ |
|
3797 |
^ self class new |
|
3798 |
]. |
|
3799 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
|
3800 |
^ self |
|
3801 |
]. |
|
3802 |
^ self copyFrom:startIndex to:endIndex |
|
3803 |
||
3804 |
" |
|
3805 |
' foo ' trimForWhich:[:ch | ch isSeparator] |
|
3806 |
#(0 0 0 1 2 3 0 0 0) trimForWhich:[:e | e = 0] |
|
3807 |
" |
|
3808 |
! |
|
3809 |
||
5231 | 3810 |
upTo:anElement |
3811 |
"return a new collection consisting of the receivers elements upTo |
|
3812 |
(but excluding) anElement. |
|
3813 |
If anElement is not in the receiver, the returned collection |
|
3814 |
will consist of all elements of the receiver. |
|
9307 | 3815 |
See also #restAfter: , #copyFrom:index." |
5231 | 3816 |
|
3817 |
|pos| |
|
3818 |
||
3819 |
pos := self indexOf:anElement. |
|
3820 |
pos == 0 ifTrue:[^ self copy]. |
|
3821 |
||
3822 |
^ self copyFrom:1 to:(pos - 1) |
|
3823 |
||
3824 |
" |
|
8874 | 3825 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3826 |
'hello world' upTo:Character space |
|
3827 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3828 |
'1234.5678' upTo:$. |
|
3829 |
'1234' upTo:$. |
|
5231 | 3830 |
|
3831 |
raises an error: |
|
3832 |
||
8874 | 3833 |
(Dictionary new |
9307 | 3834 |
at:#foo put:'foo'; |
3835 |
at:#bar put:'bar'; |
|
3836 |
at:#baz put:'baz'; |
|
3837 |
yourself) upTo:#bar |
|
8874 | 3838 |
|
5231 | 3839 |
" |
3840 |
||
3841 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3842 |
! |
|
3843 |
||
3844 |
upTo:anElement count:count |
|
3845 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3846 |
(but excluding) count found anElements, i.e. the first found count-1 |
3847 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3848 |
procedure is done from the end of the collection backwards. |
3849 |
If anElement is not in the receiver, the returned collection |
|
3850 |
will consist of all elements of the receiver" |
|
3851 |
||
3852 |
|startPos endPos pos found| |
|
3853 |
||
3854 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3855 |
^ self upTo:anElement |
5231 | 3856 |
]. |
3857 |
||
3858 |
startPos := 1. |
|
3859 |
endPos := self size. |
|
3860 |
found := 0. |
|
3861 |
||
3862 |
count > 1 ifTrue:[ |
|
8874 | 3863 |
pos := 0. |
3864 |
[pos < self size and: [found < count]] |
|
3865 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3866 |
found == count ifTrue:[ |
|
3867 |
endPos := pos - 1 |
|
3868 |
] |
|
3869 |
]. |
|
5231 | 3870 |
|
3871 |
count < 0 ifTrue:[ |
|
8874 | 3872 |
pos := self size + 1. |
3873 |
[pos > 1 and: [found < count abs]] |
|
3874 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3875 |
found == count abs ifTrue:[ |
|
3876 |
startPos := pos + 1 |
|
3877 |
] |
|
3878 |
]. |
|
5231 | 3879 |
|
3880 |
^ self copyFrom: startPos to: endPos |
|
3881 |
||
3882 |
" |
|
8874 | 3883 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3884 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3885 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3886 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3887 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3888 |
" |
|
5563 | 3889 |
! |
3890 |
||
3891 |
upToAny:aCollectionOfObjects |
|
3892 |
"return a new collection consisting of the receivers elements upTo |
|
3893 |
(but excluding) any in aCollectionOfObjects. |
|
3894 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3895 |
will consist of all elements of the receiver. |
|
3896 |
See also #upTo:" |
|
3897 |
||
3898 |
|pos| |
|
3899 |
||
3900 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3901 |
pos == 0 ifTrue:[^ self copy]. |
|
3902 |
||
3903 |
^ self copyFrom:1 to:(pos - 1) |
|
3904 |
||
3905 |
" |
|
8874 | 3906 |
'hello world' upToAny:#($/ $:) |
3907 |
'hello:world' upToAny:#($/ $:) |
|
3908 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3909 |
" |
12187 | 3910 |
! |
3911 |
||
3912 |
withoutLeadingForWhich:aCheckBlock |
|
3913 |
"return a copy of myself without leading eleents for which aCheckBlock returns true. |
|
3914 |
Returns an empty collection, if the receiver consist only of matching elements. |
|
3915 |
Normally, this is mostly used with string receivers." |
|
3916 |
||
3917 |
|index sz| |
|
3918 |
||
3919 |
sz := self size. |
|
3920 |
index := 1. |
|
3921 |
[index <= sz] whileTrue:[ |
|
3922 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
3923 |
index == 1 ifTrue:[^ self]. |
|
3924 |
^ self copyFrom:index |
|
3925 |
]. |
|
3926 |
index := index + 1 |
|
3927 |
]. |
|
3928 |
^ self class new |
|
3929 |
||
3930 |
" |
|
3931 |
'****foo****' withoutLeadingForWhich:[:ch | ch isLetter not] |
|
3932 |
#( 0 0 0 1 2 3 4 0 0 0) withoutLeadingForWhich:[:e | e = 0] |
|
3933 |
" |
|
3934 |
! |
|
3935 |
||
3936 |
withoutTrailingForWhich:aCheckBlock |
|
3937 |
"return a copy of myself without trailing characters for which aCheckBlock returns true. |
|
3938 |
Returns an empty collection, if the receiver consist only of matching chars." |
|
3939 |
||
3940 |
|index sz| |
|
3941 |
||
3942 |
index := sz := self size. |
|
3943 |
[index ~~ 0] whileTrue:[ |
|
3944 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
3945 |
index == sz ifTrue:[^ self]. |
|
3946 |
^ self copyTo:index |
|
3947 |
]. |
|
3948 |
index := index - 1 |
|
3949 |
]. |
|
3950 |
^ self class new |
|
3951 |
||
3952 |
" |
|
3953 |
' foo....' withoutTrailingForWhich:[:ch | ch isLetter not]. |
|
3954 |
#( 0 0 0 1 2 3 4 0 0 0) withoutTrailingForWhich:[:e | e = 0] |
|
3955 |
" |
|
602 | 3956 |
! ! |
3957 |
||
3958 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3959 |
||
3960 |
collect:aBlock |
|
3961 |
"evaluate the argument, aBlock for every element in the collection |
|
3962 |
and return a collection of the results" |
|
3963 |
||
3964 |
|newCollection |
|
3965 |
sz "{ Class:SmallInteger }"| |
|
3966 |
||
3967 |
sz := self size. |
|
3968 |
newCollection := self copyEmptyAndGrow:sz. |
|
3969 |
1 to:sz do:[:index | |
|
8874 | 3970 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3971 |
]. |
3972 |
^ newCollection |
|
3973 |
||
3974 |
" |
|
8874 | 3975 |
#(one two three four five six) collect:[:element | element asUppercase] |
3976 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3977 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3978 |
" |
202 | 3979 |
! |
3980 |
||
11711 | 3981 |
collect:aBlock as:aClass |
11870 | 3982 |
"like collect, but use an instance of aClass to collect the results. |
3983 |
Also avoids the need for an extra intermediate collection which is created with |
|
3984 |
the standard coding: 'self asXXXX collect:[...]" |
|
11711 | 3985 |
|
3986 |
|newCollection |
|
3987 |
sz "{ Class:SmallInteger }"| |
|
3988 |
||
3989 |
sz := self size. |
|
3990 |
newCollection := (aClass new:sz) grow:sz. |
|
12007 | 3991 |
newCollection isSequenceable ifTrue:[ |
3992 |
1 to:sz do:[:index | |
|
3993 |
newCollection at:index put:(aBlock value:(self at:index)). |
|
3994 |
]. |
|
3995 |
] ifFalse:[ |
|
3996 |
1 to:sz do:[:index | |
|
3997 |
newCollection add:(aBlock value:(self at:index)). |
|
3998 |
]. |
|
11711 | 3999 |
]. |
4000 |
^ newCollection |
|
4001 |
||
4002 |
" |
|
4003 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
|
4004 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
|
4005 |
" |
|
4006 |
! |
|
4007 |
||
602 | 4008 |
do:aBlock |
4009 |
"evaluate the argument, aBlock for every element in the collection." |
|
4010 |
||
4011 |
|stop "{ Class:SmallInteger }"| |
|
4012 |
||
4013 |
stop := self size. |
|
4014 |
1 to:stop do:[:index | |
|
8874 | 4015 |
aBlock value:(self at:index). |
602 | 4016 |
] |
2927 | 4017 |
|
602 | 4018 |
" |
1422 | 4019 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 4020 |
" |
4021 |
! |
|
4022 |
||
2927 | 4023 |
do:aBlock separatedBy:sepBlock |
4024 |
"evaluate the argument, aBlock for every element in the collection. |
|
4025 |
Between each element (i.e. not before the first element and not after the |
|
4026 |
last element), evaluate sepBlock. |
|
4027 |
This supports printing of collections with elements separated by some |
|
4028 |
string." |
|
4029 |
||
4030 |
|stop "{ Class:SmallInteger }"| |
|
4031 |
||
4032 |
stop := self size. |
|
4033 |
stop < 1 ifTrue:[^ self]. |
|
4034 |
aBlock value:(self at:1). |
|
4035 |
2 to:stop do:[:index | |
|
8874 | 4036 |
sepBlock value. |
4037 |
aBlock value:(self at:index) |
|
2927 | 4038 |
]. |
4039 |
||
4040 |
" |
|
4041 |
#(one two three four five six) |
|
8874 | 4042 |
do:[:each | Transcript show:each] |
4043 |
separatedBy:[Transcript show:' , '] |
|
2927 | 4044 |
" |
4045 |
! |
|
3003 | 4046 |
|
3044 | 4047 |
doWithIndex:aBlock |
4048 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
4049 |
the index as second argument." |
|
4050 |
||
4051 |
self keysAndValuesDo:[:index :value | |
|
8874 | 4052 |
aBlock value:value value:index |
3044 | 4053 |
]. |
4054 |
||
4055 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
4056 |
! |
|
4057 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4058 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4059 |
"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
|
4060 |
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
|
4061 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4062 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4063 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4064 |
" |
8874 | 4065 |
#(one two three four five six) |
4066 |
from:2 |
|
4067 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4068 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4069 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4070 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4071 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4072 |
|
602 | 4073 |
from:startIndex do:aBlock |
4074 |
"evaluate the argument, aBlock for the elements starting with the |
|
4075 |
element at startIndex to the end." |
|
4076 |
||
4077 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 4078 |
|
95 | 4079 |
" |
8874 | 4080 |
#(one two three four five six) |
4081 |
from:3 |
|
4082 |
do:[:element | Transcript showCR:element] |
|
602 | 4083 |
" |
4084 |
! |
|
4085 |
||
7490 | 4086 |
from:startIndex keysAndValuesDo:aBlock |
4087 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
4088 |
element at startIndex to the end." |
|
4089 |
||
4090 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
4091 |
||
4092 |
" |
|
8874 | 4093 |
#(one two three four five six) |
4094 |
from:3 |
|
4095 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 4096 |
" |
4097 |
! |
|
4098 |
||
7337 | 4099 |
from:index1 to:index2 by:stepArg do:aBlock |
4100 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
4101 |
||
4102 |
|start "{ Class:SmallInteger }" |
|
4103 |
stop "{ Class:SmallInteger }" |
|
4104 |
step "{ Class:SmallInteger }" | |
|
4105 |
||
4106 |
start := index1. "/ these assignments force type checking... |
|
4107 |
stop := index2. "/ and guarantee inline loop code below. |
|
4108 |
step := stepArg. |
|
4109 |
||
4110 |
start to:stop by:step do:[:index | |
|
8874 | 4111 |
aBlock value:(self at:index). |
7337 | 4112 |
] |
4113 |
||
4114 |
" |
|
8874 | 4115 |
#(one two three four five six seven eight nine ten) |
4116 |
from:2 |
|
4117 |
to:10 |
|
4118 |
by:2 |
|
4119 |
do:[:element | Transcript showCR:element] |
|
7337 | 4120 |
" |
4121 |
! |
|
4122 |
||
602 | 4123 |
from:start to:stop collect:aBlock |
4124 |
"evaluate the argument, aBlock for the elements indexed by start |
|
4125 |
to stop in the collection and return a collection of the results" |
|
4126 |
||
4127 |
|newCollection sz |
|
4128 |
idx "{ Class:SmallInteger }"| |
|
4129 |
||
4130 |
sz := stop - start + 1. |
|
4131 |
newCollection := self copyEmptyAndGrow:sz. |
|
4132 |
idx := 1. |
|
4133 |
start to:stop do:[:index | |
|
8874 | 4134 |
newCollection at:idx put:(aBlock value:(self at:index)). |
4135 |
idx := idx + 1 |
|
602 | 4136 |
]. |
4137 |
^ newCollection |
|
4138 |
||
4139 |
" |
|
8874 | 4140 |
#(one two three four five six) |
4141 |
from:2 |
|
4142 |
to:4 |
|
4143 |
collect:[:element | element asUppercase] |
|
602 | 4144 |
" |
4145 |
! |
|
4146 |
||
4147 |
from:index1 to:index2 do:aBlock |
|
4148 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4149 |
index2 in the collection" |
|
4150 |
||
4151 |
|start "{ Class:SmallInteger }" |
|
4152 |
stop "{ Class:SmallInteger }" | |
|
4153 |
||
7337 | 4154 |
start := index1. "/ these assignments force type checking... |
4155 |
stop := index2. "/ and guarantee inline loop code below. |
|
4156 |
||
602 | 4157 |
start to:stop do:[:index | |
8874 | 4158 |
aBlock value:(self at:index). |
602 | 4159 |
] |
4160 |
||
4161 |
" |
|
8874 | 4162 |
#(one two three four five six) |
4163 |
from:3 |
|
4164 |
to:5 |
|
4165 |
do:[:element | Transcript showCR:element] |
|
602 | 4166 |
" |
4167 |
! |
|
4168 |
||
6868 | 4169 |
from:index1 to:index2 keysAndValuesDo:aBlock |
4170 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4171 |
index2 in the collection; pass both index and value." |
|
4172 |
||
4173 |
|start "{ Class:SmallInteger }" |
|
4174 |
stop "{ Class:SmallInteger }" | |
|
4175 |
||
7337 | 4176 |
start := index1. "/ these assignments force type checking... |
4177 |
stop := index2. "/ and guarantee inline loop code below. |
|
4178 |
||
6868 | 4179 |
start to:stop do:[:index | |
8874 | 4180 |
aBlock value:index value:(self at:index). |
6868 | 4181 |
] |
4182 |
||
4183 |
" |
|
8874 | 4184 |
#(one two three four five six) |
4185 |
from:3 |
|
4186 |
to:5 |
|
4187 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 4188 |
" |
4189 |
! |
|
4190 |
||
602 | 4191 |
from:index1 to:index2 reverseDo:aBlock |
4192 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4193 |
index2 in the collection. Step in reverse order" |
|
4194 |
||
4195 |
|start "{ Class:SmallInteger }" |
|
4196 |
stop "{ Class:SmallInteger }" | |
|
4197 |
||
7337 | 4198 |
start := index1. "/ these assignments force type checking... |
4199 |
stop := index2. "/ and guarantee inline loop code below. |
|
4200 |
||
602 | 4201 |
stop to:start by:-1 do:[:index | |
8874 | 4202 |
aBlock value:(self at:index). |
602 | 4203 |
] |
4204 |
||
4205 |
" |
|
8874 | 4206 |
#(one two three four five six) |
4207 |
from:3 |
|
4208 |
to:5 |
|
4209 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 4210 |
" |
61 | 4211 |
! |
4212 |
||
6027 | 4213 |
from:startIndex to:stopIndex select:aBlock |
4214 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
4215 |
and return a collection of those elements for which the block return true." |
|
4216 |
||
4217 |
|element newColl species needCopy |
|
4218 |
n "{ Class:SmallInteger }"| |
|
4219 |
||
4220 |
n := stopIndex - startIndex + 1. |
|
4221 |
species := self species. |
|
4222 |
species growIsCheap ifFalse:[ |
|
8874 | 4223 |
newColl := OrderedCollection new:n. |
4224 |
needCopy := true |
|
6027 | 4225 |
] ifTrue:[ |
8874 | 4226 |
newColl := self copyEmpty:n. |
4227 |
needCopy := false |
|
6027 | 4228 |
]. |
4229 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4230 |
element := self at:index. |
4231 |
(aBlock value:element) ifTrue:[ |
|
4232 |
newColl add:element |
|
4233 |
]. |
|
6027 | 4234 |
]. |
4235 |
needCopy ifTrue:[ |
|
8874 | 4236 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 4237 |
]. |
4238 |
^ newColl |
|
4239 |
||
4240 |
" |
|
8874 | 4241 |
#(faba one two three four five six) |
4242 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 4243 |
" |
4244 |
! |
|
4245 |
||
10150 | 4246 |
inGroupsOf:n collect:anNArgBlock |
4247 |
"evaluate the argument, anNArgBlock for every group of n elements in the collection, |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4248 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4249 |
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
|
4250 |
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
|
4251 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4252 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4253 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4254 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4255 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4256 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4257 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4258 |
|
10150 | 4259 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
4260 |
"/ allow for the compiler (jitter) to generate better code for the block-call |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4261 |
n == 2 ifTrue:[ |
10150 | 4262 |
1 to:stop by:n do:[:index | |
4263 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4264 |
newCollection at:dstIdx put:rslt. |
|
4265 |
dstIdx := dstIdx + 1. |
|
4266 |
]. |
|
4267 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4268 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4269 |
n == 3 ifTrue:[ |
10150 | 4270 |
1 to:stop by:n do:[:index | |
4271 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4272 |
newCollection at:dstIdx put:rslt. |
|
4273 |
dstIdx := dstIdx + 1. |
|
4274 |
]. |
|
4275 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4276 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4277 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4278 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4279 |
1 to:stop by:n do:[:index | |
10150 | 4280 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4281 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
4282 |
newCollection at:dstIdx put:rslt. |
|
4283 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4284 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4285 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4286 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4287 |
"for groups of 2, this is the same as: |
8874 | 4288 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4289 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 4290 |
|
4291 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4292 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4293 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4294 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4295 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
4296 |
" |
|
4297 |
||
4298 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
4299 |
! |
|
4300 |
||
4301 |
inGroupsOf:n do:anNArgBlock |
|
4302 |
"evaluate the argument, anNArgBlock for every group of n elements in the collection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4303 |
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
|
4304 |
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
|
4305 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4306 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4307 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4308 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4309 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4310 |
|
10150 | 4311 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
4312 |
"/ allow for the compiler (jitter) to generate better code for the block-call |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4313 |
n == 2 ifTrue:[ |
10150 | 4314 |
1 to:stop by:n do:[:index | |
4315 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4316 |
]. |
|
4317 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4318 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4319 |
n == 3 ifTrue:[ |
10150 | 4320 |
1 to:stop by:n do:[:index | |
4321 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4322 |
]. |
|
4323 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4324 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4325 |
n == 4 ifTrue:[ |
10150 | 4326 |
1 to:stop by:n do:[:index | |
4327 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
4328 |
]. |
|
4329 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4330 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4331 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4332 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4333 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4334 |
1 to:stop by:n do:[:index | |
10150 | 4335 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4336 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4337 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4338 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4339 |
"for groups of 2, this is the same as: |
8874 | 4340 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4341 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4342 |
|
4343 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4344 |
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
|
4345 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4346 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4347 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
4348 |
" |
|
4349 |
||
4350 |
"Modified: / 27-10-2006 / 10:07:55 / cg" |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4351 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4352 |
|
8853 | 4353 |
keysAndValuesCollect:aTwoArgBlock |
4354 |
"evaluate the argument, aBlock for every element in the collection, |
|
4355 |
passing both index and element as arguments. |
|
4356 |
Collect the returned values and return them." |
|
4357 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4358 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4359 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4360 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4361 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4362 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4363 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 4364 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4365 |
|
8853 | 4366 |
^ newCollection |
4367 |
||
4368 |
" |
|
8874 | 4369 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4370 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4371 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 4372 |
|
4373 |
||
4374 |
|d| |
|
4375 |
||
4376 |
d := Dictionary new. |
|
4377 |
d at:'a' put:'A'. |
|
4378 |
d at:'b' put:'B'. |
|
4379 |
d at:'c' put:'C'. |
|
4380 |
d at:'d' put:'D'. |
|
4381 |
d at:'e' put:'E'. |
|
8874 | 4382 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4383 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4384 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4385 |
ifFalse:[element asLowercase]] |
8853 | 4386 |
" |
4387 |
! |
|
4388 |
||
602 | 4389 |
keysAndValuesDo:aTwoArgBlock |
4390 |
"evaluate the argument, aBlock for every element in the collection, |
|
4391 |
passing both index and element as arguments." |
|
4392 |
||
4393 |
|stop "{ Class:SmallInteger }"| |
|
4394 |
||
4395 |
stop := self size. |
|
4396 |
1 to:stop do:[:index | |
|
8874 | 4397 |
aTwoArgBlock value:index value:(self at:index). |
602 | 4398 |
] |
4399 |
" |
|
8874 | 4400 |
#(one two three four five six) |
4401 |
keysAndValuesDo:[:key :element | |
|
4402 |
Transcript show:key; space; showCR:element |
|
4403 |
] |
|
602 | 4404 |
" |
4405 |
! |
|
4406 |
||
1359 | 4407 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 4408 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 4409 |
in the collection, passing both index and element as arguments." |
4410 |
||
4411 |
|stop "{ Class:SmallInteger }"| |
|
4412 |
||
4413 |
stop := self size. |
|
4414 |
stop to:1 by:-1 do:[:index | |
|
8874 | 4415 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 4416 |
] |
4417 |
||
4418 |
" |
|
8874 | 4419 |
#(one two three four five six) |
4420 |
keysAndValuesReverseDo:[:key :element | |
|
4421 |
Transcript show:key; space; showCR:element |
|
4422 |
] |
|
1359 | 4423 |
" |
4424 |
||
4425 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
4426 |
! |
|
4427 |
||
602 | 4428 |
nonNilElementsDo:aBlock |
4429 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
4430 |
||
4431 |
|stop "{ Class:SmallInteger }" |
|
4432 |
element| |
|
4433 |
||
4434 |
stop := self size. |
|
4435 |
1 to:stop do:[:index | |
|
8874 | 4436 |
(element := self at:index) notNil ifTrue:[ |
4437 |
aBlock value:element. |
|
4438 |
] |
|
602 | 4439 |
] |
4440 |
" |
|
1422 | 4441 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 4442 |
" |
4443 |
! |
|
4444 |
||
10150 | 4445 |
pairWiseCollect:aTwoArgBlock |
4446 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 4447 |
The block is called with 2 arguments for each 2 elements in the receiver. |
4448 |
An error will be reported, if the number of elements in the receiver |
|
4449 |
is not a multiple of 2. |
|
4450 |
Collect the results and return a new collection containing those." |
|
4451 |
||
10150 | 4452 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 4453 |
|
95 | 4454 |
" |
8874 | 4455 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4456 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 4457 |
|
4458 |
||
4459 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4460 |
pairWiseCollect:[:x :y | x@y] |
4461 |
" |
|
4462 |
||
4463 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
4464 |
! |
|
4465 |
||
4466 |
pairWiseDo:aTwoArgBlock |
|
10385 | 4467 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
4468 |
An error will be reported, if the number of elements in the receiver |
|
602 | 4469 |
is not a multiple of 2." |
4470 |
||
10150 | 4471 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4472 |
|
602 | 4473 |
" |
8874 | 4474 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4475 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4476 |
|
4477 |
||
4478 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4479 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
4480 |
" |
|
4481 |
||
10385 | 4482 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 4483 |
! |
4484 |
||
602 | 4485 |
reverseDo:aBlock |
4486 |
"evaluate the argument, aBlock for every element in the collection |
|
4487 |
in reverse order" |
|
4488 |
||
4489 |
|sz "{ Class:SmallInteger }"| |
|
4490 |
||
4491 |
sz := self size. |
|
4492 |
sz to:1 by:-1 do:[:index | |
|
8874 | 4493 |
aBlock value:(self at:index). |
602 | 4494 |
] |
4495 |
||
4496 |
" |
|
1422 | 4497 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 4498 |
" |
4499 |
! |
|
4500 |
||
4501 |
select:aBlock |
|
4502 |
"evaluate the argument, aBlock for every element in the collection |
|
4503 |
and return a collection of all elements for which the block return |
|
4504 |
true" |
|
4505 |
||
4506 |
|element newColl species needCopy |
|
4507 |
sz "{ Class:SmallInteger }"| |
|
4508 |
||
4509 |
sz := self size. |
|
4510 |
species := self species. |
|
4511 |
species growIsCheap ifFalse:[ |
|
8874 | 4512 |
newColl := OrderedCollection new:sz. |
4513 |
needCopy := true |
|
602 | 4514 |
] ifTrue:[ |
8874 | 4515 |
newColl := self copyEmpty:sz. |
4516 |
needCopy := false |
|
602 | 4517 |
]. |
4518 |
1 to:sz do:[:index | |
|
8874 | 4519 |
element := self at:index. |
4520 |
(aBlock value:element) ifTrue:[ |
|
4521 |
newColl add:element |
|
4522 |
]. |
|
602 | 4523 |
]. |
4524 |
needCopy ifTrue:[ |
|
8874 | 4525 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 4526 |
]. |
61 | 4527 |
^ newColl |
4528 |
||
95 | 4529 |
" |
8874 | 4530 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
4531 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
4532 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
4533 |
(1 to:9) select:[:element | element odd] |
|
4534 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 4535 |
" |
186 | 4536 |
! |
4537 |
||
602 | 4538 |
with:aSequenceableCollection do:aTwoArgBlock |
4539 |
"evaluate the argument, aBlock for successive elements from |
|
4540 |
each the receiver and the argument, aSequenceableCollection. |
|
4541 |
The second argument, aBlock must be a two-argument block. |
|
4542 |
The collection argument must implement access via a numeric key." |
|
4543 |
||
4544 |
|stop "{ Class: SmallInteger }" | |
|
4545 |
||
4546 |
stop := self size. |
|
4547 |
1 to:stop do:[:index | |
|
8874 | 4548 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 4549 |
] |
186 | 4550 |
" |
8874 | 4551 |
#(one two three four five six) |
4552 |
with:(1 to:10) |
|
4553 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 4554 |
" |
9454 | 4555 |
! |
4556 |
||
4557 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
4558 |
"evaluate the argument, aBlock for successive elements from |
|
4559 |
each the receiver and the argument, aSequenceableCollection. |
|
4560 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4561 |
the index as arguments. |
|
4562 |
The collection argument must implement access via a numeric key." |
|
4563 |
||
4564 |
|stop "{ Class: SmallInteger }" | |
|
4565 |
||
4566 |
stop := self size. |
|
4567 |
1 to:stop do:[:index | |
|
4568 |
aThreeArgBlock |
|
4569 |
value:(self at:index) |
|
4570 |
value:(aSequenceableCollection at:index) |
|
4571 |
value:index. |
|
4572 |
] |
|
4573 |
" |
|
4574 |
#(one two three four five six) |
|
4575 |
with:(1 to:10) |
|
4576 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4577 |
" |
|
4578 |
||
4579 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 4580 |
! ! |
4581 |
||
4154 | 4582 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 4583 |
|
602 | 4584 |
atAllPut:anObject |
4585 |
"replace all elements of the collection by the argument, anObject. |
|
4586 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4587 |
therefore the change may affect all others referencing the receiver." |
|
4588 |
||
4589 |
self from:1 to:(self size) put:anObject |
|
4590 |
||
4591 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4592 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4593 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4594 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4595 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4596 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4597 |
" |
602 | 4598 |
(Array new:10) atAllPut:1 |
4599 |
(String new:10) atAllPut:$a |
|
4600 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4601 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4602 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 4603 |
! |
4604 |
||
1 | 4605 |
from:index1 to:index2 put:anObject |
4606 |
"replace the elements from index1 to index2 of the collection |
|
44 | 4607 |
by the argument, anObject. |
4608 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4609 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4610 |
|
4611 |
|index "{ Class: SmallInteger }" |
|
4612 |
end "{ Class: SmallInteger }"| |
|
4613 |
||
4614 |
index := index1. |
|
4615 |
end := index2. |
|
4616 |
[index <= end] whileTrue:[ |
|
8874 | 4617 |
self at:index put:anObject. |
4618 |
index := index + 1 |
|
1 | 4619 |
] |
44 | 4620 |
|
95 | 4621 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4622 |
args: index1 : <integer> |
8874 | 4623 |
index2 : <integer> |
4624 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4625 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4626 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4627 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4628 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4629 |
" |
95 | 4630 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
4631 |
'abcdefghijkl' from:3 to:6 put:$X |
|
4632 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4633 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4634 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 4635 |
! |
4636 |
||
3003 | 4637 |
replaceAll:oldObject by:newObject |
4638 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
4639 |
Pleace do no longer use this one; |
|
4640 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
4641 |
||
5872 | 4642 |
<resource:#obsolete> |
4643 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4644 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 4645 |
^ self replaceAll:oldObject with:newObject |
4646 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4647 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4648 |
args: oldObject : <object> |
8874 | 4649 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4650 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4651 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4652 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4653 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4654 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 4655 |
! |
4656 |
||
4657 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
4658 |
"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
|
4659 |
Please do no longer use this one; |
3003 | 4660 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
4661 |
||
5872 | 4662 |
<resource:#obsolete> |
4663 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4664 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 4665 |
^ 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
|
4666 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4667 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4668 |
args: oldObject : <object> |
8874 | 4669 |
newObject : <object> |
4670 |
startIndex : <integer> |
|
4671 |
stopIndex : <integer> |
|
4672 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4673 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4674 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4675 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4676 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 4677 |
! |
4678 |
||
2927 | 4679 |
replaceAll:oldObject with:newObject |
44 | 4680 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4681 |
|
44 | 4682 |
Notice: This operation modifies the receiver, NOT a copy; |
4683 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4684 |
|
8874 | 4685 |
^ self |
4686 |
replaceAll:oldObject |
|
4687 |
with:newObject |
|
4688 |
from:1 |
|
4689 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4690 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4691 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4692 |
args: oldObject : <object> |
8874 | 4693 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4694 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4695 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4696 |
" |
362 | 4697 |
|
4698 |
" |
|
8874 | 4699 |
'123123abc123' replaceAll:$1 with:$* |
4700 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4701 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4702 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4703 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4704 |
! |
293 | 4705 |
|
2927 | 4706 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4707 |
"replace all oldObjects found between startIndex and endIndex, |
4708 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4709 |
|
362 | 4710 |
Notice: This operation modifies the receiver, NOT a copy; |
4711 |
therefore the change may affect all others referencing the receiver." |
|
4712 |
||
4713 |
|start "{ Class: SmallInteger }" |
|
4714 |
stop "{ Class: SmallInteger }"| |
|
4715 |
||
4716 |
start := startIndex. |
|
4717 |
stop := stopIndex. |
|
4718 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4719 |
(self at:index) = oldObject ifTrue:[ |
4720 |
self at:index put:newObject |
|
4721 |
] |
|
1 | 4722 |
] |
44 | 4723 |
|
95 | 4724 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4725 |
args: oldObject : <object> |
8874 | 4726 |
newObject : <object> |
4727 |
startIndex : <integer> |
|
4728 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4729 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4730 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4731 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4732 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4733 |
" |
2980 | 4734 |
'123123abc123' replaceAll:$1 with:$* |
4735 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4736 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4737 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4738 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4739 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4740 |
! |
4741 |
||
7615 | 4742 |
replaceAllForWhich:aConditionBlock with:newObject |
4743 |
"replace all elements for which aConditionBlock returns true |
|
4744 |
between startIndex and endIndex, by newObject in the receiver. |
|
4745 |
||
4746 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4747 |
therefore the change may affect all others referencing the receiver." |
|
4748 |
||
8874 | 4749 |
self |
4750 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4751 |
from:1 to:(self size) |
|
7615 | 4752 |
|
4753 |
" |
|
4754 |
args: aConditionBlock : <block> |
|
8874 | 4755 |
newObject : <object> |
7615 | 4756 |
|
4757 |
returns: self |
|
4758 |
" |
|
4759 |
! |
|
4760 |
||
4761 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4762 |
"replace all elements for which aConditionBlock returns true |
|
4763 |
between startIndex and endIndex, by newObject in the receiver. |
|
4764 |
||
4765 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4766 |
therefore the change may affect all others referencing the receiver." |
|
4767 |
||
4768 |
|start "{ Class: SmallInteger }" |
|
4769 |
stop "{ Class: SmallInteger }"| |
|
4770 |
||
4771 |
start := startIndex. |
|
4772 |
stop := stopIndex. |
|
4773 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4774 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4775 |
self at:index put:newObject |
|
4776 |
] |
|
7615 | 4777 |
] |
4778 |
||
4779 |
" |
|
4780 |
args: aConditionBlock : <block> |
|
8874 | 4781 |
newObject : <object> |
4782 |
startIndex : <integer> |
|
4783 |
stopIndex : <integer> |
|
7615 | 4784 |
|
4785 |
returns: self |
|
4786 |
" |
|
4787 |
! |
|
4788 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4789 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4790 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4791 |
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
|
4792 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4793 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4794 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4795 |
|
8874 | 4796 |
^ self |
4797 |
replaceAllIdentical:oldObject |
|
4798 |
with:newObject |
|
4799 |
from:1 |
|
4800 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4801 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4802 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4803 |
args: oldObject : <object> |
8874 | 4804 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4805 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4806 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4807 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4808 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4809 |
" |
8874 | 4810 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4811 |
#(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
|
4812 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4813 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4814 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4815 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4816 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4817 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4818 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4819 |
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
|
4820 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4821 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4822 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4823 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4824 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4825 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4826 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4827 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4828 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4829 |
startIndex to:stopIndex do:[:index | |
8874 | 4830 |
(self at:index) == oldObject ifTrue:[ |
4831 |
self at:index put:newObject |
|
4832 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4833 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4834 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4835 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4836 |
args: oldObject : <object> |
8874 | 4837 |
newObject : <object> |
4838 |
startIndex : <integer> |
|
4839 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4840 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4841 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4842 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4843 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4844 |
" |
8874 | 4845 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4846 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4847 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4848 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4849 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4850 |
|
3003 | 4851 |
replaceAny:aCollection by:newObject |
4852 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4853 |
Pleace do no longer use this one; |
3003 | 4854 |
instead use #replaceAny:with: for naming consistence." |
4855 |
||
5872 | 4856 |
<resource:#obsolete> |
4857 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4858 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4859 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4860 |
|
4861 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4862 |
args: aCollection : <colleciton of <object> > |
8874 | 4863 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4864 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4865 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4866 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4867 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4868 |
" |
3003 | 4869 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4870 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4871 |
" |
|
4872 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4873 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4874 |
! |
4875 |
||
3003 | 4876 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4877 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4878 |
Pleace do no longer use this one; |
3003 | 4879 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4880 |
|
5872 | 4881 |
<resource:#obsolete> |
4882 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4883 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4884 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4885 |
|
4886 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4887 |
args: aCollection : <colleciton of <object> > |
8874 | 4888 |
newObject : <object> |
4889 |
startIndex : <integer> |
|
4890 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4891 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4892 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4893 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4894 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4895 |
" |
8874 | 4896 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4897 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4898 |
" |
4899 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4900 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4901 |
! |
4902 |
||
4903 |
replaceAny:aCollection with:newObject |
|
4904 |
"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
|
4905 |
|
2927 | 4906 |
Notice: This operation modifies the receiver, NOT a copy; |
4907 |
therefore the change may affect all others referencing the receiver." |
|
4908 |
||
8874 | 4909 |
^ self |
10439 | 4910 |
replaceAny:aCollection |
4911 |
with:newObject |
|
4912 |
from:1 |
|
4913 |
to:self size |
|
4914 |
||
4915 |
" |
|
4916 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
4917 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
4918 |
" |
|
4919 |
||
4920 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 4921 |
! |
4922 |
||
2927 | 4923 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4924 |
"replace all elements within a range, |
4925 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4926 |
|
362 | 4927 |
Notice: This operation modifies the receiver, NOT a copy; |
4928 |
therefore the change may affect all others referencing the receiver." |
|
4929 |
||
4930 |
|start "{ Class: SmallInteger }" |
|
4931 |
stop "{ Class: SmallInteger }"| |
|
4932 |
||
4933 |
start := startIndex. |
|
4934 |
stop := stopIndex. |
|
4935 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4936 |
(aCollection includes:(self at:index)) ifTrue:[ |
4937 |
self at:index put:newObject |
|
4938 |
] |
|
95 | 4939 |
] |
4940 |
||
4941 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4942 |
args: aCollection : <colleciton of <object> > |
8874 | 4943 |
newObject : <object> |
4944 |
startIndex : <integer> |
|
4945 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4946 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4947 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4948 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4949 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4950 |
" |
8874 | 4951 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4952 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4953 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4954 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4955 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4956 |
! |
4957 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4958 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4959 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4960 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4961 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4962 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4963 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4964 |
|
8874 | 4965 |
^ self |
4966 |
replaceFrom:startIndex |
|
4967 |
to:(startIndex + numberOfElements - 1) |
|
4968 |
with:replacementCollection |
|
4969 |
startingAt:1 |
|
4970 |
||
4971 |
" |
|
4972 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4973 |
#($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
|
4974 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4975 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4976 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4977 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4978 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4979 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4980 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4981 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4982 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4983 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4984 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4985 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4986 |
^ self |
8874 | 4987 |
replaceFrom:startIndex |
4988 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4989 |
with:replacementCollection |
|
4990 |
startingAt:repStartIndex |
|
4991 |
||
4992 |
" |
|
4993 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4994 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4995 |
#($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
|
4996 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4997 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4998 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4999 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5000 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5001 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5002 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 5003 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5004 |
|
44 | 5005 |
Notice: This operation modifies the receiver, NOT a copy; |
5006 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5007 |
|
8874 | 5008 |
^ self |
5009 |
replaceFrom:startIndex |
|
5010 |
to:stopIndex |
|
5011 |
with:replacementCollection |
|
5012 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5013 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5014 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5015 |
args: startIndex : <integer> |
8874 | 5016 |
stopIndex : <integer> |
5017 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5018 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5019 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5020 |
" |
61 | 5021 |
|
95 | 5022 |
" |
5023 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
5024 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
5025 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5026 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5027 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 5028 |
! |
5029 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5030 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5031 |
"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
|
5032 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 5033 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5034 |
|
44 | 5035 |
Notice: This operation modifies the receiver, NOT a copy; |
5036 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5037 |
|
5038 |
|srcIndex "{ Class: SmallInteger }" |
|
5039 |
dstIndex "{ Class: SmallInteger }" |
|
5040 |
end "{ Class: SmallInteger }" | |
|
5041 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5042 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 5043 |
" must do reverse copy " |
5044 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
5045 |
dstIndex := stopIndex. |
|
5046 |
end := startIndex. |
|
5047 |
[dstIndex >= end] whileTrue:[ |
|
5048 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
5049 |
srcIndex := srcIndex - 1. |
|
5050 |
dstIndex := dstIndex - 1 |
|
5051 |
]. |
|
5052 |
^ self |
|
1 | 5053 |
]. |
5054 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5055 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5056 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5057 |
end := stopIndex. |
1 | 5058 |
[dstIndex <= end] whileTrue:[ |
8874 | 5059 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
5060 |
srcIndex := srcIndex + 1. |
|
5061 |
dstIndex := dstIndex + 1 |
|
1 | 5062 |
] |
61 | 5063 |
|
95 | 5064 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5065 |
args: startIndex : <integer> |
8874 | 5066 |
stopIndex : <integer> |
5067 |
replacementCollection : <colleciton of <object> > |
|
5068 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5069 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5070 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5071 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5072 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5073 |
" |
8874 | 5074 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
5075 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
5076 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 5077 |
|
5078 |
|c| |
|
5079 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5080 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 5081 |
|
5082 |
|c| |
|
5083 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5084 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 5085 |
" |
3234 | 5086 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5087 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 5088 |
! |
5089 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5090 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5091 |
"replace elements in the receiver starting at startIndex, |
602 | 5092 |
with elements taken from replacementCollection starting at 1 |
5093 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5094 |
|
602 | 5095 |
Notice: This operation modifies the receiver, NOT a copy; |
5096 |
therefore the change may affect all others referencing the receiver." |
|
5097 |
||
8874 | 5098 |
^ self |
5099 |
replaceFrom:startIndex |
|
5100 |
to:(startIndex + replacementCollection size - 1) |
|
5101 |
with:replacementCollection |
|
5102 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5103 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5104 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5105 |
args: startIndex : <integer> |
8874 | 5106 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5107 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5108 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5109 |
" |
602 | 5110 |
|
5111 |
" |
|
5112 |
'1234567890' replaceFrom:5 with:'abc' |
|
5113 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
5114 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5115 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5116 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 5117 |
! |
5118 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5119 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5120 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5121 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 5122 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5123 |
|
602 | 5124 |
Notice: This operation modifies the receiver, NOT a copy; |
5125 |
therefore the change may affect all others referencing the receiver." |
|
5126 |
||
8874 | 5127 |
^ self |
5128 |
replaceFrom:startIndex |
|
5129 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
5130 |
with:replacementCollection |
|
5131 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5132 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5133 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5134 |
args: startIndex : <integer> |
8874 | 5135 |
replacementCollection : <colleciton of <object> > |
5136 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5137 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5138 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5139 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5140 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5141 |
" |
8874 | 5142 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 5143 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
5144 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5145 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5146 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 5147 |
! |
5148 |
||
61 | 5149 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
5150 |
"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
|
5151 |
|
61 | 5152 |
Notice: This operation modifies the destination collection, NOT a copy; |
5153 |
therefore the change may affect all others referencing this object." |
|
5154 |
||
8874 | 5155 |
destColl |
5156 |
replaceFrom:destStartIndex |
|
5157 |
to:destEndIndex |
|
5158 |
with:self |
|
5159 |
startingAt:sourceStart |
|
61 | 5160 |
|
5161 |
" |
|
5162 |
|s| |
|
5163 |
s := 'abcdefghijklmnop'. |
|
5164 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
5165 |
s'123defghijklmnop' |
|
5166 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5167 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5168 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 5169 |
! ! |
5170 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5171 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5172 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5173 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5174 |
"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
|
5175 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5176 |
Elements on the left are filled with padElement. |
8874 | 5177 |
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
|
5178 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5179 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5180 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5181 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5182 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5183 |
(len < size) ifTrue:[ |
8874 | 5184 |
s := self species new:size withAll:padElement. |
5185 |
s replaceFrom:(size - len + 1) with:self. |
|
5186 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5187 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5188 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5189 |
" |
8874 | 5190 |
'foo' leftPaddedTo:10 with:$. |
5191 |
'fooBar' leftPaddedTo:5 with:$. |
|
5192 |
123 printString leftPaddedTo:10 with:$. |
|
5193 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
5194 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
5195 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
5196 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
5197 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
5198 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
5199 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5200 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5201 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5202 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5203 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5204 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5205 |
plus pad elements up to length. |
8874 | 5206 |
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
|
5207 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5208 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5209 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5210 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5211 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5212 |
len < newSize ifTrue:[ |
8874 | 5213 |
s := self species new:newSize withAll:padElement. |
5214 |
s replaceFrom:1 to:len with:self. |
|
5215 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5216 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5217 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5218 |
" |
8874 | 5219 |
'foo' paddedTo:10 with:$. |
5220 |
123 printString paddedTo:10 with:$* |
|
5221 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
5222 |
(Float pi printString) paddedTo:15 with:$- |
|
5223 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
5224 |
#[1 2 3 4] paddedTo:6 with:0 |
|
5225 |
#[1 2 3 4] paddedTo:10 with:99 |
|
5226 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5227 |
" |
7116 | 5228 |
! |
5229 |
||
5230 |
paddedToMultipleOf:sizeModulu with:padElement |
|
5231 |
|mySize mod numPad| |
|
5232 |
||
5233 |
mySize := self size. |
|
5234 |
mod := (mySize \\ sizeModulu). |
|
5235 |
mod == 0 ifTrue:[^ self]. |
|
5236 |
||
5237 |
numPad := sizeModulu - mod. |
|
5238 |
^ self , (self species new:numPad withAll:padElement). |
|
5239 |
||
5240 |
" |
|
8874 | 5241 |
#[] paddedToMultipleOf:3 with:0 |
5242 |
#[1] paddedToMultipleOf:3 with:0 |
|
5243 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
5244 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 5245 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
5246 |
||
5247 |
#() paddedToMultipleOf:3 with:nil |
|
5248 |
#(1) paddedToMultipleOf:3 with:nil |
|
5249 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
5250 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
5251 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
5252 |
||
5253 |
'' paddedToMultipleOf:3 with:Character space |
|
5254 |
'a' paddedToMultipleOf:3 with:Character space |
|
5255 |
'ab' paddedToMultipleOf:3 with:Character space |
|
5256 |
'abc' paddedToMultipleOf:3 with:Character space |
|
5257 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
5258 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5259 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5260 |
|
7260 | 5261 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 5262 |
|
5358 | 5263 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 5264 |
"Private!! |
5358 | 5265 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
5266 |
||
5267 |
| i1 i2 val1 val2 out | |
|
5268 |
||
5269 |
i1 := first. |
|
5270 |
i2 := middle + 1. |
|
5271 |
val1 := self at: i1. |
|
5272 |
val2 := self at: i2. |
|
5273 |
out := first - 1. "will be pre-incremented" |
|
5274 |
||
5275 |
"select 'lower' half of the elements based on comparator" |
|
5276 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 5277 |
(aBlock value: val1 value: val2) |
5278 |
ifTrue: [ |
|
5279 |
dst at: (out := out + 1) put: val1. |
|
5280 |
val1 := self at: (i1 := i1 + 1)] |
|
5281 |
ifFalse: [ |
|
5282 |
dst at: (out := out + 1) put: val2. |
|
5283 |
i2 := i2 + 1. |
|
5284 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 5285 |
|
5286 |
"copy the remaining elements" |
|
5287 |
i1 <= middle |
|
8874 | 5288 |
ifTrue: [ |
5289 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
5290 |
ifFalse: [ |
|
5291 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 5292 |
|
5293 |
! |
|
5294 |
||
5295 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
5296 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
5297 |
||
5298 |
| middle | |
|
5299 |
||
5300 |
first = last ifTrue: [^ self]. |
|
5301 |
middle := (first + last) // 2. |
|
5302 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
5303 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
5304 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
5305 |
||
5306 |
||
5307 |
! |
|
5308 |
||
2069 | 5309 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5310 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5311 |
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
|
5312 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5313 |
|
8874 | 5314 |
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
|
5315 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5316 |
|
5317 |
|begin "{ Class: SmallInteger }" |
|
5318 |
end "{ Class: SmallInteger }" |
|
5319 |
b "{ Class: SmallInteger }" |
|
5320 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5321 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5322 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5323 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5324 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5325 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5326 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5327 |
[stack notEmpty] whileTrue:[ |
8874 | 5328 |
end := stack removeLast. |
5329 |
begin := stack removeLast. |
|
5330 |
||
5331 |
b := begin. |
|
5332 |
e := end. |
|
5333 |
middleElement := self at:((b + e) // 2). |
|
5334 |
||
5335 |
[b < e] whileTrue:[ |
|
5336 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5337 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5338 |
||
5339 |
(b <= e) ifTrue:[ |
|
5340 |
(b == e) ifFalse:[ |
|
5341 |
temp1 := self at:b. temp2 := self at:e. |
|
5342 |
self at:b put:temp2. self at:e put:temp1 |
|
5343 |
]. |
|
5344 |
b := b + 1. |
|
5345 |
e := e - 1 |
|
5346 |
] |
|
5347 |
]. |
|
5348 |
(begin < e) ifTrue:[ |
|
5349 |
stack add:begin. |
|
5350 |
stack add:e. |
|
5351 |
]. |
|
5352 |
(b < end) ifTrue:[ |
|
5353 |
stack add:b. |
|
5354 |
stack add:end. |
|
5355 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5356 |
] |
2069 | 5357 |
! |
5358 |
||
5359 |
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
|
5360 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5361 |
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
|
5362 |
with degenerated collections." |
2069 | 5363 |
|
5364 |
|begin "{ Class: SmallInteger }" |
|
5365 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5366 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5367 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5368 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5369 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5370 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5371 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5372 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5373 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5374 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5375 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5376 |
[stack notEmpty] whileTrue:[ |
8874 | 5377 |
end := stack removeLast. |
5378 |
begin := stack removeLast. |
|
5379 |
||
5380 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5381 |
elB := self at:begin. |
|
5382 |
elE := self at:end. |
|
5383 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5384 |
temp := elB. elB := elE. elE := temp. |
|
5385 |
self at:begin put:elB. |
|
5386 |
self at:end put:elE. |
|
5387 |
]. |
|
5388 |
||
5389 |
nToSort > 2 ifTrue:[ |
|
5390 |
m := (begin + end) // 2. |
|
5391 |
elM := self at:m. |
|
5392 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5393 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5394 |
temp := self at:end. |
|
5395 |
self at:end put:(self at:m). |
|
5396 |
self at:m put:temp. |
|
5397 |
elM := elE |
|
5398 |
] |
|
5399 |
] ifFalse:[ |
|
5400 |
temp := self at:begin. |
|
5401 |
self at:begin put:(self at:m). |
|
5402 |
self at:m put:temp. |
|
5403 |
elM := elB |
|
5404 |
]. |
|
5405 |
nToSort > 3 ifTrue:[ |
|
5406 |
bRun := begin. |
|
5407 |
eRun := end. |
|
5408 |
[ |
|
5409 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5410 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5411 |
bRun <= eRun |
|
5412 |
] whileTrue:[ |
|
5413 |
temp := self at:bRun. |
|
5414 |
self at:bRun put:(self at:eRun). |
|
5415 |
self at:eRun put:temp. |
|
5416 |
]. |
|
5417 |
stack add:begin. |
|
5418 |
stack add:eRun. |
|
5419 |
||
5420 |
stack add:bRun. |
|
5421 |
stack add:end. |
|
5422 |
] |
|
5423 |
] |
|
5424 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5425 |
] |
2069 | 5426 |
! |
5427 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5428 |
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
|
5429 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5430 |
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
|
5431 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5432 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5433 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5434 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5435 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5436 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5437 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5438 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5439 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5440 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5441 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5442 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5443 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5444 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5445 |
[stack notEmpty] whileTrue:[ |
8874 | 5446 |
end := stack removeLast. |
5447 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5448 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5449 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 5450 |
elB := self at:begin. |
5451 |
elE := self at:end. |
|
5452 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
5453 |
temp := elB. elB := elE. elE := temp. |
|
5454 |
self at:begin put:elB. |
|
5455 |
self at:end put:elE. |
|
5456 |
]. |
|
5457 |
||
5458 |
nToSort > 2 ifTrue:[ |
|
5459 |
m := (begin + end) // 2. |
|
5460 |
elM := self at:m. |
|
5461 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
5462 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
5463 |
temp := self at:end. |
|
5464 |
self at:end put:(self at:m). |
|
5465 |
self at:m put:temp. |
|
5466 |
elM := elE |
|
5467 |
] |
|
5468 |
] ifFalse:[ |
|
5469 |
temp := self at:begin. |
|
5470 |
self at:begin put:(self at:m). |
|
5471 |
self at:m put:temp. |
|
5472 |
elM := elB |
|
5473 |
]. |
|
5474 |
nToSort > 3 ifTrue:[ |
|
5475 |
bRun := begin. |
|
5476 |
eRun := end. |
|
5477 |
[ |
|
5478 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
5479 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
5480 |
bRun <= eRun |
|
5481 |
] whileTrue:[ |
|
5482 |
temp := self at:bRun. |
|
5483 |
self at:bRun put:(self at:eRun). |
|
5484 |
self at:eRun put:temp. |
|
5485 |
]. |
|
5486 |
stack add:begin. |
|
5487 |
stack add:eRun. |
|
5488 |
||
5489 |
stack add:bRun. |
|
5490 |
stack add:end. |
|
5491 |
] |
|
5492 |
] |
|
5493 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5494 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5495 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5496 |
|
2069 | 5497 |
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
|
5498 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5499 |
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
|
5500 |
with degenerated collections." |
2069 | 5501 |
|
5502 |
|begin "{ Class: SmallInteger }" |
|
5503 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5504 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5505 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5506 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5507 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5508 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5509 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5510 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5511 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5512 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5513 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5514 |
[stack notEmpty] whileTrue:[ |
8874 | 5515 |
end := stack removeLast. |
5516 |
begin := stack removeLast. |
|
5517 |
||
5518 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5519 |
||
5520 |
elB := self at:begin. |
|
5521 |
elE := self at:end. |
|
5522 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5523 |
temp := elB. elB := elE. elE := temp. |
|
5524 |
self at:begin put:elB. |
|
5525 |
self at:end put:elE. |
|
5526 |
||
5527 |
temp := aCollection at:end. |
|
5528 |
aCollection at:end put:(aCollection at:begin). |
|
5529 |
aCollection at:begin put:temp. |
|
5530 |
]. |
|
5531 |
||
5532 |
nToSort > 2 ifTrue:[ |
|
5533 |
m := (begin + end) // 2. |
|
5534 |
elM := self at:m. |
|
5535 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5536 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5537 |
temp := self at:end. |
|
5538 |
self at:end put:(self at:m). |
|
5539 |
self at:m put:temp. |
|
5540 |
||
5541 |
temp := aCollection at:end. |
|
5542 |
aCollection at:end put:(aCollection at:m). |
|
5543 |
aCollection at:m put:temp. |
|
5544 |
||
5545 |
elM := elE |
|
5546 |
] |
|
5547 |
] ifFalse:[ |
|
5548 |
temp := self at:begin. |
|
5549 |
self at:begin put:(self at:m). |
|
5550 |
self at:m put:temp. |
|
5551 |
||
5552 |
temp := aCollection at:begin. |
|
5553 |
aCollection at:begin put:(aCollection at:m). |
|
5554 |
aCollection at:m put:temp. |
|
5555 |
elM := elB |
|
5556 |
]. |
|
5557 |
nToSort > 3 ifTrue:[ |
|
5558 |
bRun := begin. |
|
5559 |
eRun := end. |
|
5560 |
[ |
|
5561 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5562 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5563 |
bRun <= eRun |
|
5564 |
] whileTrue:[ |
|
5565 |
temp := self at:bRun. |
|
5566 |
self at:bRun put:(self at:eRun). |
|
5567 |
self at:eRun put:temp. |
|
5568 |
||
5569 |
temp := aCollection at:bRun. |
|
5570 |
aCollection at:bRun put:(aCollection at:eRun). |
|
5571 |
aCollection at:eRun put:temp. |
|
5572 |
]. |
|
5573 |
stack add:begin. |
|
5574 |
stack add:eRun. |
|
5575 |
||
5576 |
stack add:bRun. |
|
5577 |
stack add:end. |
|
5578 |
] |
|
5579 |
] |
|
5580 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5581 |
] |
2069 | 5582 |
! |
5583 |
||
5584 |
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
|
5585 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5586 |
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
|
5587 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5588 |
|
8874 | 5589 |
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
|
5590 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5591 |
|
5592 |
|begin "{ Class: SmallInteger }" |
|
5593 |
end "{ Class: SmallInteger }" |
|
5594 |
b "{ Class: SmallInteger }" |
|
5595 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5596 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5597 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5598 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5599 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5600 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5601 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5602 |
[stack notEmpty] whileTrue:[ |
8874 | 5603 |
end := stack removeLast. |
5604 |
begin := stack removeLast. |
|
5605 |
||
5606 |
b := begin. |
|
5607 |
e := end. |
|
5608 |
middleElement := self at:((b + e) // 2). |
|
5609 |
||
5610 |
[b < e] whileTrue:[ |
|
5611 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5612 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5613 |
||
5614 |
(b <= e) ifTrue:[ |
|
5615 |
(b == e) ifFalse:[ |
|
5616 |
temp1 := self at:b. temp2 := self at:e. |
|
5617 |
self at:b put:temp2. self at:e put:temp1. |
|
5618 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5619 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5620 |
]. |
|
5621 |
b := b + 1. |
|
5622 |
e := e - 1 |
|
5623 |
] |
|
5624 |
]. |
|
5625 |
(begin < e) ifTrue:[ |
|
5626 |
stack add:begin. |
|
5627 |
stack add:e. |
|
5628 |
]. |
|
5629 |
(b < end) ifTrue:[ |
|
5630 |
stack add:b. |
|
5631 |
stack add:end. |
|
5632 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5633 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5634 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5635 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5636 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5637 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5638 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5639 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5640 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5641 |
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
|
5642 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5643 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5644 |
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
|
5645 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5646 |
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
|
5647 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5648 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5649 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5650 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5651 |
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
|
5652 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5653 |
"/ 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
|
5654 |
^ 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
|
5655 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5656 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5657 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5658 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5659 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5660 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5661 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5662 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5663 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5664 |
"/ 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
|
5665 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5666 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5667 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5668 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5669 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5670 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5671 |
"/ "/ 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
|
5672 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5673 |
"/ 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
|
5674 |
"/ 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
|
5675 |
"/ 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
|
5676 |
"/ aCollection notNil ifTrue:[ |
8874 | 5677 |
"/ 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
|
5678 |
"/ 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
|
5679 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5680 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5681 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5682 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5683 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5684 |
"/ 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
|
5685 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5686 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5687 |
"/ [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
|
5688 |
"/ [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
|
5689 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5690 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5691 |
"/ (b == e) ifFalse:[ |
8874 | 5692 |
"/ 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
|
5693 |
"/ 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
|
5694 |
"/ aCollection notNil ifTrue:[ |
8874 | 5695 |
"/ 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
|
5696 |
"/ 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
|
5697 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5698 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5699 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5700 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5701 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5702 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5703 |
"/ (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
|
5704 |
"/ (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
|
5705 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5706 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5707 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5708 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5709 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5710 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5711 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5712 |
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
|
5713 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5714 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5715 |
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
|
5716 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5717 |
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
|
5718 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5719 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5720 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5721 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5722 |
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
|
5723 |
"/ 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
|
5724 |
^ 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
|
5725 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5726 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5727 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5728 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5729 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5730 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5731 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5732 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5733 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5734 |
"/ 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
|
5735 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5736 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5737 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5738 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5739 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5740 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5741 |
"/ "/ 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
|
5742 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5743 |
"/ 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
|
5744 |
"/ 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
|
5745 |
"/ 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
|
5746 |
"/ aCollection notNil ifTrue:[ |
8874 | 5747 |
"/ 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
|
5748 |
"/ 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
|
5749 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5750 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5751 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5752 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5753 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5754 |
"/ 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
|
5755 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5756 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5757 |
"/ [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
|
5758 |
"/ [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
|
5759 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5760 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5761 |
"/ (b == e) ifFalse:[ |
8874 | 5762 |
"/ 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
|
5763 |
"/ 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
|
5764 |
"/ aCollection notNil ifTrue:[ |
8874 | 5765 |
"/ 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
|
5766 |
"/ 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
|
5767 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5768 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5769 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5770 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5771 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5772 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5773 |
"/ (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
|
5774 |
"/ (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
|
5775 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5776 |
"/ "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
|
5777 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5778 |
! ! |
5779 |
||
602 | 5780 |
!SequenceableCollection methodsFor:'queries'! |
5781 |
||
5782 |
firstIndex |
|
5783 |
"return the first elements index" |
|
5784 |
||
5785 |
^ 1 |
|
5786 |
! |
|
5787 |
||
5788 |
isSequenceable |
|
5789 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5790 |
||
5791 |
^ true |
|
1 | 5792 |
! |
5793 |
||
602 | 5794 |
isSequenceableCollection |
5795 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5796 |
This method is a historic leftover and will be removed." |
|
5797 |
||
5872 | 5798 |
<resource:#obsolete> |
5799 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5800 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5801 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5802 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5803 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5804 |
! |
5805 |
||
5358 | 5806 |
isSorted |
5807 |
"return true. if my elements are sorted (already)" |
|
5808 |
||
5809 |
|lastEl el| |
|
5810 |
||
5811 |
lastEl := self first. |
|
5812 |
2 to:self size do:[:i | |
|
8874 | 5813 |
el := self at:i. |
5814 |
el >= lastEl ifFalse: [^ false]. |
|
5815 |
lastEl := el |
|
5358 | 5816 |
]. |
5817 |
^ true |
|
5818 |
||
5819 |
" |
|
8874 | 5820 |
#(1 2 3 5 10 100) isSorted |
5821 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5822 |
" |
5823 |
! |
|
5824 |
||
5825 |
isSortedBy:aBlock |
|
5826 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5827 |
||
5828 |
|lastEl el| |
|
5829 |
||
5830 |
lastEl := self first. |
|
5831 |
2 to:self size do:[:i | |
|
8874 | 5832 |
el := self at:i. |
5833 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5834 |
lastEl := el |
|
5358 | 5835 |
]. |
5836 |
^ true |
|
5837 |
||
5838 |
" |
|
8874 | 5839 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5840 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5841 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5842 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5843 |
" |
5844 |
||
5845 |
! |
|
5846 |
||
602 | 5847 |
keys |
5848 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5849 |
||
5850 |
|sz| |
|
5851 |
||
1 | 5852 |
sz := self size. |
602 | 5853 |
sz == 0 ifTrue:[ |
8874 | 5854 |
^ #() |
61 | 5855 |
]. |
602 | 5856 |
^ 1 to:sz |
1 | 5857 |
! |
5858 |
||
602 | 5859 |
lastIndex |
5860 |
"return the last elements index" |
|
5861 |
||
5862 |
^ self size |
|
61 | 5863 |
! |
5864 |
||
602 | 5865 |
size |
5866 |
"return the number of elements in the collection. |
|
5867 |
concrete implementations must define this" |
|
5868 |
||
5869 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5870 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5871 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5872 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5873 |
"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
|
5874 |
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
|
5875 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5876 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5877 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5878 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5879 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5880 |
! ! |
5881 |
||
5882 |
!SequenceableCollection methodsFor:'searching'! |
|
5883 |
||
3887 | 5884 |
detect:aBlock startingAt:startIndex |
5885 |
"find the first element, for which evaluation of the argument, aBlock |
|
5886 |
return true, starting the search with startIndex. |
|
5887 |
If none does so, report an error" |
|
5888 |
||
5889 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5890 |
||
5891 |
" |
|
8874 | 5892 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5893 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5894 |
" |
5895 |
||
5896 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5897 |
! |
|
5898 |
||
5899 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5900 |
"find the first element, for which evaluation of the argument, aBlock |
|
5901 |
return true, starting the search with startIndex. |
|
5717 | 5902 |
If none does so, return the evaluation of exceptionBlock" |
5903 |
||
5904 |
|stop| |
|
3887 | 5905 |
|
5906 |
stop := self size. |
|
5717 | 5907 |
self from:startIndex to:stop do:[:el | |
8874 | 5908 |
(aBlock value:el) ifTrue:[ |
5909 |
^ el |
|
5910 |
]. |
|
3887 | 5911 |
]. |
5912 |
^ exceptionBlock value |
|
5913 |
||
5914 |
" |
|
8874 | 5915 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5916 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5917 |
" |
5918 |
||
5919 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5920 |
! |
|
5921 |
||
11705 | 5922 |
findFirst:aBlock ifNone:exceptionalValue |
10453 | 5923 |
"find the index of the first element, for which evaluation of the argument, aBlock |
11705 | 5924 |
returns true; return its index or the value from exceptionalValue if none detected. |
3887 | 5925 |
This is much like #detect, however, here an INDEX is returned, |
5926 |
while #detect returns the element." |
|
602 | 5927 |
|
5928 |
|stop "{ Class: SmallInteger }" | |
|
5929 |
||
32 | 5930 |
stop := self size. |
602 | 5931 |
1 to:stop do:[:index | |
10453 | 5932 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5933 |
]. |
11705 | 5934 |
^ exceptionalValue value |
5935 |
||
5936 |
" |
|
5937 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
5938 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
5939 |
" |
|
1 | 5940 |
! |
5941 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5942 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5943 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5944 |
returns true; return its index or 0 if none detected. |
5945 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5946 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5947 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5948 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5949 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5950 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5951 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5952 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5953 |
start to:stop do:[:index | |
8874 | 5954 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5955 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5956 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5957 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5958 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5959 |
#(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
|
5960 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5961 |
" |
3887 | 5962 |
|
5963 |
"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
|
5964 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5965 |
|
602 | 5966 |
findLast:aBlock |
5967 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5968 |
returns true. |
5969 |
Return its index or 0 if none detected." |
|
602 | 5970 |
|
5971 |
|start "{ Class: SmallInteger }"| |
|
5972 |
||
5973 |
start := self size. |
|
5974 |
start to:1 by:-1 do:[:index | |
|
8874 | 5975 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5976 |
]. |
5977 |
^ 0 |
|
5978 |
||
5979 |
" |
|
602 | 5980 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5981 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5982 |
" |
5983 |
! |
|
5984 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5985 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5986 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5987 |
returns true. Start the search at startIndex. |
5988 |
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
|
5989 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5990 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5991 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5992 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5993 |
start to:1 by:-1 do:[:index | |
8874 | 5994 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5995 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5996 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5997 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5998 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5999 |
#(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
|
6000 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6001 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6002 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6003 |
|
8193 | 6004 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
6005 |
"find the last element, for which evaluation of the argument, aBlock |
|
6006 |
returns true. Start the search at startIndex. |
|
6007 |
End the search at endIndex or when an element is found. |
|
6008 |
Return its index or 0 if none detected." |
|
6009 |
||
6010 |
|start "{ Class: SmallInteger }"| |
|
6011 |
||
6012 |
start := startIndex. |
|
6013 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 6014 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 6015 |
]. |
6016 |
^ 0 |
|
6017 |
||
6018 |
" |
|
6019 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
6020 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
6021 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
6022 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
6023 |
" |
|
6024 |
! |
|
6025 |
||
159 | 6026 |
indexOfAny:aCollection |
6027 |
"search the collection for an element in aCollection. |
|
6028 |
if found, return the index otherwise return 0. |
|
8874 | 6029 |
The comparison is done using = |
159 | 6030 |
(i.e. equality test - not identity test). |
6031 |
||
6032 |
Notice, that for big collections, the runtime of this search |
|
6033 |
grows proportional to size(receiver) * size(aCollection). |
|
6034 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6035 |
||
6036 |
^ self indexOfAny:aCollection startingAt:1 |
|
6037 |
||
6038 |
" |
|
6039 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
6040 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
6041 |
" |
|
6042 |
! |
|
6043 |
||
6044 |
indexOfAny:aCollection startingAt:start |
|
6045 |
"search the collection for an element in aCollection, |
|
6046 |
starting the search at index start; |
|
6047 |
if found, return the index otherwise return 0. |
|
8874 | 6048 |
The comparison is done using = |
159 | 6049 |
(i.e. equality test - not identity test). |
6050 |
||
6051 |
Notice, that for big collections, the runtime of this search |
|
6052 |
grows proportional to size(receiver) * size(aCollection). |
|
6053 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6054 |
||
6055 |
||
6056 |
|startIndex "{ Class: SmallInteger }" |
|
6057 |
stop "{ Class: SmallInteger }" | |
|
6058 |
||
6059 |
startIndex := start. |
|
6060 |
stop := self size. |
|
6061 |
startIndex to:stop do:[:index | |
|
8874 | 6062 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 6063 |
]. |
6064 |
^ 0 |
|
6065 |
" |
|
8874 | 6066 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
6067 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 6068 |
" |
1 | 6069 |
! |
6070 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6071 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6072 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6073 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6074 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 6075 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6076 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6077 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6078 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6079 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6080 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6081 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6082 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6083 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6084 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6085 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6086 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6087 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6088 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6089 |
" |
8874 | 6090 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
6091 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
6092 |
#(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
|
6093 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6094 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6095 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6096 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6097 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6098 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6099 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6100 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6101 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6102 |
#(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
|
6103 |
#(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
|
6104 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6105 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6106 |
|
8874 | 6107 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6108 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6109 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6110 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6111 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6112 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6113 |
#(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
|
6114 |
#(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
|
6115 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6116 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6117 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6118 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6119 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6120 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6121 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6122 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6123 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6124 |
|
368 | 6125 |
indexOfSubCollection:aCollection |
9234 | 6126 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 6127 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6128 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 6129 |
|
6130 |
" |
|
8874 | 6131 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
6132 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
6133 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
6134 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
6135 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
6136 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
6137 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
6138 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 6139 |
" |
6140 |
! |
|
6141 |
||
602 | 6142 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
6143 |
"find a subcollection. If found, return the index; |
|
6144 |
if not found, return the result of evaluating exceptionBlock." |
|
6145 |
||
6146 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
6147 |
! |
|
6148 |
||
368 | 6149 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6150 |
"find a subcollection starting at index. |
368 | 6151 |
If found, return the index; if not found, return 0." |
6152 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6153 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 6154 |
! |
6155 |
||
6156 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
6157 |
"find a subcollection, starting at index. If found, return the index; |
|
6158 |
if not found, return the result of evaluating exceptionBlock. |
|
6159 |
This is a q&d hack - not very efficient" |
|
6160 |
||
8874 | 6161 |
|same first |
368 | 6162 |
mySize "{Class: SmallInteger }" |
6163 |
checkIndex "{Class: SmallInteger }" |
|
6164 |
cmpIndex "{Class: SmallInteger }" |
|
6165 |
sz "{Class: SmallInteger }"| |
|
6166 |
||
6167 |
mySize := self size. |
|
6168 |
sz := aCollection size. |
|
6169 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
6170 |
first := aCollection at:1. |
|
6171 |
||
8874 | 6172 |
checkIndex := startIndex - 1. |
368 | 6173 |
[true] whileTrue:[ |
8874 | 6174 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
6175 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6176 |
||
6177 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
6178 |
^ 0 |
|
6179 |
]. |
|
6180 |
same := true. |
|
6181 |
cmpIndex := 1. |
|
6182 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6183 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
6184 |
same := false |
|
6185 |
] ifTrue:[ |
|
6186 |
cmpIndex := cmpIndex + 1 |
|
6187 |
] |
|
6188 |
]. |
|
6189 |
same ifTrue:[^ checkIndex]. |
|
6190 |
] |
|
6191 |
||
6192 |
" |
|
6193 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
6194 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
6195 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
6196 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
6197 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
6198 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 6199 |
" |
602 | 6200 |
! |
6201 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6202 |
lastIndexOfSubCollection:aCollection |
8874 | 6203 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6204 |
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
|
6205 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6206 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6207 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6208 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6209 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6210 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6211 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6212 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6213 |
"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
|
6214 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6215 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6216 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6217 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6218 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6219 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6220 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6221 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6222 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6223 |
"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
|
6224 |
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
|
6225 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6226 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6227 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6228 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6229 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6230 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6231 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6232 |
"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
|
6233 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6234 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6235 |
|
8874 | 6236 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6237 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6238 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6239 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6240 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6241 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6242 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6243 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6244 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6245 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6246 |
|
8874 | 6247 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6248 |
checkIndex > mySize ifTrue:[ |
8874 | 6249 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6250 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6251 |
[true] whileTrue:[ |
8874 | 6252 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
6253 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6254 |
||
6255 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
6256 |
^ 0 |
|
6257 |
]. |
|
6258 |
same := true. |
|
6259 |
cmpIndex := 1. |
|
6260 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6261 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
6262 |
same := false |
|
6263 |
] ifTrue:[ |
|
6264 |
cmpIndex := cmpIndex + 1 |
|
6265 |
] |
|
6266 |
]. |
|
6267 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
6268 |
] |
|
6269 |
||
6270 |
" |
|
6271 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
6272 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
6273 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
6274 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
6275 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
6276 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
6277 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
6278 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
6279 |
#(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
|
6280 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6281 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6282 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
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 |
|
7264 | 6285 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
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 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6288 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6289 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6290 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6291 |
|
10194 | 6292 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6293 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6294 |
" |
8874 | 6295 |
args: anElement : <object> |
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 |
returns: true - if found |
10194 | 6298 |
false - if not found |
8874 | 6299 |
" |
6300 |
||
6301 |
" |
|
6302 |
#(10 20 30 40 50 60 70) includes:99 |
|
6303 |
#(10 20 30 40 50 60 70) includes:40 |
|
6304 |
#(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
|
6305 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6306 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6307 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6308 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6309 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6310 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6311 |
"search the collection for anElement; |
602 | 6312 |
if found, return the index otherwise return 0. |
8874 | 6313 |
The comparison is done using = |
602 | 6314 |
(i.e. equality test - not identity test)." |
6315 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6316 |
^ self indexOf:anElement startingAt:1 |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6319 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6320 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6321 |
returns: elementIndex - if found |
8874 | 6322 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6323 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6324 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6325 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6326 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6327 |
#(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
|
6328 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6329 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
"Modified: / 20.5.1998 / 14:59:55 / 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 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
"search the collection for anElement; |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6335 |
if found, return the index; otherwise return the value of the exceptionBlock. |
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. equality test - not identity 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 |
|index| |
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 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6342 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6343 |
^ index |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6346 |
args: anElement : <object> |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6347 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6348 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6349 |
returns: elementIndex - if found |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6350 |
exceptionBlock value - if not found |
8874 | 6351 |
" |
6352 |
||
6353 |
" |
|
6354 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
6355 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
6356 |
#(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
|
6357 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6358 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6359 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6360 |
! |
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 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 6363 |
"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
|
6364 |
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
|
6365 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6366 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6367 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6368 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6369 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6370 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6371 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 6372 |
self at:idx put:replacement. |
6373 |
^ idx |
|
3468
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6376 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6377 |
" |
8874 | 6378 |
args: elementToFind : <object> |
6379 |
replacement : <integer> |
|
6380 |
start : <integer> |
|
6381 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6382 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6383 |
returns: elementIndex - if found (and replaced) |
8874 | 6384 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6385 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6386 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6387 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6388 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6389 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6390 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6391 |
(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
|
6392 |
a printNL. |
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 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6396 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6397 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6398 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6399 |
"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
|
6400 |
if found, return the index otherwise return 0. |
8874 | 6401 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6402 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6403 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6404 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6405 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6406 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6407 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6408 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6409 |
startIndex to:stop do:[:index | |
8874 | 6410 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6411 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6412 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6413 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6414 |
" |
8874 | 6415 |
args: anElement : <object> |
6416 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6417 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6418 |
returns: elementIndex - if found |
8874 | 6419 |
0 - if not found |
6420 |
" |
|
6421 |
||
6422 |
" |
|
6423 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
6424 |
#(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
|
6425 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6426 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6427 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6428 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6429 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6430 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6431 |
"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
|
6432 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6433 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6434 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6435 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6436 |
|
602 | 6437 |
|startIndex "{ Class: SmallInteger }" |
6438 |
stopIndex "{ Class: SmallInteger }" | |
|
6439 |
||
6440 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6441 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6442 |
startIndex to:stopIndex do:[:index | |
8874 | 6443 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6444 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6445 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6446 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6447 |
" |
8874 | 6448 |
args: anElement : <object> |
6449 |
start : <integer> |
|
6450 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6451 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6452 |
returns: elementIndex - if found |
8874 | 6453 |
exceptionBlock value - if not found |
6454 |
" |
|
6455 |
||
6456 |
" |
|
6457 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
6458 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
6459 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
6460 |
#(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
|
6461 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6462 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6463 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6464 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6465 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6466 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6467 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6468 |
"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
|
6469 |
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
|
6470 |
exceptionBlock. |
8874 | 6471 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6472 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6473 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6474 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6475 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6476 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6477 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6478 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6479 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6480 |
" |
8874 | 6481 |
args: anElement : <object> |
6482 |
start : <integer> |
|
6483 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6484 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6485 |
returns: elementIndex - if found |
8874 | 6486 |
exceptionBlock value - if not found |
6487 |
" |
|
6488 |
||
6489 |
" |
|
6490 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
6491 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
6492 |
#(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
|
6493 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6494 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6495 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6496 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6497 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6498 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6499 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6500 |
if found, return the index otherwise return 0. |
8874 | 6501 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6502 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6503 |
|
8874 | 6504 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6505 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6506 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6507 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6508 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6509 |
returns: elementIndex - if found |
8874 | 6510 |
0 - if not found |
6511 |
" |
|
6512 |
||
6513 |
" |
|
6514 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
6515 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
6516 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
6517 |
#(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
|
6518 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6519 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6520 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6521 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6522 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6523 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6524 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6525 |
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
|
6526 |
exceptionBlock. |
8874 | 6527 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6528 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6529 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6530 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6531 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6532 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6533 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6534 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6535 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6536 |
" |
8874 | 6537 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
6538 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
6539 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
6540 |
#(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
|
6541 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6542 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6543 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6544 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6545 |
"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
|
6546 |
if found, return the index otherwise return 0. |
8874 | 6547 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6548 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6549 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6550 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6551 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6552 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6553 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6554 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 6555 |
]. |
6556 |
^ 0 |
|
6557 |
||
6558 |
" |
|
8874 | 6559 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
6560 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
6561 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
6562 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
6563 |
#(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
|
6564 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6565 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6566 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6567 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6568 |
"search the collection backwards for anElement starting the search at |
6569 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6570 |
otherwise return the value of the exceptionBlock. |
8874 | 6571 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6572 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6573 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6574 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6575 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6576 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6577 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6578 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6579 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6580 |
" |
8874 | 6581 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
6582 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6583 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6584 |
#(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
|
6585 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6586 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6587 |
|
4788 | 6588 |
lastIndexOfAny:aCollection |
6589 |
"search the collection backwards for any in aCollection; |
|
6590 |
if found, return the index, otherwise return 0. |
|
8874 | 6591 |
The comparison is done using = |
4788 | 6592 |
(i.e. equality test - not identity test)." |
6593 |
||
8874 | 6594 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 6595 |
|
6596 |
" |
|
6597 |
args: aCollection : <collection> |
|
6598 |
||
6599 |
returns: elementIndex - if found |
|
8874 | 6600 |
0 - if not found |
6601 |
" |
|
6602 |
||
6603 |
" |
|
6604 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 6605 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 6606 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
6607 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 6608 |
" |
6609 |
||
6610 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
6611 |
! |
|
6612 |
||
6613 |
lastIndexOfAny:aCollection startingAt:start |
|
6614 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
6615 |
if found, return the index, otherwise return 0. |
|
8874 | 6616 |
The comparison is done using = |
4788 | 6617 |
(i.e. equality test - not identity test)." |
6618 |
||
6619 |
|startIndex "{ Class: SmallInteger }"| |
|
6620 |
||
6621 |
startIndex := start. |
|
6622 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 6623 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 6624 |
]. |
6625 |
^ 0 |
|
6626 |
||
6627 |
" |
|
8874 | 6628 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
6629 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 6630 |
" |
6631 |
! |
|
6632 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6633 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6634 |
"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
|
6635 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6636 |
if found, return the index otherwise return nil. |
8874 | 6637 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6638 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6639 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6640 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6641 |
although this is somewhat strange (the indexOf-search |
6642 |
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
|
6643 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6644 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6645 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6646 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6647 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6648 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6649 |
startIndex to:stop do:[:index | |
8874 | 6650 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6651 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6652 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6653 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6654 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6655 |
args: anElement : <object> |
8874 | 6656 |
start : <integer> |
6657 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6658 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6659 |
returns: elementIndex - if found |
8874 | 6660 |
nil - if not found |
6661 |
" |
|
6662 |
||
6663 |
" |
|
6664 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
6665 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
6666 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 6667 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6668 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6669 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 6670 |
! |
6671 |
||
6672 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6673 |
"search the collection for anElement, starting the search at index start |
|
6674 |
and stopping at stop; |
|
6675 |
if found, return the index otherwise return the value of the |
|
6676 |
exceptionBlock. |
|
8874 | 6677 |
The comparison is done using = |
602 | 6678 |
(i.e. equality test - not identity test)." |
6679 |
||
6680 |
|index| |
|
6681 |
||
6682 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6683 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 6684 |
^ index |
6685 |
||
6686 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6687 |
args: anElement : <object> |
8874 | 6688 |
start : <integer> |
6689 |
stop : <integer> |
|
6690 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6691 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6692 |
returns: elementIndex - if found |
8874 | 6693 |
valueImplementor value - if not found |
6694 |
" |
|
6695 |
||
6696 |
" |
|
6697 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6698 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6699 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6700 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6701 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6702 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 6703 |
! |
6704 |
||
6705 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6706 |
"search the collection for anElement, starting the search at index start; |
|
6707 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6708 |
If found (within the range), return the index, otherwise return nil. |
8874 | 6709 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6710 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6711 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6712 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6713 |
although this is somewhat strange (the indexOf-search |
6714 |
methods return 0), this is done for VW compatibility." |
|
2275 | 6715 |
|
6716 |
|startIndex "{ Class: SmallInteger }" |
|
6717 |
stopIndex "{ Class: SmallInteger }" | |
|
6718 |
||
6719 |
startIndex := startSearchIndex. |
|
6720 |
stopIndex := endSearchIndex max:1. |
|
6721 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 6722 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6723 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6724 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6725 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6726 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6727 |
args: anElement : <object> |
8874 | 6728 |
start : <integer> |
6729 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6730 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6731 |
returns: elementIndex - if found |
8874 | 6732 |
nil - if not found |
6733 |
" |
|
6734 |
||
6735 |
" |
|
6736 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6737 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6738 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6739 |
" |
6740 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6741 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6742 |
! |
6743 |
||
6744 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6745 |
"search the collection for anElement, starting the search at index start |
|
6746 |
and stopping at stop, doing a reverse search; |
|
6747 |
if found, return the index otherwise return the value of the |
|
6748 |
exceptionBlock. |
|
8874 | 6749 |
The comparison is done using = |
2275 | 6750 |
(i.e. equality test - not identity test)." |
6751 |
||
6752 |
|index| |
|
6753 |
||
6754 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6755 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6756 |
^ index |
|
6757 |
||
6758 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6759 |
args: anElement : <object> |
8874 | 6760 |
start : <integer> |
6761 |
stop : <integer> |
|
6762 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6763 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6764 |
returns: elementIndex - if found |
8874 | 6765 |
valueImplementor value - if not found |
6766 |
" |
|
6767 |
||
6768 |
" |
|
6769 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6770 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6771 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6772 |
" |
6773 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6774 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6775 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6776 |
|
7264 | 6777 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6778 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6779 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6780 |
"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
|
6781 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6782 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6783 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6784 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6785 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6786 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6787 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6788 |
returns: elementIndex - if found |
8874 | 6789 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6790 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6791 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6792 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6793 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6794 |
#(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
|
6795 |
#(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
|
6796 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6797 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6798 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6799 |
#(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
|
6800 |
#(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
|
6801 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6802 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6803 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6804 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6805 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6806 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6807 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6808 |
"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
|
6809 |
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
|
6810 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6811 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6812 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6813 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6814 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6815 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6816 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6817 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6818 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6819 |
args: anElement : <object> |
8874 | 6820 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6821 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6822 |
returns: elementIndex - if found |
8874 | 6823 |
exceptionBlock value - if not found |
6824 |
" |
|
6825 |
||
6826 |
" |
|
6827 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6828 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6829 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6830 |
#(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
|
6831 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6832 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6833 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6834 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6835 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6836 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6837 |
"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
|
6838 |
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
|
6839 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6840 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6841 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6842 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6843 |
args: anElement : <object> |
8874 | 6844 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6845 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6846 |
returns: elementIndex - if found |
8874 | 6847 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6848 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6849 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6850 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6851 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6852 |
"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
|
6853 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6854 |
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
|
6855 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6856 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6857 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6858 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6859 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6860 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6861 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6862 |
startIndex to:stop do:[:index | |
8874 | 6863 |
el := self at:index. |
6864 |
anElement == el ifTrue:[^ index]. |
|
6865 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6866 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6867 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6868 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6869 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6870 |
args: anElement : <object> |
8874 | 6871 |
anotherElement : <object> |
6872 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6873 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6874 |
returns: elementIndex - if found |
8874 | 6875 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6876 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6877 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6878 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6879 |
#(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
|
6880 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6881 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6882 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6883 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6884 |
"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
|
6885 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6886 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6887 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6888 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6889 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6890 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6891 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6892 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6893 |
startIndex to:stop do:[:index | |
8874 | 6894 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6895 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6896 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6897 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6898 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6899 |
args: anElement : <object> |
8874 | 6900 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6901 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6902 |
returns: elementIndex - if found |
8874 | 6903 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6904 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6905 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6906 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6907 |
#(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
|
6908 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6909 |
#(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
|
6910 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6911 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6912 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6913 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6914 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6915 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6916 |
"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
|
6917 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6918 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6919 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6920 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6921 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6922 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6923 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6924 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6925 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6926 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6927 |
startIndex to:stopIndex do:[:index | |
8874 | 6928 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6929 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6930 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6931 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6932 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6933 |
args: anElement : <object> |
8874 | 6934 |
start : <integer> |
6935 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6936 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6937 |
returns: elementIndex - if found |
8874 | 6938 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6939 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6940 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6941 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6942 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6943 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6944 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6945 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6946 |
"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
|
6947 |
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
|
6948 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6949 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6950 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6951 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6952 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6953 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6954 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6955 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6956 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6957 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6958 |
args: anElement : <object> |
8874 | 6959 |
start : <integer> |
6960 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6961 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6962 |
returns: elementIndex - if found |
8874 | 6963 |
exceptionBlock value - if not found |
6964 |
" |
|
6965 |
||
6966 |
" |
|
6967 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6968 |
#(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
|
6969 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6970 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6971 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6972 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6973 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6974 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6975 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6976 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6977 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6978 |
|
10194 | 6979 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6980 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6981 |
" |
8874 | 6982 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6983 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6984 |
returns: true - if found |
10194 | 6985 |
false - if not found |
8874 | 6986 |
" |
6987 |
||
6988 |
" |
|
6989 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6990 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6991 |
#(10 20 30 40 50 60 70) includes:40 |
|
6992 |
#(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
|
6993 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6994 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6995 |
|
8874 | 6996 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6997 |
#(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
|
6998 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6999 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7000 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7001 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7002 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7003 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7004 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7005 |
if found, return the index otherwise return 0. |
8874 | 7006 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7007 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7008 |
|
8874 | 7009 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7010 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7011 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7012 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7013 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7014 |
returns: elementIndex - if found |
8874 | 7015 |
0 - if not found |
7016 |
" |
|
7017 |
||
7018 |
" |
|
7019 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
7020 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
7021 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
7022 |
#(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
|
7023 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7024 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7025 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7026 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7027 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7028 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7029 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7030 |
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
|
7031 |
exceptionBlock. |
8874 | 7032 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7033 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7034 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7035 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7036 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7037 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7038 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7039 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7040 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7041 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7042 |
args: anElement : <object> |
8874 | 7043 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7044 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7045 |
returns: elementIndex - if found |
8874 | 7046 |
exceptionBlock value - if not found |
7047 |
" |
|
7048 |
||
7049 |
" |
|
7050 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
7051 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
7052 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
7053 |
#(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
|
7054 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7055 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7056 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7057 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7058 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7059 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7060 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7061 |
"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
|
7062 |
if found, return the index otherwise return 0. |
8874 | 7063 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7064 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7065 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7066 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7067 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7068 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7069 |
startIndex to:1 by:-1 do:[:index | |
8874 | 7070 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7071 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7072 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7073 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7074 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7075 |
args: anElement : <object> |
8874 | 7076 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7077 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7078 |
returns: elementIndex - if found |
8874 | 7079 |
0 - if not found |
7080 |
" |
|
7081 |
||
7082 |
" |
|
7083 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
7084 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
7085 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
7086 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
7087 |
#(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
|
7088 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7089 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7090 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7091 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7092 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7093 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 7094 |
"search the collection backwards for anElement starting the search at |
7095 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7096 |
otherwise return the value of the exceptionBlock. |
8874 | 7097 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7098 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7099 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7100 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7101 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7102 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7103 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7104 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7105 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7106 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7107 |
args: anElement : <object> |
8874 | 7108 |
start : <integer> |
7109 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7110 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7111 |
returns: elementIndex - if found |
8874 | 7112 |
exceptionBlock value - if not found |
7113 |
" |
|
7114 |
||
7115 |
" |
|
7116 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
7117 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
7118 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
7119 |
#(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
|
7120 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7121 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7122 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7123 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 7124 |
! ! |
7125 |
||
7126 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
7127 |
||
6958 | 7128 |
detectFirstInOrder:sortBlock |
7129 |
"find the first element of the collection sorted with sortBlock" |
|
7130 |
||
7131 |
|selectedForNow start| |
|
7132 |
||
7133 |
start := true. |
|
7134 |
self do:[:element| |
|
8874 | 7135 |
start ifTrue:[ |
7136 |
start := false. |
|
7137 |
selectedForNow := element. |
|
7138 |
] ifFalse:[ |
|
7139 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
7140 |
selectedForNow := element. |
|
7141 |
]. |
|
7142 |
]. |
|
6958 | 7143 |
]. |
7144 |
||
7145 |
^ selectedForNow |
|
7146 |
||
7147 |
" |
|
8874 | 7148 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
7149 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
7150 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
7151 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 7152 |
" |
7153 |
! |
|
7154 |
||
5358 | 7155 |
mergeSort |
8874 | 7156 |
"sort the collection using a mergeSort algorithm. |
7157 |
The elements are compared using'<' |
|
5358 | 7158 |
i.e. they should offer a magnitude-like protocol. |
7159 |
||
7160 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 7161 |
the best possible for all situations |
5358 | 7162 |
See also #quickSort and #randomizedSort for other sort algorithms |
7163 |
with different worst- and average case behavior)" |
|
7164 |
||
7165 |
|stop| |
|
7166 |
||
7167 |
stop := self size. |
|
7168 |
(stop > 1) ifTrue:[ |
|
8874 | 7169 |
self mergeSort:[:a :b | a < b] from:1 to:stop |
5358 | 7170 |
] |
7171 |
||
7172 |
" |
|
7173 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
7174 |
||
7175 |
|data| |
|
7176 |
data := Random new next:100000. |
|
7177 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
7178 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 7179 |
data reverse. |
5358 | 7180 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
7181 |
||
7182 |
data := Random new next:100000. |
|
7183 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7184 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7185 |
data reverse. |
5358 | 7186 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7187 |
||
7188 |
data := Random new next:100000. |
|
7189 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
7190 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 7191 |
data reverse. |
5358 | 7192 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
7193 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7194 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7195 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7196 |
mergeSort:sortBlock |
8874 | 7197 |
"sort the collection using a mergeSort algorithm. |
7198 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7199 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7200 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7201 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 7202 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7203 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7204 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7205 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7206 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7207 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7208 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7209 |
(stop > 1) ifTrue:[ |
8874 | 7210 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7211 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7212 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7213 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7214 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7215 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7216 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7217 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7218 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7219 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7220 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7221 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7222 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7223 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7224 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7225 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 7226 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7227 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7228 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7229 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7230 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7231 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 7232 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7233 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7234 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7235 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7236 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7237 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 7238 |
"Sort the given range of indices using the mergesort algorithm. |
7239 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 7240 |
as many comparisons as heapsort or quicksort." |
7241 |
||
8874 | 7242 |
"Details: recursively split the range to be sorted into two halves, |
7243 |
mergesort each half, then merge the two halves together. |
|
7244 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
7245 |
and forth between the receiver and this copy. |
|
7246 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 7247 |
resulting in the receiver being completely sorted." |
7248 |
||
7249 |
| temp | |
|
7250 |
||
7251 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
7252 |
startIndex = stopIndex ifTrue: [^ self]. |
|
7253 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 7254 |
ifFalse: [self error: 'bad start index']. |
5358 | 7255 |
stopIndex <= self size |
8874 | 7256 |
ifFalse: [self error: 'bad stop index']. |
5358 | 7257 |
temp := self clone. |
7258 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
7259 |
||
7260 |
" |
|
8874 | 7261 |
#(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
|
7262 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7263 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7264 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7265 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7266 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7267 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7268 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7269 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7270 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7271 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7272 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7273 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7274 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7275 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7276 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 7277 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7278 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7279 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7280 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7281 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7282 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7283 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7284 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7285 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7286 |
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
|
7287 |
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
|
7288 |
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
|
7289 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7290 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7291 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7292 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7293 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7294 |
(stop > 1) ifTrue:[ |
8874 | 7295 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7296 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7297 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7298 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7299 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 7300 |
|
7301 |
|data| |
|
7302 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7303 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7304 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 7305 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7306 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7307 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7308 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7309 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7310 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7311 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7312 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7313 |
"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
|
7314 |
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
|
7315 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7316 |
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
|
7317 |
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
|
7318 |
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
|
7319 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7320 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7321 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7322 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7323 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7324 |
(stop > 1) ifTrue:[ |
8874 | 7325 |
sortBlock numArgs == 3 ifTrue:[ |
7326 |
"/ TODO: pass a collating policy to aBlock |
|
7327 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
7328 |
] ifFalse:[ |
|
7329 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
7330 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7331 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7332 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7333 |
" |
8874 | 7334 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7335 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7336 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7337 |
#(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
|
7338 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7339 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7340 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7341 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7342 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7343 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7344 |
"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
|
7345 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7346 |
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
|
7347 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7348 |
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
|
7349 |
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
|
7350 |
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
|
7351 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7352 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7353 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7354 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7355 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7356 |
(stop > 1) ifTrue:[ |
8874 | 7357 |
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
|
7358 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7359 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7360 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7361 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7362 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7363 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7364 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7365 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7366 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7367 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7368 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7369 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7370 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7371 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7372 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7373 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7374 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7375 |
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
|
7376 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7377 |
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
|
7378 |
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
|
7379 |
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
|
7380 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7381 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7382 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7383 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7384 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7385 |
(stop > 1) ifTrue:[ |
8874 | 7386 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7387 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7388 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7389 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 7390 |
! |
7391 |
||
11272 | 7392 |
randomShuffle |
7393 |
"random shuffle my elements in place. |
|
7394 |
This is a destructive algorithm. |
|
7395 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
7396 |
See 'The Art of Computer Programming'." |
|
7397 |
||
7398 |
self size to:2 by:-1 do:[:endIndex | |
|
7399 |
|rndIndex t| |
|
7400 |
||
7401 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
7402 |
"/ exchange |
|
7403 |
t := self at:rndIndex. |
|
7404 |
self at:rndIndex put:(self at:endIndex). |
|
7405 |
self at:endIndex put:t. |
|
7406 |
]. |
|
7407 |
||
7408 |
" |
|
7409 |
(1 to:10) asOrderedCollection randomShuffle |
|
7410 |
||
7411 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7412 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7413 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7414 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7415 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7416 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7417 |
" |
|
7418 |
||
11273 | 7419 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
7420 |
||
7421 |
|a score| |
|
7422 |
||
7423 |
a := 'abcd'. |
|
7424 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
7425 |
||
7426 |
1000000 times do:[ |
|
7427 |
|sorted| |
|
7428 |
sorted := a copy randomShuffle. |
|
7429 |
score at:sorted put:(score at:sorted)+1. |
|
7430 |
]. |
|
7431 |
||
7432 |
score keys asSortedCollection do:[:k | |
|
7433 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
7434 |
]. |
|
7435 |
" |
|
7436 |
||
7437 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
11272 | 7438 |
! |
7439 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7440 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7441 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7442 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7443 |
|
8874 | 7444 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7445 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7446 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7447 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7448 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7449 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7450 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7451 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7452 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7453 |
"/ 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
|
7454 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7455 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7456 |
"/ 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
|
7457 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7458 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7459 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7460 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7461 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7462 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7463 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7464 |
|
8874 | 7465 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7466 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7467 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7468 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7469 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7470 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7471 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7472 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7473 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7474 |
"/ 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
|
7475 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7476 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7477 |
"/ 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
|
7478 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7479 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7480 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7481 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7482 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7483 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7484 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7485 |
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
|
7486 |
|
8874 | 7487 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7488 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7489 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7490 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7491 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7492 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7493 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7494 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7495 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7496 |
"/ 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
|
7497 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7498 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7499 |
"/ 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
|
7500 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7501 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7502 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7503 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7504 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7505 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7506 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7507 |
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
|
7508 |
|
8874 | 7509 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7510 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7511 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7512 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7513 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7514 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7515 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7516 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7517 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7518 |
"/ 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
|
7519 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7520 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7521 |
"/ 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
|
7522 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7523 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7524 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7525 |
|
602 | 7526 |
reverse |
7335 | 7527 |
"destructively reverse the order of the elements inplace. |
11264 | 7528 |
WARNING: this is a destructive operation, which modifies the receiver. |
7529 |
Please use reversed (with a d) for a functional version." |
|
602 | 7530 |
|
7531 |
|lowIndex "{ Class: SmallInteger }" |
|
7532 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7533 |
t1 t2| |
602 | 7534 |
|
7535 |
hiIndex := self size. |
|
7536 |
lowIndex := 1. |
|
7537 |
[lowIndex < hiIndex] whileTrue:[ |
|
11264 | 7538 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
7539 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
7540 |
||
7541 |
lowIndex := lowIndex + 1. |
|
7542 |
hiIndex := hiIndex - 1 |
|
602 | 7543 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7544 |
|
69 | 7545 |
" |
602 | 7546 |
#(4 5 6 7 7) reverse |
7547 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 7548 |
" |
1 | 7549 |
! |
7550 |
||
5004 | 7551 |
reversed |
7552 |
"return a copy with elements in reverse order" |
|
7553 |
||
7554 |
^ self copy reverse |
|
7555 |
||
7556 |
" |
|
8874 | 7557 |
#(4 5 6 7 7) reversed |
5004 | 7558 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
7559 |
" |
|
7560 |
! |
|
7561 |
||
1 | 7562 |
sort |
7563 |
"sort the collection inplace. The elements are compared using |
|
2069 | 7564 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7565 |
|
159 | 7566 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7567 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7568 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7569 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 7570 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7571 |
self quickSort |
1 | 7572 |
|
95 | 7573 |
" |
7574 |
#(1 16 7 98 3 19 4 0) sort |
|
7575 |
||
7576 |
|data| |
|
7577 |
data := Random new next:100000. |
|
7578 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7579 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7580 |
data reverse. |
95 | 7581 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7582 |
" |
|
2069 | 7583 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7584 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 7585 |
! |
7586 |
||
7587 |
sort:sortBlock |
|
7588 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7589 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7590 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7591 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7592 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7593 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7594 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7595 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7596 |
self quickSort:sortBlock |
1 | 7597 |
|
95 | 7598 |
" |
8874 | 7599 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7600 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7601 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7602 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 7603 |
" |
2069 | 7604 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7605 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 7606 |
! |
7607 |
||
7608 |
sort:sortBlock with:aCollection |
|
7609 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 7610 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7611 |
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
|
7612 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7613 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7614 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7615 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7616 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7617 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7618 |
self quickSort:sortBlock with:aCollection |
1 | 7619 |
|
69 | 7620 |
" |
7621 |
|c1 c2| |
|
1 | 7622 |
c1 := #(1 16 7 9). |
7623 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
7624 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 7625 |
c1 printNL. |
7626 |
c2 printNL |
|
69 | 7627 |
" |
2069 | 7628 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7629 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 7630 |
! |
7631 |
||
11154 | 7632 |
sortBySelector:aSelector |
7633 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
7634 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
7635 |
||
7636 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
7637 |
||
7638 |
" |
|
7639 |
replace all uses of sort as in: |
|
7640 |
... sort:[:a :b | a foo < b foo] |
|
7641 |
by: |
|
7642 |
... sortBySelector:#foo |
|
7643 |
||
7644 |
find these by searching for code matching (code-search in the browsers method list): |
|
7645 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
7646 |
" |
|
7647 |
! |
|
7648 |
||
602 | 7649 |
sortWith:aCollection |
2069 | 7650 |
"sort the receiver collection inplace, using '<' to compare elements. |
7651 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7652 |
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
|
7653 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7654 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7655 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7656 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7657 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 7658 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7659 |
self quickSortWith:aCollection |
424 | 7660 |
! |
7661 |
||
11279 | 7662 |
sortedBy:aBlock |
9072 | 7663 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
7664 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
7665 |
||
7666 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
11279 | 7667 |
|
7668 |
"Modified: / 22-10-2008 / 21:25:07 / cg" |
|
9072 | 7669 |
! |
7670 |
||
602 | 7671 |
topologicalSort:sortBlock |
7672 |
"sort the collection inplace using a sloooow sort algorithm. |
|
7673 |
This algorithm has O-square runtime behavior and should be used only |
|
7674 |
in special situations. |
|
7675 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
7676 |
a>c (for example, to sort classes by inheritance) |
|
7677 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7678 |
In all other situations, use #sort; which implements the quicksort algorithm. |
10812 | 7679 |
|
7680 |
If there are cycles, this algorithm may loop endless!! |
|
7681 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
77 | 7682 |
" |
602 | 7683 |
|
7684 |
|smallestIndex "{ Class: SmallInteger }" |
|
7685 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7686 |
index2 "{ Class: SmallInteger }" |
602 | 7687 |
smallest thisOne| |
7688 |
||
7689 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
7690 |
||
7691 |
end := self size. |
|
862 | 7692 |
1 to:end-1 do:[:index | |
10812 | 7693 |
smallest := self at:index. |
7694 |
smallestIndex := index. |
|
7695 |
||
7696 |
index2 := index + 1. |
|
7697 |
[ |
|
7698 |
thisOne := self at:index2. |
|
7699 |
(index2 ~~ smallestIndex |
|
7700 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
7701 |
smallestIndex := index2. |
|
7702 |
smallest := thisOne. |
|
7703 |
index2 := index + 1. |
|
7704 |
] ifFalse:[ |
|
7705 |
index2 := index2 + 1. |
|
7706 |
]. |
|
7707 |
] doWhile:[index2 <= end]. |
|
862 | 7708 |
|
7709 |
"/ (index + 1) to:end do:[:index2 | |
|
7710 |
"/ thisOne := self at:index2. |
|
7711 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
7712 |
"/ smallestIndex := index2. |
|
7713 |
"/ smallest := thisOne |
|
7714 |
"/ ]. |
|
7715 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7716 |
|
10812 | 7717 |
(smallestIndex ~~ index) ifTrue:[ |
7718 |
thisOne := self at:index. |
|
7719 |
self at:index put:smallest. |
|
7720 |
self at:smallestIndex put:thisOne |
|
7721 |
]. |
|
1 | 7722 |
] |
95 | 7723 |
|
77 | 7724 |
" |
8874 | 7725 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
7726 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
10812 | 7727 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
8874 | 7728 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
7729 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 7730 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7731 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7732 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 7733 |
! ! |
602 | 7734 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7735 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7736 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7737 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7738 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7739 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 7740 |
however, having a common protocol with indexed collections |
7741 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7742 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7743 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7744 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7745 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7746 |
" |
8874 | 7747 |
#(1 2 3) includesKey:4 |
7748 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7749 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7750 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7751 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7752 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7753 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7754 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7755 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7756 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7757 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7758 |
|
2069 | 7759 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7760 |
|
7761 |
version |
|
12187 | 7762 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.284 2009-10-09 10:22:47 fm Exp $' |
12051 | 7763 |
! |
7764 |
||
7765 |
version_CVS |
|
12187 | 7766 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.284 2009-10-09 10:22:47 fm Exp $' |
628 | 7767 |
! ! |
6820 | 7768 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7769 |
SequenceableCollection initialize! |