author | Stefan Vogel <sv@exept.de> |
Wed, 05 May 2010 15:56:55 +0200 | |
changeset 12915 | d80685e8f20e |
parent 12888 | 5de3c4d8bd8b |
child 12938 | 9fb4e6f91c83 |
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 |
||
12647 | 361 |
|
7261 | 362 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 363 |
|
7796 | 364 |
allButFirst |
8874 | 365 |
"Return a copy of the receiver containing all but the first element. |
7796 | 366 |
Raise an error if there are not enough elements." |
367 |
||
368 |
^ self copyFrom:2 |
|
8571 | 369 |
|
370 |
" |
|
371 |
'1234567890' allButFirst |
|
372 |
" |
|
373 |
! |
|
374 |
||
375 |
allButFirst:n |
|
8874 | 376 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 377 |
Raise an error if there are not enough elements." |
378 |
||
379 |
^ self copyFrom:n+1 |
|
380 |
||
381 |
" |
|
382 |
'1234567890' allButFirst:5 |
|
383 |
" |
|
7796 | 384 |
! |
385 |
||
6149 | 386 |
allButLast |
8874 | 387 |
"Return a copy of the receiver containing all but the last element. |
7796 | 388 |
Raise an error if there are not enough elements." |
6183 | 389 |
|
390 |
^ self allButLast:1 |
|
391 |
||
8571 | 392 |
" |
393 |
'1234567890' allButLast |
|
394 |
" |
|
395 |
||
6183 | 396 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
397 |
! |
|
398 |
||
399 |
allButLast:n |
|
8874 | 400 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 401 |
Raise an error if there are not enough elements." |
6183 | 402 |
|
403 |
^ self copyFrom: 1 to: self size - n |
|
404 |
||
8571 | 405 |
" |
406 |
'1234567890' allButLast:5 |
|
407 |
" |
|
408 |
||
6183 | 409 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 410 |
! |
411 |
||
8874 | 412 |
atPin: index |
4414 | 413 |
"Return the index'th element of me if possible. |
414 |
Return the first or last element if index is out of bounds." |
|
415 |
||
416 |
index < 1 ifTrue:[^ self first]. |
|
417 |
index > self size ifTrue:[^ self last]. |
|
418 |
^ self at:index |
|
419 |
||
420 |
" |
|
8874 | 421 |
#(1 2 3) atPin:4 |
422 |
#(1 2 3) atPin:0 |
|
423 |
#(1 2 3) atPin:1 |
|
424 |
#(1 2 3) atPin:3 |
|
425 |
" |
|
426 |
! |
|
427 |
||
428 |
atRandom |
|
4459 | 429 |
"Return any random element from the receiver" |
430 |
||
8573 | 431 |
^ self atRandom:Random |
432 |
||
433 |
" |
|
8874 | 434 |
#(1 2 3) atRandom |
435 |
" |
|
436 |
! |
|
437 |
||
438 |
atRandom:aRandomGenerator |
|
8573 | 439 |
"Return any random element from the receiver" |
440 |
||
4459 | 441 |
|max idx| |
442 |
||
443 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 444 |
self emptyCollectionError |
4459 | 445 |
]. |
8573 | 446 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 447 |
^ self at:idx |
448 |
||
449 |
" |
|
8874 | 450 |
#(1 2 3) atRandom:(Random new) |
451 |
" |
|
452 |
! |
|
453 |
||
454 |
atWrap:index |
|
4975 | 455 |
"Return the index'th element of me if possible. |
456 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
457 |
||
458 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
459 |
||
460 |
" |
|
8874 | 461 |
#(1 2 3) atWrap:1 |
462 |
#(1 2 3) atWrap:2 |
|
463 |
#(1 2 3) atWrap:3 |
|
464 |
#(1 2 3) atWrap:4 |
|
465 |
#(1 2 3) atWrap:5 |
|
466 |
#(1 2 3) atWrap:6 |
|
467 |
#(1 2 3) atWrap:7 |
|
468 |
#(1 2 3) atWrap:0 |
|
469 |
#(1 2 3) atWrap:-1 |
|
4975 | 470 |
" |
5372 | 471 |
! |
472 |
||
473 |
beginsWith:aCollection |
|
6183 | 474 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 475 |
|
476 |
^ self startsWith:aCollection |
|
6183 | 477 |
|
478 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 479 |
! |
480 |
||
8611 | 481 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 482 |
"squeak uses a different naming here - sigh. |
483 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
484 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
485 |
this method first takes a slice (from:to:) and then collects over that. |
|
486 |
I.e. it is equivalent to: |
|
8874 | 487 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 488 |
|
489 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
490 |
! |
|
491 |
||
7012 | 492 |
copyAfter: anElement |
8874 | 493 |
"Answer a copy of the receiver from after the first occurrence |
494 |
of anElement up to the end. If no such element exists, answer |
|
495 |
an empty copy." |
|
7012 | 496 |
|idx| |
497 |
||
498 |
idx := self indexOf:anElement. |
|
499 |
idx == 0 ifTrue:[idx := self size]. |
|
500 |
^ self copyFrom:idx + 1 |
|
501 |
||
502 |
" |
|
8874 | 503 |
'hello world' copyAfter:$l |
504 |
'123456123456' copyAfter:$2 |
|
505 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
506 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
507 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 508 |
" |
509 |
! |
|
510 |
||
6982 | 511 |
copyAfterLast:element |
8874 | 512 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 513 |
of element to the end; uses = for comparison" |
514 |
||
515 |
|idx| |
|
516 |
||
517 |
idx := self lastIndexOf:element. |
|
518 |
idx == 0 ifTrue:[idx := self size]. |
|
519 |
^ self copyFrom:idx + 1 |
|
520 |
||
521 |
" |
|
8874 | 522 |
'hello world' copyAfterLast:$l |
523 |
'123456123456' copyAfterLast:$2 |
|
524 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
525 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
526 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 527 |
" |
528 |
! |
|
529 |
||
6980 | 530 |
copyUpToLast:element |
8874 | 531 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 532 |
of element; uses = for comparison" |
533 |
||
534 |
|idx| |
|
535 |
||
536 |
idx := self lastIndexOf:element. |
|
537 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
538 |
^ self copyTo:idx - 1 |
|
539 |
||
540 |
" |
|
8874 | 541 |
'hello world' copyUpToLast:$l |
542 |
'123456123456' copyUpToLast:$2 |
|
543 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
544 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
545 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 546 |
" |
547 |
! |
|
548 |
||
8276 | 549 |
copyWithoutAll:elementsToSkip |
550 |
"return a new collection consisting of a copy of the receiver, with |
|
551 |
ALL elements equal to any in elementsToSkip are left out. |
|
552 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
553 |
||
554 |
|n "{ Class: SmallInteger }" |
|
555 |
sz "{ Class: SmallInteger }" |
|
556 |
srcIndex "{ Class: SmallInteger }" |
|
557 |
dstIndex "{ Class: SmallInteger }" |
|
558 |
skipIndex "{ Class: SmallInteger }" |
|
559 |
copy l| |
|
560 |
||
8874 | 561 |
"the code below may look like overkill, |
8276 | 562 |
however, for big collections its better to move data |
563 |
around in big chunks" |
|
564 |
||
565 |
n := self occurrencesOfAny:elementsToSkip. |
|
566 |
n == 0 ifTrue:[^ self copy]. |
|
567 |
||
568 |
sz := self size. |
|
569 |
copy := self copyEmptyAndGrow:(sz - n). |
|
570 |
||
571 |
srcIndex := 1. |
|
572 |
dstIndex := 1. |
|
573 |
||
574 |
n timesRepeat:[ |
|
8874 | 575 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
576 |
l := skipIndex - srcIndex. |
|
577 |
l ~~ 0 ifTrue:[ |
|
578 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
579 |
with:self startingAt:srcIndex. |
|
580 |
dstIndex := dstIndex + l |
|
581 |
]. |
|
582 |
srcIndex := skipIndex + 1 |
|
8276 | 583 |
]. |
584 |
l := sz - srcIndex. |
|
585 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 586 |
with:self startingAt:srcIndex. |
8276 | 587 |
^ copy |
588 |
||
589 |
" |
|
590 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 591 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 592 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
593 |
" |
|
594 |
! |
|
595 |
||
7304 | 596 |
copyWithoutFirst |
597 |
^ self copyFrom:2 |
|
598 |
! |
|
599 |
||
12051 | 600 |
shuffled |
601 |
"return a randomly shuffled copy of the receiver" |
|
602 |
||
603 |
^ self shuffledBy:Random |
|
604 |
||
605 |
" |
|
606 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
607 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
608 |
" |
|
609 |
! |
|
610 |
||
611 |
shuffledBy:aRandom |
|
612 |
"return a randomly shuffled copy of the receiver, using the given random generator" |
|
613 |
||
614 |
|copy| |
|
615 |
||
616 |
copy := self shallowCopy. |
|
617 |
copy size to: 1 by: -1 do:[:i | |
|
618 |
copy swap: i with: ((1 to: i) atRandom: aRandom) |
|
619 |
]. |
|
620 |
^ copy |
|
621 |
||
622 |
" |
|
623 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
624 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
625 |
" |
|
626 |
! |
|
627 |
||
11279 | 628 |
sortBy:aBlock |
629 |
"Sort inplace - destructive" |
|
630 |
||
631 |
self sort:aBlock |
|
632 |
||
633 |
"Created: / 22-10-2008 / 21:25:35 / cg" |
|
4414 | 634 |
! ! |
635 |
||
7261 | 636 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 637 |
|
638 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
639 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
640 |
! ! |
|
641 |
||
12647 | 642 |
|
1 | 643 |
!SequenceableCollection methodsFor:'accessing'! |
644 |
||
1366 | 645 |
after:anObject |
646 |
"return the element, after anObject. |
|
5231 | 647 |
If anObject is not in the receiver, report an error; |
648 |
if anObject is the last in the receiver, return nil. |
|
1366 | 649 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
650 |
||
651 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
652 |
||
653 |
" |
|
8874 | 654 |
#(4 3 2 1) asOrderedCollection after:3. |
655 |
#(4 3 2 1) asOrderedCollection after:5 |
|
656 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 657 |
" |
658 |
||
659 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
660 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
661 |
! |
|
662 |
||
663 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 664 |
"return the element, after anObject. |
5231 | 665 |
If anObject is the last in the receiver, return nil. |
666 |
If there is no such element (anObject is not found), |
|
1366 | 667 |
return the value from exceptionBlock." |
668 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
669 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
670 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
671 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
672 |
idx ~~ 0 ifTrue:[ |
8874 | 673 |
idx == self size ifTrue:[^ nil]. |
674 |
^ self at:(idx + 1). |
|
5501
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 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
677 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
678 |
" |
8874 | 679 |
#(4 3 2 1) after:3 ifAbsent:nil. |
680 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
681 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
682 |
(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
|
683 |
" |
1366 | 684 |
! |
685 |
||
6967 | 686 |
anElement |
8874 | 687 |
"return any element from the collection, |
6967 | 688 |
report an error if there is none" |
689 |
||
690 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
691 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
692 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
693 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
694 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
695 |
" |
6967 | 696 |
! |
697 |
||
32 | 698 |
at:index ifAbsent:exceptionBlock |
8874 | 699 |
"return the element at index if valid. |
700 |
If the index is invalid, return the result of evaluating |
|
302 | 701 |
the exceptionblock. |
8874 | 702 |
NOTICE: |
703 |
in ST-80, this message is only defined for Dictionaries, |
|
704 |
however, having a common protocol with indexed collections |
|
705 |
often simplifies things." |
|
32 | 706 |
|
8579 | 707 |
(index between:1 and:self size) ifFalse:[ |
8874 | 708 |
^ exceptionBlock value |
32 | 709 |
]. |
710 |
^ self at:index |
|
711 |
||
95 | 712 |
" |
713 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 714 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
715 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 716 |
|
95 | 717 |
(Dictionary with:(#foo -> #bar) |
8874 | 718 |
with:(#frob -> #baz)) |
719 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 720 |
" |
362 | 721 |
! |
722 |
||
1366 | 723 |
before:anObject |
724 |
"return the element before the argument, anObject. |
|
5231 | 725 |
If anObject is not in the receiver, report an error; |
726 |
if anObject is the first in the receiver, return nil. |
|
1366 | 727 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
728 |
||
729 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
730 |
||
731 |
" |
|
8874 | 732 |
#(4 3 2 1) asOrderedCollection before:3. |
733 |
#(4 3 2 1) asOrderedCollection before:4 |
|
734 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 735 |
" |
736 |
||
737 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
738 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
739 |
! |
|
740 |
||
741 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 742 |
"return the element, before anObject. |
5231 | 743 |
If anObject is the first in the receiver, return nil. |
744 |
If there is no such element (anObject is not found), |
|
1366 | 745 |
return the value from exceptionBlock." |
746 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
747 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
748 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
749 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
750 |
idx ~~ 0 ifTrue:[ |
8874 | 751 |
idx == 1 ifTrue:[^ nil]. |
752 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
753 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
754 |
^ exceptionBlock value |
1366 | 755 |
|
5231 | 756 |
" |
8874 | 757 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
758 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
759 |
#(4 3 2 1) asOrderedCollection before:5. |
|
760 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
761 |
#(4 3 2 1) asOrderedCollection before:4. |
|
762 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
763 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 764 |
" |
765 |
||
1366 | 766 |
! |
767 |
||
602 | 768 |
first |
8874 | 769 |
"return the first element; |
5231 | 770 |
report an error, if the collection is empty." |
602 | 771 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
772 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 773 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
774 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
775 |
" |
8874 | 776 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
777 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
778 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
779 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
780 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 781 |
! |
782 |
||
5887 | 783 |
first:n |
784 |
"return the n first elements of the collection. |
|
785 |
Raises an error if there are not enough elements in the receiver." |
|
786 |
||
787 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
788 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
789 |
||
790 |
^ self copyFirst:n |
|
791 |
||
792 |
" |
|
8874 | 793 |
#(1 2 3 4 5) first:3 |
794 |
#(1 2 3 4 5) first:6 |
|
795 |
#(1 2 3 4 5) asSet first:3 |
|
796 |
'hello world' first:5 |
|
5887 | 797 |
" |
798 |
! |
|
799 |
||
11878 | 800 |
indexOfNth:n occurrenceOf:what |
801 |
"return the index of the nTh occurence of a value, or 0 if there are not that many" |
|
802 |
||
803 |
|idx cnt| |
|
804 |
||
805 |
n > self size ifTrue:[^ 0]. |
|
806 |
cnt := 0. |
|
807 |
idx := 0. |
|
808 |
[ |
|
809 |
idx := self indexOf:what startingAt:idx+1. |
|
810 |
idx == 0 ifTrue:[^ 0]. |
|
811 |
cnt := cnt + 1. |
|
812 |
cnt = n ifTrue:[ ^ idx]. |
|
813 |
] loop. |
|
814 |
||
815 |
" |
|
816 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:1 |
|
817 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:9 |
|
818 |
" |
|
819 |
! |
|
820 |
||
362 | 821 |
keyAtEqualValue:value |
822 |
"return the index of a value. |
|
823 |
This is normally not used (use indexOf:), but makes the |
|
824 |
protocol more compatible with dictionaries." |
|
825 |
||
826 |
^ self indexOf:value |
|
827 |
! |
|
828 |
||
829 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
830 |
"return the index of a value. |
|
831 |
This is normally not used (use indexOf:), but makes the |
|
832 |
protocol more compatible with dictionaries." |
|
833 |
||
834 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 835 |
! |
836 |
||
837 |
keyAtValue:value |
|
838 |
"return the index of a value. |
|
839 |
This is normally not used (use indexOf:), but makes the |
|
840 |
protocol more compatible with dictionaries." |
|
841 |
||
842 |
^ self identityIndexOf:value |
|
843 |
! |
|
844 |
||
845 |
keyAtValue:value ifAbsent:exceptionBlock |
|
846 |
"return the index of a value. |
|
847 |
This is normally not used (use indexOf:), but makes the |
|
848 |
protocol more compatible with dictionaries." |
|
849 |
||
850 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
851 |
! |
|
852 |
||
853 |
last |
|
5231 | 854 |
"return the last element; |
855 |
report an error, if the collection is empty." |
|
602 | 856 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
857 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
858 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
859 |
(sz := self size) == 0 ifFalse:[ |
8874 | 860 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
861 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
862 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
863 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
864 |
|
5231 | 865 |
" |
866 |
#(1 2 3 4 5) last |
|
867 |
#() last |
|
868 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
869 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
870 |
|
5887 | 871 |
last:n |
872 |
"return the n last elements of the collection. |
|
873 |
Raises an error if there are not enough elements in the receiver." |
|
874 |
||
875 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
876 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
877 |
||
878 |
^ self copyLast:n |
|
879 |
||
880 |
" |
|
8874 | 881 |
#(1 2 3 4 5) last:3 |
882 |
#(1 2 3 4 5) last:6 |
|
883 |
#(1 2 3 4 5) asSet last:3 |
|
884 |
'hello world' last:5 |
|
5887 | 885 |
" |
886 |
! |
|
887 |
||
4986 | 888 |
nth:n |
5231 | 889 |
"return the nth element; |
890 |
report an error, if the collections size is smaller than n." |
|
4986 | 891 |
|
892 |
^ self at:n |
|
893 |
||
894 |
" |
|
8874 | 895 |
args: |
4986 | 896 |
returns: nth element <object> |
897 |
" |
|
898 |
||
5231 | 899 |
" |
8874 | 900 |
#(10 20 30 40 50) nth:4 |
5231 | 901 |
#(10 20 30 40 50) nth:6 |
902 |
#() nth:1 |
|
903 |
" |
|
904 |
||
4986 | 905 |
! |
906 |
||
11915 | 907 |
order |
908 |
"the order is the set of keys " |
|
909 |
||
910 |
^ 1 to:self size |
|
911 |
! |
|
912 |
||
4855 | 913 |
second |
5231 | 914 |
"return the second element; |
915 |
report an error, if the collections size is smaller than 2." |
|
4855 | 916 |
|
917 |
^ self at:2 |
|
918 |
||
919 |
" |
|
8874 | 920 |
args: |
4855 | 921 |
returns: secondElement <object> |
922 |
" |
|
923 |
||
924 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
925 |
! |
|
926 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
927 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
928 |
"exchange two elements" |
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| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
931 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
932 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
933 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
934 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
935 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
936 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 937 |
! ! |
938 |
||
602 | 939 |
!SequenceableCollection methodsFor:'adding & removing'! |
940 |
||
941 |
add:anObject |
|
942 |
"append the argument, anObject to the collection. |
|
943 |
Return the argument, anObject. |
|
944 |
||
945 |
Notice, that this is modifies the receiver NOT a copy. |
|
946 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 947 |
due to the grow:-message, which is inefficient for fixed size |
602 | 948 |
collections (i.e. for Strings and Arrays it is not recommened)." |
949 |
||
950 |
|newSize| |
|
951 |
||
952 |
newSize := self size + 1. |
|
953 |
self grow:newSize. |
|
954 |
self at:newSize put:anObject. |
|
955 |
^ anObject |
|
956 |
||
957 |
" |
|
958 |
|a| |
|
959 |
a := #(1 2 3 4 5 6 7 8). |
|
960 |
a add:'hello'. |
|
961 |
a |
|
962 |
" |
|
963 |
" |
|
964 |
|c| |
|
965 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
966 |
c add:'hello'. |
|
967 |
c |
|
968 |
" |
|
969 |
! |
|
970 |
||
971 |
add:anElement beforeIndex:index |
|
972 |
"insert the first argument, anObject into the collection before slot index. |
|
973 |
Return the receiver (sigh - ST-80 compatibility). |
|
974 |
||
975 |
Notice, that this is modifies the receiver NOT a copy. |
|
976 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 977 |
due to the grow:-message, which is inefficient for fixed size |
602 | 978 |
collections (i.e. for Strings and Arrays it is not recommened)." |
979 |
||
980 |
|newSize| |
|
981 |
||
982 |
newSize := self size + 1. |
|
983 |
self grow:newSize. |
|
984 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
985 |
self at:index put:anElement |
|
986 |
||
987 |
" |
|
988 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
989 |
" |
|
202 | 990 |
! |
991 |
||
602 | 992 |
addFirst:anObject |
993 |
"prepend the argument, anObject to the collection. |
|
994 |
Return the argument, anObject. |
|
995 |
||
996 |
Notice, that this is modifies the receiver NOT a copy. |
|
997 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 998 |
due to the grow:-message, which is inefficient for fixed size |
602 | 999 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1000 |
||
1001 |
|newSize| |
|
1002 |
||
1003 |
newSize := self size + 1. |
|
1004 |
self grow:newSize. |
|
1005 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
1006 |
self at:1 put:anObject. |
|
1007 |
^ anObject |
|
1008 |
||
1009 |
" |
|
8874 | 1010 |
|a| |
1011 |
a:= #(1 2 3 4 5 6 7 8). |
|
1012 |
a addFirst:'hello'. |
|
1013 |
a |
|
602 | 1014 |
" |
1015 |
" |
|
1016 |
|c| |
|
1017 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1018 |
c addFirst:'hello'. |
|
1019 |
c |
|
1020 |
" |
|
69 | 1021 |
! |
1022 |
||
602 | 1023 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1024 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1025 |
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
|
1026 |
Use equality compare (=) to search for an occurrence. |
602 | 1027 |
|
1028 |
Notice, that this is modifies the receiver NOT a copy. |
|
1029 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1030 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1031 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1032 |
||
8874 | 1033 |
|any |
602 | 1034 |
dstIndex "{ Class: SmallInteger }" |
1035 |
sz "{ Class: SmallInteger }"| |
|
1036 |
||
1037 |
dstIndex := 1. |
|
1038 |
any := false. |
|
1039 |
sz := self size. |
|
1040 |
1 to:sz do:[:srcIndex | |
|
8874 | 1041 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1042 |
any := true |
|
1043 |
] ifFalse:[ |
|
1044 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1045 |
self at:dstIndex put:(self at:srcIndex) |
|
1046 |
]. |
|
1047 |
dstIndex := dstIndex + 1 |
|
1048 |
] |
|
602 | 1049 |
]. |
1050 |
any ifTrue:[ |
|
8874 | 1051 |
self grow:dstIndex - 1. |
1052 |
^ anElement |
|
602 | 1053 |
]. |
1054 |
^ aBlock value |
|
1055 |
||
1056 |
" |
|
1422 | 1057 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1058 |
#(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
|
1059 |
#(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
|
1060 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1061 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1062 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1063 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1064 |
! |
1065 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1066 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1067 |
"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
|
1068 |
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
|
1069 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1070 |
Return a collection containing the removed elements." |
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 |
"/ 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
|
1073 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1074 |
|runIndex removed element| |
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 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1077 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1078 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1079 |
[runIndex <= self size] whileTrue:[ |
8874 | 1080 |
element := self at:runIndex. |
1081 |
(aBlock value:element) ifTrue:[ |
|
1082 |
removed add:element. |
|
1083 |
self removeAtIndex:runIndex |
|
1084 |
] ifFalse:[ |
|
1085 |
runIndex := runIndex + 1 |
|
1086 |
] |
|
1369
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 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1089 |
|
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| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1092 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1093 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1094 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1095 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1096 |
" |
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 |
"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
|
1099 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1100 |
|
602 | 1101 |
removeAtIndex:anIndex |
1102 |
"remove the element stored at anIndex. Return the removed object. |
|
1103 |
||
1104 |
Notice, that this is modifies the receiver NOT a copy. |
|
1105 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1106 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1107 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1108 |
||
1109 |
|element| |
|
1110 |
||
1111 |
element := self at:anIndex. |
|
1112 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1113 |
^ element |
|
1114 |
||
1115 |
" |
|
1116 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1117 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1118 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1119 |
" |
|
13 | 1120 |
! |
1121 |
||
602 | 1122 |
removeFirst |
1123 |
"remove the first element of the receiver and return it. |
|
1124 |
||
1125 |
Notice, that this is modifies the receiver NOT a copy. |
|
1126 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1127 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1128 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1129 |
||
1130 |
^ self removeAtIndex:1 |
|
1131 |
||
1132 |
" |
|
1133 |
|a| |
|
1134 |
a := #(1 2 3 4 5 6). |
|
1135 |
a removeFirst. |
|
8874 | 1136 |
a |
602 | 1137 |
" |
13 | 1138 |
! |
1139 |
||
2294 | 1140 |
removeFromIndex:startIndex |
1141 |
"remove the elements stored at indexes from startIndex to the end. |
|
1142 |
Return the receiver. |
|
1143 |
Returning the receiver is a historic leftover - it may at one |
|
1144 |
time return a collection of the removed elements. |
|
1145 |
||
8874 | 1146 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1147 |
therefore any other users of the receiver will also see this change. |
1148 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1149 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1150 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1151 |
||
1152 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1153 |
||
1154 |
" |
|
8874 | 1155 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1156 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1157 |
" |
|
1158 |
||
1159 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1160 |
! |
|
1161 |
||
602 | 1162 |
removeFromIndex:startIndex toIndex:endIndex |
1163 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1164 |
Return the receiver. |
|
1165 |
Returning the receiver is a historic leftover - it may at one |
|
1166 |
time return a collection of the removed elements. |
|
1167 |
||
8874 | 1168 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1169 |
therefore any other users of the receiver will also see this change. |
1170 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1171 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1172 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1173 |
||
1174 |
|size newSize| |
|
1175 |
||
1176 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1177 |
self grow:(startIndex - 1) |
602 | 1178 |
] ifFalse:[ |
8874 | 1179 |
newSize := size - endIndex + startIndex - 1. |
1180 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1181 |
self grow:newSize |
|
602 | 1182 |
] |
1183 |
||
1184 |
"/ |newSize| |
|
1185 |
"/ |
|
1186 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1187 |
"/ newSize <= 0 ifTrue:[ |
|
1188 |
"/ self grow:0 |
|
1189 |
"/ ] ifFalse:[ |
|
1190 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1191 |
"/ self grow:newSize |
|
1192 |
"/ ] |
|
1193 |
||
1194 |
" |
|
8874 | 1195 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1196 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1197 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1198 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1199 |
" |
1200 |
! |
|
1201 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1202 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1203 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1204 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1205 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1206 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1207 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1208 |
Also note, that it may be a slow operation for some collections, |
8874 | 1209 |
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
|
1210 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1211 |
|
2363 | 1212 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1213 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1214 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1215 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1216 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1217 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1218 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1219 |
1 to:sz do:[:srcIndex | |
8874 | 1220 |
el := self at:srcIndex. |
1221 |
(anElement == el) ifTrue:[ |
|
1222 |
any := true |
|
1223 |
] ifFalse:[ |
|
1224 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1225 |
self at:dstIndex put:el |
|
1226 |
]. |
|
1227 |
dstIndex := dstIndex + 1 |
|
1228 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1229 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1230 |
any ifTrue:[ |
8874 | 1231 |
self grow:dstIndex - 1. |
1232 |
^ anElement |
|
2346
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 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1235 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1236 |
" |
8874 | 1237 |
#(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
|
1238 |
#(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
|
1239 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1240 |
|
2363 | 1241 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1242 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1243 |
|
602 | 1244 |
removeIndex:index |
1245 |
"remove the argument stored at index. Return the receiver. |
|
1246 |
||
1247 |
Notice, that this is modifies the receiver NOT a copy. |
|
1248 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1249 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1250 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1251 |
||
1252 |
self removeFromIndex:index toIndex:index |
|
1253 |
||
1254 |
" |
|
1255 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1256 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1257 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1258 |
" |
|
77 | 1259 |
! |
1260 |
||
602 | 1261 |
removeLast |
1262 |
"remove the last element of the receiver and return it. |
|
1263 |
||
1264 |
Notice, that this is modifies the receiver NOT a copy. |
|
1265 |
Also note, that it may be a slow operation for some collections, |
|
1266 |
due to the grow:-message, which is inefficient for fixed size |
|
1267 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1268 |
||
8874 | 1269 |
^ self removeAtIndex:(self size) |
602 | 1270 |
|
1271 |
" |
|
1272 |
|a| |
|
1273 |
a := #(1 2 3 4 5 6). |
|
1274 |
a removeLast. |
|
8874 | 1275 |
a |
602 | 1276 |
" |
1277 |
! ! |
|
1278 |
||
11407 | 1279 |
!SequenceableCollection methodsFor:'combinatoric'! |
1280 |
||
1281 |
combinationsDo: aBlock |
|
11463 | 1282 |
"Repeatly evaluate aBlock with all combinations of elements from the receivers elements. |
1283 |
The receivers elements must be collections of the individuals to be taken for the combinations" |
|
11407 | 1284 |
|
1285 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1286 |
||
1287 |
" |
|
1288 |
(Array |
|
11410 | 1289 |
with:($a to:$d)) |
1290 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1291 |
" |
|
1292 |
" |
|
1293 |
(Array |
|
11407 | 1294 |
with:($a to:$d) |
1295 |
with:(1 to: 4)) |
|
1296 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1297 |
" |
|
1298 |
" |
|
1299 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1300 |
" |
|
11408 | 1301 |
"all combinations of two letters are generated with: |
1302 |
(Array |
|
1303 |
with:($a to:$z) |
|
1304 |
with:($a to:$z)) |
|
1305 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1306 |
" |
|
11432 | 1307 |
" |
1308 |
(Array |
|
1309 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1310 |
with:#(A)) |
|
1311 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1312 |
" |
|
11407 | 1313 |
! |
1314 |
||
1315 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1316 |
"a helper for combinationsDo:" |
|
1317 |
||
11431 | 1318 |
|loopedElement| |
1319 |
||
1320 |
loopedElement := self at:anInteger. |
|
1321 |
||
11407 | 1322 |
anInteger == self size ifTrue:[ |
11431 | 1323 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1324 |
^ self. |
11431 | 1325 |
]. |
1326 |
||
1327 |
loopedElement do:[:el | |
|
1328 |
|newPrefix| |
|
1329 |
||
1330 |
newPrefix := (prefix copyWith:el). |
|
1331 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1332 |
]. |
1333 |
||
1334 |
" |
|
1335 |
(Array |
|
1336 |
with:($a to:$d) |
|
1337 |
with:(1 to: 4)) |
|
1338 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1339 |
" |
|
11431 | 1340 |
" |
1341 |
(Array |
|
1342 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1343 |
with:#(A)) |
|
1344 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1345 |
" |
|
11407 | 1346 |
! |
1347 |
||
11462 | 1348 |
lexicographicPermutationsDo:aBlock |
1349 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1350 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1351 |
permutations in lexicographical order (i.e. sorted by size). |
|
11932 | 1352 |
|
11462 | 1353 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1354 |
the identical same collection (but with different contents) every time. |
1355 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1356 |
of permuted copies. The user of this function must be careful in the block, |
|
1357 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1358 |
|
1359 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1360 |
||
1361 |
" |
|
1362 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1363 |
" |
|
1364 |
! |
|
1365 |
||
1366 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1367 |
"a helper for lexicographicPermutationsDo:" |
|
1368 |
||
1369 |
|mySize values indices| |
|
1370 |
||
1371 |
mySize := self size. |
|
1372 |
anInteger > mySize ifTrue: [^ self]. |
|
1373 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1374 |
values := self copyFrom:anInteger to:mySize. |
|
1375 |
indices := (anInteger to:mySize) asArray. |
|
1376 |
values sortWith:indices. |
|
1377 |
indices do:[:i | |
|
1378 |
self swap: anInteger with: i. |
|
1379 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1380 |
self swap: anInteger with: i |
|
1381 |
] |
|
1382 |
||
1383 |
" |
|
1384 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1385 |
" |
|
1386 |
! |
|
1387 |
||
1388 |
permutationsDo:aBlock |
|
1389 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1390 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1391 |
|
11462 | 1392 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1393 |
the identical same collection (but with different contents) every time. |
1394 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1395 |
of permuted copies. The user of this function must be careful in the block, |
|
1396 |
to copy the argument, if it has to be stored somewhere." |
|
11407 | 1397 |
|
1398 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1399 |
||
1400 |
" |
|
11462 | 1401 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1402 |
" |
1403 |
||
1404 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1405 |
! |
|
1406 |
||
1407 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1408 |
"a helper for permutationsDo:" |
11407 | 1409 |
|
1410 |
|mySize| |
|
1411 |
||
1412 |
mySize := self size. |
|
11462 | 1413 |
anInteger > mySize ifTrue: [^ self]. |
1414 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1415 |
anInteger to:mySize do:[:i | |
1416 |
self swap: anInteger with: i. |
|
11462 | 1417 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1418 |
self swap: anInteger with: i |
1419 |
] |
|
1420 |
||
1421 |
" |
|
1422 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1423 |
|
1424 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1425 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1426 |
" |
1427 |
||
1428 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1429 |
! |
1430 |
||
1431 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1432 |
"a helper for permutationsDo:" |
|
1433 |
||
1434 |
|mySize| |
|
1435 |
||
1436 |
mySize := self size. |
|
1437 |
anInteger > mySize ifTrue: [^ self]. |
|
1438 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1439 |
anInteger to:mySize do:[:i | |
|
1440 |
self swap: anInteger with:i. |
|
1441 |
anotherCollection swap: anInteger with:i. |
|
1442 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1443 |
self swap:anInteger with: i. |
|
1444 |
anotherCollection swap:anInteger with:i. |
|
1445 |
] |
|
1446 |
||
1447 |
" |
|
1448 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1449 |
||
1450 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1451 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1452 |
" |
|
1453 |
||
1454 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1455 |
! |
|
1456 |
||
1457 |
permutationsWith:anotherCollection do:aBlock |
|
1458 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1459 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1460 |
|
11462 | 1461 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1462 |
the identical same collection (but with different contents) every time. |
1463 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1464 |
of permuted copies. The user of this function must be careful in the block, |
|
1465 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1466 |
|
1467 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1468 |
||
1469 |
" |
|
1470 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1471 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1472 |
" |
|
1473 |
||
1474 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1475 |
! ! |
1476 |
||
602 | 1477 |
!SequenceableCollection methodsFor:'comparing'! |
1478 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1479 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1480 |
"compare two sequenceable collections" |
8874 | 1481 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1482 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1483 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1484 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1485 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1486 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1487 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1488 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1489 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1490 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1491 |
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
|
1492 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1493 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1494 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1495 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1496 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1497 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1498 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1499 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1500 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1501 |
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
|
1502 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1503 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1504 |
(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
|
1505 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1506 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1507 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1508 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1509 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1510 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1511 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1512 |
#(1 2 3) < #(1 2 3) |
8311
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 b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1515 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1516 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1517 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1518 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1519 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1520 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1521 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1522 |
] |
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 b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1525 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1526 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1527 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1528 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1529 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1530 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1531 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1532 |
] |
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 b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1535 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1536 |
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
|
1537 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1538 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1539 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1540 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1541 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1542 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1543 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1544 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1545 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1546 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1547 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1548 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1549 |
"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
|
1550 |
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
|
1551 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1552 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1553 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1554 |
|
602 | 1555 |
= aCollection |
1556 |
"return true if the receiver and aCollection represent collections |
|
12290 | 1557 |
with equal contents, and if they are of the same species." |
602 | 1558 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1559 |
| stop "{ Class: SmallInteger }" | |
602 | 1560 |
|
1561 |
(aCollection == self) ifTrue:[^true]. |
|
1562 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1563 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1564 |
|
1565 |
stop := self size. |
|
1566 |
stop == (aCollection size) ifFalse:[^false]. |
|
1567 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1568 |
1 to:stop do:[:index| |
9366 | 1569 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1570 |
]. |
1571 |
^ true |
|
1572 |
||
1573 |
" |
|
8874 | 1574 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1575 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1576 |
#($1 $2 $3 $4 $5) = '12345' |
|
1577 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1578 |
" |
1579 |
! |
|
1580 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1581 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1582 |
"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
|
1583 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1584 |
Otherwise return false." |
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 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1587 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1588 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1589 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1590 |
"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
|
1591 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1592 |
Otherwise return false." |
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 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1595 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1596 |
|
8874 | 1597 |
commonPrefixWith:aCollection |
6507 | 1598 |
"return the common prefix of myself and the argument, aCollection. |
1599 |
If there is none, an empty collection is returned." |
|
1600 |
||
1601 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1602 |
||
1603 |
" |
|
8874 | 1604 |
'hello' commonPrefixWith:'hello' |
1605 |
'hello' commonPrefixWith:'hElLo' |
|
1606 |
'hello' commonPrefixWith:'hello world' |
|
1607 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1608 |
||
1609 |
'hello world' commonPrefixWith:'hello' |
|
1610 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1611 |
||
1612 |
'abcd' commonPrefixWith:'bcde' |
|
1613 |
||
1614 |
'abcd' commonPrefixWith:'abab' |
|
1615 |
'abcd' commonPrefixWith:'ab' |
|
1616 |
'abcd' commonPrefixWith:'ababab' |
|
1617 |
'abcd' commonPrefixWith:'abcdef' |
|
1618 |
||
1619 |
'abab' commonPrefixWith:'abcd' |
|
1620 |
'ab' commonPrefixWith:'abcd' |
|
1621 |
'ababab' commonPrefixWith:'abcd' |
|
1622 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1623 |
" |
1624 |
! |
|
1625 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1626 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1627 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1628 |
If there is none, an empty collection is returned." |
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| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1631 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1632 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1633 |
1 to:matchLen do:[:idx | |
8874 | 1634 |
|elHere elThere same| |
1635 |
||
1636 |
elHere := self at:idx. |
|
1637 |
elThere := aCollection at:idx. |
|
1638 |
||
1639 |
ignoreCase ifTrue:[ |
|
1640 |
same := elHere sameAs:elThere |
|
1641 |
] ifFalse:[ |
|
1642 |
same := elHere = elThere |
|
1643 |
]. |
|
1644 |
same ifFalse:[ |
|
1645 |
^ self copyTo:(idx - 1). |
|
1646 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1647 |
]. |
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 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1650 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1651 |
" |
8874 | 1652 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1653 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1654 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1655 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1656 |
||
1657 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1658 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1659 |
||
1660 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1661 |
||
1662 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1663 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1664 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1665 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1666 |
||
1667 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1668 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1669 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1670 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1671 |
" |
|
1672 |
! |
|
1673 |
||
1674 |
commonSuffixWith:aCollection |
|
6507 | 1675 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1676 |
If there is none, an empty collection is returned." |
|
1677 |
||
1678 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1679 |
||
1680 |
" |
|
8874 | 1681 |
'hello' commonSuffixWith:'hello' |
1682 |
'hello' commonSuffixWith:'hElLo' |
|
1683 |
'hello' commonSuffixWith:'hello world' |
|
1684 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1685 |
'hello2 world' commonSuffixWith:'hello world' |
|
1686 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1687 |
||
1688 |
'hello world' commonSuffixWith:'world' |
|
1689 |
'hello WoRlD' commonSuffixWith:'world' |
|
1690 |
||
1691 |
'dcba' commonSuffixWith:'edcb' |
|
1692 |
||
1693 |
'dcba' commonSuffixWith:'baba' |
|
1694 |
'dcba' commonSuffixWith:'ba' |
|
1695 |
'dcba' commonSuffixWith:'bababa' |
|
1696 |
'dcba' commonSuffixWith:'fdcba' |
|
1697 |
||
1698 |
'baba' commonSuffixWith:'dcba' |
|
1699 |
'ba' commonSuffixWith:'dcba' |
|
1700 |
'bababa' commonSuffixWith:'dcba' |
|
1701 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1702 |
" |
1703 |
! |
|
1704 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1705 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1706 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1707 |
If there is none, an empty collection is returned." |
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 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1710 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1711 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1712 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1713 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1714 |
1 to:matchLen do:[:idx | |
8874 | 1715 |
|elHere elThere same| |
1716 |
||
1717 |
elHere := self at:(l1 - idx + 1). |
|
1718 |
elThere := aCollection at:(l2 - idx + 1). |
|
1719 |
||
1720 |
ignoreCase ifTrue:[ |
|
1721 |
same := elHere sameAs:elThere |
|
1722 |
] ifFalse:[ |
|
1723 |
same := elHere = elThere |
|
1724 |
]. |
|
1725 |
same ifFalse:[ |
|
1726 |
^ self copyFrom:(l1 - idx + 2). |
|
1727 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1728 |
]. |
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 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1731 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1732 |
" |
8874 | 1733 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1734 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1735 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1736 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1737 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1738 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1739 |
||
1740 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1741 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1742 |
||
1743 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1744 |
||
1745 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1746 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1747 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1748 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1749 |
||
1750 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1751 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1752 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1753 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1754 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1755 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1756 |
|
5813 | 1757 |
deepSameContentsAs:aCollection |
1758 |
"return true, if the receiver and the arg have the same contents |
|
1759 |
in both the named instance vars and any indexed instVars. |
|
1760 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1761 |
||
1762 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1763 |
unused space" |
|
1764 |
||
1765 |
|index "{ Class: SmallInteger }" |
|
1766 |
stop "{ Class: SmallInteger }" | |
|
1767 |
||
1768 |
(aCollection == self) ifTrue:[^true]. |
|
1769 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1770 |
||
1771 |
stop := self size. |
|
1772 |
stop == (aCollection size) ifFalse:[^false]. |
|
1773 |
||
1774 |
index := 1. |
|
1775 |
[index <= stop] whileTrue:[ |
|
8874 | 1776 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1777 |
index := index + 1 |
|
5813 | 1778 |
]. |
1779 |
^ true |
|
1780 |
||
1781 |
" |
|
8874 | 1782 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1783 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1784 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1785 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1786 |
" |
1787 |
! |
|
1788 |
||
602 | 1789 |
endsWith:aCollection |
1790 |
"return true, if the receivers last elements match those |
|
1791 |
of aCollection" |
|
1792 |
||
1793 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1794 |
index2 "{ Class: SmallInteger }" |
1795 |
stop "{ Class: SmallInteger }" |
|
602 | 1796 |
sz "{ Class: SmallInteger }"| |
1797 |
||
1798 |
(aCollection == self) ifTrue:[^ true]. |
|
1799 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1800 |
||
1801 |
stop := aCollection size. |
|
77 | 1802 |
sz := self size. |
602 | 1803 |
stop > sz ifTrue:[^false]. |
1804 |
||
1805 |
index1 := sz. |
|
1806 |
index2 := stop. |
|
1807 |
[index2 > 0] whileTrue:[ |
|
8874 | 1808 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1809 |
index1 := index1 - 1. |
|
1810 |
index2 := index2 - 1 |
|
77 | 1811 |
]. |
602 | 1812 |
^ true |
1813 |
||
1814 |
" |
|
1815 |
'abcde' endsWith:#($d $e) |
|
8874 | 1816 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1817 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1818 |
" |
|
1819 |
! |
|
1820 |
||
7278 | 1821 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1822 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1823 |
|
1824 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1825 |
||
1826 |
" |
|
1827 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1828 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1829 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1830 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1831 |
" |
1832 |
! |
|
1833 |
||
11820 | 1834 |
hasEqualElements: otherCollection |
1835 |
"Answer whether the receiver's size is the same as otherCollection's |
|
12290 | 1836 |
size, and each of the receiver's elements equal the corresponding |
1837 |
element of otherCollection. |
|
1838 |
This should probably replace the current definition of #= ." |
|
1839 |
||
1840 |
^ self isSameSequenceAs:otherCollection |
|
11820 | 1841 |
! |
1842 |
||
602 | 1843 |
hash |
1844 |
"return a hash key for the receiver" |
|
1845 |
||
1846 |
"this hash is stupid - but for larger collections, the hashing |
|
1847 |
time can become much bigger than the time lost in added probing. |
|
1848 |
Time will show ..." |
|
1849 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1850 |
|mySize h| |
3653 | 1851 |
|
1852 |
mySize := self size. |
|
1853 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1854 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1855 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1856 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1857 |
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
|
1858 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1859 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1860 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1861 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1862 |
"/ 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
|
1863 |
"/ ^ ((((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
|
1864 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1865 |
" |
8874 | 1866 |
#(1 2 3 4 5 6) hash |
1867 |
(1 to:6) hash |
|
1868 |
||
1869 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1870 |
#(1 2 3 4 5) hash |
|
602 | 1871 |
" |
3653 | 1872 |
|
1873 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1874 |
! |
1875 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1876 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1877 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1878 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1879 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1880 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1881 |
^ self |
8874 | 1882 |
sameContentsAs:aCollection |
1883 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1884 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1885 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1886 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1887 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1888 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1889 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1890 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
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 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1893 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1894 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1895 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1896 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1897 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1898 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1899 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1900 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1901 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1902 |
" |
8874 | 1903 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1904 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1905 |
#($1 $2 $3 $4 $5) = '12345' |
|
1906 |
||
1907 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1908 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1909 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1910 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1911 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1912 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1913 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1914 |
|
12289
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1915 |
isSameSequenceAs:otherCollection |
8874 | 1916 |
"Answer whether the receiver's size is the same as otherCollection's size, |
12290 | 1917 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1918 |
This should probably replace the current definition of #= ." |
|
6118 | 1919 |
|
1920 |
| size | |
|
1921 |
||
12289
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1922 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1923 |
otherCollection isSequenceable ifFalse: [^ false]. |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1924 |
|
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1925 |
1 to:size do:[:index | |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1926 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false] |
6118 | 1927 |
]. |
1928 |
^ true |
|
1929 |
! |
|
1930 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1931 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1932 |
"return true if the receiver and aCollection represent collections |
8874 | 1933 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1934 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1935 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1936 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1937 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1938 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1939 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1940 |
stop "{ Class: SmallInteger }" | |
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 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1943 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1944 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1945 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1946 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1947 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1948 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1949 |
stop == (aCollection size) ifFalse:[^ false]. |
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 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1952 |
[index <= stop] whileTrue:[ |
8874 | 1953 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1954 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1955 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1956 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1957 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1958 |
" |
8874 | 1959 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
1960 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1961 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1962 |
#(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
|
1963 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1964 |
#('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
|
1965 |
" |
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 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1968 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1969 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1970 |
|
602 | 1971 |
startsWith:aCollection |
1972 |
"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
|
1973 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1974 |
If the argument is empty, true is returned." |
602 | 1975 |
|
1976 |
|index "{ Class: SmallInteger }" |
|
1977 |
stop "{ Class: SmallInteger }" | |
|
1978 |
||
1979 |
(aCollection == self) ifTrue:[^true]. |
|
1980 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1981 |
||
1982 |
stop := aCollection size. |
|
1983 |
stop > self size ifTrue:[^false]. |
|
1984 |
||
1985 |
index := 1. |
|
1986 |
[index <= stop] whileTrue:[ |
|
8874 | 1987 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1988 |
index := index + 1 |
|
602 | 1989 |
]. |
1990 |
^ true |
|
1991 |
||
1992 |
" |
|
1993 |
'abcde' startsWith:#($a $b $c) |
|
1994 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1995 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1996 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 1997 |
" |
7279 | 1998 |
! |
1999 |
||
2000 |
startsWithAnyOf:aCollectionOfCollections |
|
2001 |
"return true, if the receiver starts with any in aCollection" |
|
2002 |
||
2003 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
2004 |
||
2005 |
" |
|
2006 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
2007 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 2008 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
2009 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 2010 |
" |
13 | 2011 |
! ! |
2012 |
||
260 | 2013 |
!SequenceableCollection methodsFor:'converting'! |
2014 |
||
11879
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2015 |
asCollectionOfSubCollectionsOfSize:pieceSize |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2016 |
"return a collection containing pieces of size pieceSite from the receiver. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2017 |
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
|
2018 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2019 |
|pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2020 |
start "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2021 |
stop "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2022 |
mySize "{ Class:SmallInteger }"| |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2023 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2024 |
pieces := OrderedCollection new. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2025 |
start := 1. stop := start + pieceSize - 1. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2026 |
mySize := self size. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2027 |
[stop <= mySize] whileTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2028 |
pieces add:(self copyFrom:start to:stop). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2029 |
start := start + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2030 |
stop := stop + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2031 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2032 |
(start <= mySize) ifTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2033 |
pieces add:(self copyFrom:start to:mySize). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2034 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2035 |
^ pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2036 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2037 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2038 |
'123123123123123123' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2039 |
'12312312312312312312' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2040 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2041 |
! |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2042 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2043 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 2044 |
"return a collection containing the subcollections (separated by anElement) |
2045 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2046 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2047 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2048 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2049 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2050 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2051 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2052 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2053 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2054 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2055 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2056 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2057 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2058 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2059 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2060 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2061 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2062 |
1 to:numberOfCols do:[:lineNr | |
8874 | 2063 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
2064 |
stopIndex == 0 ifTrue:[ |
|
2065 |
stopIndex := self size |
|
2066 |
] ifFalse: [ |
|
2067 |
stopIndex := stopIndex - 1. |
|
2068 |
]. |
|
2069 |
||
2070 |
(stopIndex < startIndex) ifTrue: [ |
|
2071 |
cols add:(myClass new:0) |
|
2072 |
] ifFalse: [ |
|
2073 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
2074 |
]. |
|
2075 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2076 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2077 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2078 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2079 |
" |
8874 | 2080 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2081 |
'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
|
2082 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2083 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2084 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2085 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2086 |
|
9234 | 2087 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
2088 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2089 |
of the receiver by anElement. |
|
2090 |
If anElement occurs multiple times in a row, |
|
2091 |
the block will be invoked with empty collections as argument. |
|
2092 |
This algorithm uses equality-compare to detect the element." |
|
2093 |
||
2094 |
|subCollection |
|
2095 |
endIndex "{ Class:SmallInteger }" |
|
2096 |
startIndex "{ Class:SmallInteger }" |
|
2097 |
stopIndex "{ Class:SmallInteger }" | |
|
2098 |
||
2099 |
startIndex := 0. |
|
2100 |
endIndex := self size. |
|
2101 |
||
2102 |
[startIndex <= endIndex] whileTrue:[ |
|
2103 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2104 |
stopIndex == 0 ifTrue:[ |
|
2105 |
stopIndex := self size |
|
2106 |
] ifFalse: [ |
|
2107 |
stopIndex := stopIndex - 1. |
|
2108 |
]. |
|
2109 |
||
2110 |
(stopIndex < startIndex) ifTrue: [ |
|
2111 |
subCollection := self species new:0 |
|
2112 |
] ifFalse: [ |
|
2113 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2114 |
]. |
|
2115 |
aBlock value:subCollection. |
|
2116 |
startIndex := stopIndex + 1 |
|
2117 |
]. |
|
2118 |
||
2119 |
" |
|
2120 |
'' |
|
2121 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2122 |
||
2123 |
'1 one' |
|
2124 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2125 |
||
2126 |
'1 one:2 two:3 three:4 four:5 five' |
|
2127 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2128 |
||
2129 |
'a::b' |
|
2130 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2131 |
||
2132 |
':' |
|
2133 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2134 |
||
2135 |
':a' |
|
2136 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2137 |
||
2138 |
'a:' |
|
2139 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2140 |
" |
|
2141 |
! |
|
2142 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2143 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2144 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2145 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2146 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2147 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2148 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2149 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2150 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2151 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2152 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2153 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2154 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2155 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2156 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2157 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2158 |
[done] whileFalse:[ |
8874 | 2159 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2160 |
stopIndex == 0 ifTrue:[ |
|
2161 |
stopIndex := self size. |
|
2162 |
done := true. |
|
2163 |
] ifFalse: [ |
|
2164 |
stopIndex := stopIndex - 1. |
|
2165 |
]. |
|
2166 |
||
2167 |
(stopIndex < startIndex) ifTrue: [ |
|
2168 |
items add:(myClass new:0) |
|
2169 |
] ifFalse: [ |
|
2170 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2171 |
]. |
|
2172 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2173 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2174 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2175 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2176 |
" |
8874 | 2177 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2178 |
#(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
|
2179 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2180 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2181 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2182 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2183 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2184 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2185 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2186 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2187 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2188 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2189 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2190 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2191 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2192 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2193 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2194 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2195 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2196 |
#(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
|
2197 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2198 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2199 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2200 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2201 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2202 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2203 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2204 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2205 |
|words |
8874 | 2206 |
start "{ Class:SmallInteger }" |
2207 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2208 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2209 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2210 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2211 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2212 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2213 |
[start <= mySize] whileTrue:[ |
8874 | 2214 |
"skip multiple separators" |
2215 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2216 |
start := start + 1 . |
|
2217 |
start > mySize ifTrue:[ |
|
2218 |
^ words |
|
2219 |
]. |
|
2220 |
]. |
|
2221 |
||
2222 |
stop := self findFirst:aBlock startingAt:start. |
|
2223 |
stop == 0 ifTrue:[ |
|
2224 |
words add:(self copyFrom:start to:mySize). |
|
2225 |
^ words |
|
2226 |
]. |
|
2227 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2228 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2229 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2230 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2231 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2232 |
" |
8874 | 2233 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2234 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2235 |
#(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
|
2236 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2237 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2238 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2239 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2240 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2241 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2242 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2243 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2244 |
|
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 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2247 |
|
1413 | 2248 |
asStringCollection |
2249 |
"return a new string collection containing the elements; |
|
2250 |
these ought to be strings. (i.e. String or Text instances)" |
|
2251 |
||
2252 |
|newColl sz| |
|
2253 |
||
2254 |
sz := self size. |
|
9162 | 2255 |
newColl := StringCollection withSize:sz. |
1413 | 2256 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2257 |
^ newColl |
|
2258 |
||
2259 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2260 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2261 |
! |
|
2262 |
||
1188 | 2263 |
asStringWith:sepChar |
8874 | 2264 |
"return a string generated by concatenating my elements |
1188 | 2265 |
(which must be strings or nil) and embedding sepChar characters in between. |
2266 |
Nil entries and empty strings are counted as empty lines." |
|
2267 |
||
8874 | 2268 |
^ self |
2269 |
from:1 to:(self size) |
|
2270 |
asStringWith:sepChar |
|
2271 |
compressTabs:false |
|
2272 |
final:nil |
|
1413 | 2273 |
|
2274 |
" |
|
2275 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
2276 |
" |
|
2277 |
||
2278 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 2279 |
! |
2280 |
||
2281 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2282 |
"return part of myself as a string with embedded sepCharacters. |
|
2283 |
My elements must be strings or nil; nil entries and empty strings are |
|
2284 |
taken as empty lines." |
|
2285 |
||
8874 | 2286 |
^ self |
2287 |
from:firstLine to:lastLine |
|
2288 |
asStringWith:sepCharacter |
|
2289 |
compressTabs:false |
|
2290 |
final:nil |
|
1188 | 2291 |
" |
2292 |
creating entries for searchpath: |
|
2293 |
||
8874 | 2294 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2295 |
||
2296 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2297 |
" |
2298 |
||
2299 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2300 |
! |
|
2301 |
||
260 | 2302 |
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
|
2303 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2304 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2305 |
taken as empty lines. |
2306 |
If the argument compressTabs is true, leading spaces are converted |
|
2307 |
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
|
2308 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2309 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2310 |
^ self |
8874 | 2311 |
from:firstLine to:lastLine |
2312 |
asStringWith:sepCharacter |
|
2313 |
compressTabs:compressTabs |
|
2314 |
final:endCharacter |
|
2315 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2316 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2317 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2318 |
! |
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 |
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
|
2321 |
"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
|
2322 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2323 |
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
|
2324 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2325 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2326 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2327 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2328 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2329 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2330 |
any emphasis. If false, a plain string is returned. |
260 | 2331 |
This method is tuned for big collections, in not creating many |
263 | 2332 |
intermediate strings (has linear runtime). For very small collections |
2333 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2334 |
|
5575 | 2335 |
^ self |
8874 | 2336 |
from:firstLine to:lastLine |
2337 |
asStringWith:sepCharacter |
|
2338 |
compressTabs:compressTabs |
|
2339 |
final:endCharacter |
|
2340 |
withEmphasis:withEmphasis |
|
5575 | 2341 |
! |
2342 |
||
2343 |
asStringWithCRs |
|
8874 | 2344 |
"return a string generated by concatenating my elements |
5575 | 2345 |
(which must be strings or nil) and embedding cr characters in between. |
2346 |
Nil entries and empty strings are counted as empty lines." |
|
2347 |
||
2348 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2349 |
||
2350 |
" |
|
8874 | 2351 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2352 |
|
2353 |
(OrderedCollection new |
|
8874 | 2354 |
add:'hello'; |
2355 |
add:'world'; |
|
2356 |
add:'foo'; |
|
2357 |
add:('bar' asText allBold); |
|
2358 |
yourself) asStringWithCRs |
|
5575 | 2359 |
|
2360 |
Transcript showCR: |
|
8874 | 2361 |
(OrderedCollection new |
2362 |
add:'hello'; |
|
2363 |
add:'world'; |
|
2364 |
add:'foo'; |
|
2365 |
add:('bar' asText allBold); |
|
2366 |
yourself) asStringWithCRs |
|
5575 | 2367 |
" |
2368 |
||
2369 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2370 |
! |
|
2371 |
||
2372 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2373 |
"return a string generated by concatenating some of my elements |
5575 | 2374 |
(which must be strings or nil) and embedding cr characters in between. |
2375 |
Nil entries and empty strings are counted as empty lines." |
|
2376 |
||
2377 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2378 |
||
2379 |
" |
|
8874 | 2380 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2381 |
|
2382 |
" |
|
2383 |
||
2384 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2385 |
! |
|
2386 |
||
2387 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2388 |
"return part of myself as a string with embedded cr's. |
|
2389 |
My elements must be strings or nil. |
|
2390 |
If the argument compressTabs is true, leading spaces are converted |
|
2391 |
to tab-characters (8col tabs). |
|
2392 |
Nil entries and empty strings are taken as empty lines." |
|
2393 |
||
2394 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2395 |
! |
|
2396 |
||
2397 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2398 |
"return part of myself as a string with embedded cr's. |
|
2399 |
My elements must be strings or nil; nil entries and empty strings are |
|
2400 |
taken as empty lines. |
|
2401 |
If the argument compressTabs is true, leading spaces are converted |
|
2402 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2403 |
should be followed by a cr or not." |
|
2404 |
||
2405 |
^ self asStringWith:(Character cr) |
|
8874 | 2406 |
from:firstLine |
2407 |
to:lastLine |
|
2408 |
compressTabs:compressTabs |
|
2409 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2410 |
! |
2411 |
||
2412 |
decodeAsLiteralArray |
|
2413 |
"given a literalEncoding in the receiver, |
|
2414 |
create & return the corresponding object. |
|
2415 |
The inverse operation to #literalArrayEncoding." |
|
2416 |
||
9414 | 2417 |
|clsName cls| |
5575 | 2418 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2419 |
self size == 0 ifTrue:[ |
9358 | 2420 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2421 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2422 |
|
9414 | 2423 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2424 |
clsName isSymbol ifFalse:[ |
9358 | 2425 |
^ MissingClassInLiteralArrayErrorSignal |
2426 |
raiseRequestWith:clsName |
|
9414 | 2427 |
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
|
2428 |
]. |
9414 | 2429 |
cls := Smalltalk classNamed:clsName. |
5575 | 2430 |
cls isNil ifTrue:[ |
9414 | 2431 |
^ MissingClassInLiteralArrayErrorSignal |
2432 |
raiseRequestWith:clsName |
|
2433 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2434 |
]. |
9414 | 2435 |
|
5575 | 2436 |
^ cls decodeFromLiteralArray:self. |
2437 |
||
2438 |
" |
|
10476 | 2439 |
#(Array 10 20) decodeAsLiteralArray |
2440 |
#(10 20) literalArrayEncoding |
|
2441 |
" |
|
2442 |
||
2443 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2444 |
! |
2445 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2446 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2447 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2448 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2449 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2450 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2451 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2452 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2453 |
#( |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2454 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2455 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2456 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2457 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2458 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2459 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2460 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2461 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2462 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2463 |
|
8874 | 2464 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2465 |
"return part of myself as a string with embedded sepCharacters. |
2466 |
My elements must be strings or nil; nil entries and empty strings are |
|
2467 |
taken as empty lines." |
|
2468 |
||
8874 | 2469 |
^ self |
2470 |
from:firstLine |
|
2471 |
to:lastLine |
|
2472 |
asStringWith:sepCharacter |
|
2473 |
compressTabs:false |
|
2474 |
final:nil |
|
5575 | 2475 |
" |
2476 |
creating entries for searchpath: |
|
2477 |
||
8874 | 2478 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2479 |
||
2480 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2481 |
||
2482 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2483 |
" |
2484 |
||
2485 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2486 |
! |
|
2487 |
||
2488 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2489 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2490 |
My elements must be strings or nil; nil entries and empty strings are |
|
2491 |
taken as empty lines. |
|
2492 |
If the argument compressTabs is true, leading spaces are converted |
|
2493 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2494 |
character (if non-nil)." |
|
2495 |
||
2496 |
^ self |
|
8874 | 2497 |
from:firstLine |
2498 |
to:lastLine |
|
2499 |
asStringWith:sepCharacter |
|
2500 |
compressTabs:compressTabs |
|
2501 |
final:endCharacter |
|
2502 |
withEmphasis:true |
|
5575 | 2503 |
|
2504 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2505 |
! |
|
2506 |
||
2507 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2508 |
"return part of myself as a string or text with embedded sepCharacters |
|
2509 |
and followup endCharacter. |
|
2510 |
My elements must be strings or nil; nil entries and empty strings are |
|
2511 |
taken as empty lines. |
|
2512 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2513 |
If the argument compressTabs is true, leading spaces are converted |
|
2514 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2515 |
character (if non-nil). |
|
2516 |
The withEmphais argument controls if the returned string should preserve |
|
2517 |
any emphasis. If false, a plain string is returned. |
|
2518 |
This method is tuned for big collections, in not creating many |
|
2519 |
intermediate strings (has linear runtime). For very small collections |
|
2520 |
and small strings, it may be faster to use the comma , operation." |
|
2521 |
||
293 | 2522 |
|idx1 "{ Class:SmallInteger }" |
2523 |
idx2 "{ Class:SmallInteger }" |
|
2524 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2525 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2526 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2527 |
newString lineString spaces idx nTabs |
8099 | 2528 |
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
|
2529 |
thisLen anyTab| |
260 | 2530 |
|
2531 |
" |
|
2532 |
first accumulate the size of the string, to avoid |
|
2533 |
countless reallocations. If tabs are compressed, |
|
2534 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2535 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2536 |
" |
8099 | 2537 |
needEmphasis := false. |
2538 |
maxBitsPerCharacter := 8. |
|
1015 | 2539 |
|
260 | 2540 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2541 |
sepCharacter isNil ifTrue:[ |
8874 | 2542 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2543 |
] ifFalse:[ |
8874 | 2544 |
sepCharacter isCharacter ifTrue:[ |
2545 |
sepCnt := 1 |
|
2546 |
] ifFalse:[ |
|
2547 |
sepCnt := sepCharacter size |
|
2548 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2549 |
]. |
260 | 2550 |
|
293 | 2551 |
idx1 := firstLine. |
2552 |
idx2 := lastLine. |
|
2553 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2554 |
lineString := self at:lineIndex. |
2555 |
||
2556 |
lineString notNil ifTrue:[ |
|
2557 |
withEmphasis ifTrue:[ |
|
2558 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2559 |
needEmphasis := true |
|
2560 |
]. |
|
2561 |
]. |
|
2562 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2563 |
totalLength := totalLength + lineString size |
|
2564 |
]. |
|
2565 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2566 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2567 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2568 |
|
8099 | 2569 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2570 |
maxBitsPerCharacter > 16 ifTrue:[ |
2571 |
stringClass := Unicode32String. |
|
2572 |
] ifFalse:[ |
|
2573 |
stringClass := Unicode16String. |
|
2574 |
] |
|
8099 | 2575 |
] ifFalse:[ |
8874 | 2576 |
stringClass := String. |
8099 | 2577 |
]. |
2578 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2579 |
endCharacter notNil ifTrue:[ |
8874 | 2580 |
endCharacter isCharacter ifTrue:[ |
2581 |
totalLength := totalLength + 1 |
|
2582 |
] ifFalse:[ |
|
2583 |
totalLength := totalLength + endCharacter size |
|
2584 |
]. |
|
260 | 2585 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2586 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2587 |
|
260 | 2588 |
spaces := ' '. |
1016 | 2589 |
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
|
2590 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2591 |
needEmphasis ifTrue:[ |
8874 | 2592 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2593 |
]. |
260 | 2594 |
|
2595 |
" |
|
2596 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2597 |
Be careful with runArrays: |
8874 | 2598 |
replacing individual elements is VERY expensive. |
2599 |
Therefore, create a new runArray from scratch. |
|
260 | 2600 |
" |
2601 |
pos := 1. |
|
293 | 2602 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2603 |
lineString := self at:lineIndex. |
2604 |
thisLen := lineString size. |
|
2605 |
thisLen ~~ 0 ifTrue:[ |
|
2606 |
withEmphasis ifFalse:[ |
|
2607 |
lineString := lineString string. |
|
2608 |
]. |
|
2609 |
||
2610 |
(anyTab := compressTabs) ifTrue:[ |
|
2611 |
" |
|
2612 |
mhmh: could use withTabs from String-class here, |
|
2613 |
but we should avoid creating too many temporary strings |
|
2614 |
(especially, since this method is typically used when converting |
|
2615 |
big texts such as when saving in the filebrowser ...). |
|
2616 |
Therefore, we convert tabs inline here doing a direct replace |
|
2617 |
in newString." |
|
2618 |
||
2619 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2620 |
nTabs := (idx-1) // 8. |
|
2621 |
anyTab := (nTabs > 0) |
|
2622 |
]. |
|
2623 |
anyTab ifTrue:[ |
|
2624 |
"any tabs" |
|
2625 |
||
2626 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2627 |
||
2628 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2629 |
newRuns notNil ifTrue:[ |
|
2630 |
newRuns add:nil withOccurrences:nTabs |
|
2631 |
]. |
|
2632 |
pos := pos + nTabs. |
|
2633 |
||
2634 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2635 |
newRuns notNil ifTrue:[ |
|
2636 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2637 |
idx to:lineString size do:[:pos | |
|
2638 |
newRuns add:(lineString emphasisAt:pos) |
|
2639 |
] |
|
2640 |
] ifFalse:[ |
|
2641 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2642 |
] |
|
2643 |
]. |
|
2644 |
pos := pos + thisLen - (nTabs * 8). |
|
2645 |
] ifFalse:[ |
|
2646 |
newString replaceFrom:pos with:lineString. |
|
2647 |
||
2648 |
newRuns notNil ifTrue:[ |
|
2649 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2650 |
newRuns addAll:(lineString emphasis) |
|
2651 |
] ifFalse:[ |
|
2652 |
newRuns add:nil withOccurrences:lineString size |
|
2653 |
] |
|
2654 |
]. |
|
2655 |
pos := pos + thisLen. |
|
2656 |
]. |
|
2657 |
]. |
|
2658 |
||
2659 |
lineIndex ~~ lastLine ifTrue:[ |
|
2660 |
c := sepCharacter |
|
2661 |
] ifFalse:[ |
|
2662 |
c := endCharacter |
|
2663 |
]. |
|
2664 |
||
2665 |
c notNil ifTrue:[ |
|
2666 |
c isCharacter ifTrue:[ |
|
2667 |
newString at:pos put:c. |
|
2668 |
newRuns notNil ifTrue:[ |
|
2669 |
newRuns add:nil. |
|
2670 |
]. |
|
2671 |
pos := pos + 1 |
|
2672 |
] ifFalse:[ |
|
2673 |
newString replaceFrom:pos with:c. |
|
2674 |
newRuns notNil ifTrue:[ |
|
2675 |
newRuns add:nil withOccurrences:c size |
|
2676 |
]. |
|
2677 |
pos := pos + c size |
|
2678 |
] |
|
2679 |
]. |
|
260 | 2680 |
]. |
2681 |
||
2682 |
" |
|
2683 |
in case of tab compression, the result has to be |
|
2684 |
cut to size ... sorry |
|
2685 |
" |
|
2686 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2687 |
newString := newString copyTo:(pos - 1) |
260 | 2688 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2689 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2690 |
newRuns notNil ifTrue:[ |
8874 | 2691 |
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
|
2692 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2693 |
|
260 | 2694 |
^ newString |
2695 |
||
2696 |
" |
|
2697 |
creating entries for searchpath: |
|
2698 |
||
8874 | 2699 |
#('foo' 'bar' 'baz' '/foo/bar') |
2700 |
asStringWith:$: |
|
2701 |
from:1 to:4 |
|
2702 |
compressTabs:false |
|
2703 |
final:nil |
|
260 | 2704 |
|
2705 |
with trailing colon: |
|
2706 |
||
8874 | 2707 |
#('foo' 'bar' 'baz' '/foo/bar') |
2708 |
asStringWith:$: |
|
2709 |
from:1 to:4 |
|
2710 |
compressTabs:false |
|
2711 |
final:$: |
|
260 | 2712 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2713 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2714 |
|
8874 | 2715 |
#('foo' 'bar' 'baz') |
2716 |
asStringWith:nil |
|
2717 |
from:1 to:3 |
|
2718 |
compressTabs:false |
|
2719 |
final:nil |
|
260 | 2720 |
|
2721 |
creating a string from a collection of lines: |
|
2722 |
||
8874 | 2723 |
#('foo' 'bar' 'baz') |
2724 |
asStringWith:(Character cr) |
|
2725 |
from:1 to:3 |
|
2726 |
compressTabs:false |
|
2727 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2728 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2729 |
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
|
2730 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2731 |
(Array |
8874 | 2732 |
with:'foo' |
2733 |
with:('bar' asText allBold) |
|
2734 |
with:'baz' |
|
2735 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2736 |
) |
|
2737 |
asStringWith:(Character cr) |
|
2738 |
from:1 to:4 |
|
2739 |
compressTabs:false |
|
2740 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2741 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2742 |
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
|
2743 |
|
8874 | 2744 |
#('foo' 'bar' 'baz') |
2745 |
asStringWith:'__' |
|
2746 |
from:1 to:3 |
|
2747 |
compressTabs:false |
|
2748 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2749 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2750 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2751 |
|
8874 | 2752 |
#('foo' 'bar' 'baz') |
2753 |
asStringWith:'__' |
|
2754 |
from:1 to:3 |
|
2755 |
compressTabs:false |
|
2756 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2757 |
|
260 | 2758 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2759 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2760 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2761 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
11827 | 2762 |
! |
2763 |
||
2764 |
subCollections:aBlock |
|
2765 |
"Answser an ordered collection of ordered collections |
|
2766 |
where each subcollection is delimited by an element of the receiver |
|
2767 |
for which the given block evaluates to true." |
|
2768 |
||
2769 |
|str answer current e| |
|
2770 |
||
2771 |
str := self readStream. |
|
2772 |
answer := OrderedCollection new. |
|
2773 |
current := OrderedCollection new. |
|
2774 |
[ str atEnd ] whileFalse:[ |
|
2775 |
e := str next. |
|
2776 |
current add:e. |
|
2777 |
(aBlock value:e) ifTrue:[ |
|
2778 |
answer add:current. |
|
2779 |
current := OrderedCollection new |
|
2780 |
] |
|
2781 |
]. |
|
2782 |
current notEmpty ifTrue:[ |
|
2783 |
answer add:current |
|
2784 |
]. |
|
2785 |
^ answer |
|
2786 |
||
2787 |
" |
|
2788 |
#( 1 2 3 nil 4 5 6 nil 7 8 9 nil ) subCollections:[:el | el isNil]. |
|
2789 |
" |
|
260 | 2790 |
! ! |
2791 |
||
7253 | 2792 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2793 |
||
8874 | 2794 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2795 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2796 |
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
|
2797 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2798 |
however, physically, no copy is made). |
9291 | 2799 |
Warning: |
2800 |
The slice SHARES the memory for the element-data with the original, |
|
2801 |
this means that any modifications in the original are visible in the slice |
|
2802 |
and vice versa." |
|
7253 | 2803 |
|
2804 |
^ self |
|
9291 | 2805 |
from:startIndex |
2806 |
to:self size |
|
2807 |
by:1 |
|
8874 | 2808 |
|
2809 |
" |
|
2810 |
#(1 2 3 4 5 6 7) from:3 |
|
2811 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2812 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2813 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2814 |
" |
|
2815 |
! |
|
2816 |
||
2817 |
from:startIndex by:step |
|
9291 | 2818 |
"Create a ReindexedCollection from the receiver. |
2819 |
The new collection represents the elements starting at startIndex to the end. |
|
2820 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2821 |
however, physically, no copy is made). |
|
2822 |
Warning: |
|
2823 |
The slice SHARES the memory for the element-data with the original, |
|
2824 |
this means that any modifications in the original are visible in the slice |
|
2825 |
and vice versa." |
|
7253 | 2826 |
|
2827 |
^ self |
|
9291 | 2828 |
from:startIndex |
2829 |
to:(step > 1 |
|
2830 |
ifTrue: [self size] |
|
2831 |
ifFalse: [1]) |
|
2832 |
by:step |
|
8874 | 2833 |
|
2834 |
" |
|
2835 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2836 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2837 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2838 |
( #(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
|
2839 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2840 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2841 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2842 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2843 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2844 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2845 |
Warning: |
2846 |
The slice SHARES the memory for the element-data with the original, |
|
2847 |
this means that any modifications in the original are visible in the slice |
|
2848 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2849 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2850 |
^ self |
9291 | 2851 |
from:startIndex |
2852 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2853 |
|
2854 |
" |
|
2855 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2856 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2857 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2858 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2859 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2860 |
|coll slice| |
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 |
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
|
2863 |
slice := coll from:3 to:7. |
8874 | 2864 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2865 |
coll at:4 put:40. |
8874 | 2866 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2867 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2868 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2869 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2870 |
|coll slice| |
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 |
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
|
2873 |
slice := coll from:3 to:7. |
8874 | 2874 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2875 |
slice at:1 put:40. |
8874 | 2876 |
slice. |
2877 |
" |
|
2878 |
! |
|
2879 |
||
2880 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2881 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2882 |
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
|
2883 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2884 |
however, physically, no copy is made). |
9291 | 2885 |
Warning: |
2886 |
The slice SHARES the memory for the element-data with the original, |
|
2887 |
this means that any modifications in the original are visible in the slice |
|
2888 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2889 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2890 |
^ self |
9291 | 2891 |
from:startIndex |
2892 |
to:endIndex |
|
2893 |
by: (startIndex <= endIndex |
|
2894 |
ifTrue: [1] |
|
2895 |
ifFalse: [-1]) |
|
8874 | 2896 |
|
2897 |
" |
|
2898 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2899 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2900 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2901 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2902 |
" |
|
2903 |
! |
|
2904 |
||
2905 |
from:startIndex to:endIndex by:step |
|
9291 | 2906 |
"Create a ReindexedCollection from the receiver. |
2907 |
The new collection represents the receivers elements up to endIndex. |
|
2908 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2909 |
however, physically, no copy is made). |
|
2910 |
Warning: |
|
2911 |
The slice SHARES the memory for the element-data with the original, |
|
2912 |
this means that any modifications in the original are visible in the slice |
|
2913 |
and vice versa." |
|
7253 | 2914 |
|
2915 |
^ ReindexedCollection |
|
9291 | 2916 |
on:self |
2917 |
from:startIndex |
|
2918 |
to:endIndex |
|
2919 |
by:step |
|
8874 | 2920 |
|
2921 |
" |
|
2922 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2923 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2924 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2925 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2926 |
" |
|
2927 |
! |
|
2928 |
||
2929 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2930 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2931 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2932 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2933 |
however, physically, no copy is made). |
9291 | 2934 |
Warning: |
2935 |
The slice SHARES the memory for the element-data with the original, |
|
2936 |
this means that any modifications in the original are visible in the slice |
|
2937 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2938 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2939 |
^ self |
9291 | 2940 |
from:1 |
2941 |
to:endIndex |
|
2942 |
by:1 |
|
8874 | 2943 |
|
2944 |
" |
|
2945 |
#(1 2 3 4 5 6 7) to:4 |
|
2946 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2947 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2948 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2949 |
" |
|
2950 |
! |
|
2951 |
||
2952 |
to:endIndex by:step |
|
9291 | 2953 |
"Create a ReindexedCollection from the receiver. |
2954 |
The new collection represents the receivers elements up to endIndex. |
|
2955 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2956 |
however, physically, no copy is made). |
|
2957 |
Warning: |
|
2958 |
The slice SHARES the memory for the element-data with the original, |
|
2959 |
this means that any modifications in the original are visible in the slice |
|
2960 |
and vice versa." |
|
7253 | 2961 |
|
2962 |
^ self |
|
9291 | 2963 |
from:(step > 0 |
2964 |
ifTrue: [1] |
|
2965 |
ifFalse: [self size]) |
|
2966 |
to:endIndex |
|
2967 |
by:step |
|
8874 | 2968 |
|
2969 |
" |
|
2970 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2971 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2972 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2973 |
( #(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
|
2974 |
" |
7253 | 2975 |
! ! |
2976 |
||
1 | 2977 |
!SequenceableCollection methodsFor:'copying'! |
2978 |
||
9106 | 2979 |
++ aCollection |
2980 |
"return a new collection formed from concatenating the receiver with |
|
2981 |
the argument. The class of the new collection is determined by the |
|
2982 |
receivers class, so mixing classes is possible, if the second collections |
|
2983 |
elements can be stored into instances of the receivers class." |
|
2984 |
||
2985 |
^ self , aCollection |
|
2986 |
||
2987 |
" |
|
2988 |
#($a $b $c) ++ #(1 2 3) |
|
2989 |
#($a $b $c) ++ '123' |
|
2990 |
'abc' ++ '123' |
|
2991 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2992 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2993 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2994 |
" |
|
2995 |
! |
|
2996 |
||
1 | 2997 |
, aCollection |
2998 |
"return a new collection formed from concatenating the receiver with |
|
44 | 2999 |
the argument. The class of the new collection is determined by the |
3000 |
receivers class, so mixing classes is possible, if the second collections |
|
3001 |
elements can be stored into instances of the receivers class." |
|
1 | 3002 |
|
8874 | 3003 |
|newCollection |
1 | 3004 |
mySize "{ Class: SmallInteger }" |
3005 |
newSize "{ Class: SmallInteger }" |
|
3006 |
otherSize "{ Class: SmallInteger }" |
|
3007 |
dstIndex "{ Class: SmallInteger }"| |
|
3008 |
||
3009 |
mySize := self size. |
|
3010 |
otherSize := aCollection size. |
|
3011 |
newSize := mySize + otherSize. |
|
202 | 3012 |
|
252 | 3013 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 3014 |
|
3015 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
3016 |
dstIndex := mySize + 1. |
|
202 | 3017 |
aCollection isSequenceable ifTrue:[ |
8874 | 3018 |
" |
3019 |
yes, aCollection has indexed elements |
|
3020 |
" |
|
3021 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 3022 |
] ifFalse:[ |
8874 | 3023 |
" |
3024 |
no, enumerate aCollection |
|
3025 |
" |
|
3026 |
aCollection do:[:element | |
|
3027 |
newCollection at:dstIndex put:element. |
|
3028 |
dstIndex := dstIndex + 1 |
|
3029 |
] |
|
1 | 3030 |
]. |
3031 |
^ newCollection |
|
44 | 3032 |
|
95 | 3033 |
" |
3034 |
#($a $b $c) , #(1 2 3) |
|
3035 |
#($a $b $c) , '123' |
|
3036 |
'abc' , '123' |
|
3037 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
3038 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3039 |
'abc' asArray , #(1 2 3 4 5) |
|
3040 |
" |
|
1 | 3041 |
! |
3042 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3043 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3044 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3045 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3046 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3047 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3048 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3049 |
" |
8874 | 3050 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3051 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3052 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3053 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3054 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3055 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3056 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3057 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3058 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3059 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3060 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3061 |
" |
8874 | 3062 |
#($a $b $c $d $e $f $g) copyButLast:2 |
3063 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3064 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3065 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3066 |
|
602 | 3067 |
copyFirst:count |
3068 |
"return a new collection consisting of the receivers first count |
|
3069 |
elements - this is just a rename of copyTo: - for compatibility." |
|
3070 |
||
3071 |
^ self copyFrom:1 to:count |
|
3072 |
||
3073 |
" |
|
3074 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
3075 |
'1234567890' copyFirst:4 |
|
3076 |
" |
|
3077 |
! |
|
3078 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3079 |
copyFrom:startIndex |
9234 | 3080 |
"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
|
3081 |
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
|
3082 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3083 |
^ self copyFrom:startIndex to:(self size) |
602 | 3084 |
|
3085 |
" |
|
3086 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
3087 |
'1234567890' copyFrom:2 |
|
8874 | 3088 |
(10 to:19) copyFrom:5 |
602 | 3089 |
" |
3090 |
! |
|
3091 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3092 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3093 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3094 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3095 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3096 |
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
|
3097 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3098 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3099 |
^ self |
8874 | 3100 |
copyFrom:startIndex |
3101 |
to:(startIndex + numberOfElements - 1) |
|
3102 |
||
3103 |
" |
|
3104 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3105 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3106 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3107 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3108 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
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 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3111 |
copyFrom:startIndex to:stopIndex |
602 | 3112 |
"return a new collection consisting of receivers elements |
4403 | 3113 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3114 |
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
|
3115 |
Raise an error, if stopIndex is out of range." |
602 | 3116 |
|
3117 |
|newCollection newSize| |
|
3118 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3119 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3120 |
newSize < 0 ifTrue:[ |
8874 | 3121 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3122 |
]. |
602 | 3123 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3124 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 3125 |
^ newCollection |
3126 |
||
3127 |
" |
|
3128 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3129 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3130 |
(1 to:10) copyFrom:2 to:5 |
3131 |
||
3132 |
'1234567890' copyFrom:2 to:15 |
|
3133 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 3134 |
" |
3135 |
! |
|
3136 |
||
3137 |
copyLast:count |
|
3138 |
"return a new collection consisting of the receivers last count |
|
3139 |
elements." |
|
3140 |
||
3141 |
|sz| |
|
3142 |
||
3143 |
sz := self size. |
|
3144 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3145 |
||
3146 |
" |
|
3147 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3148 |
'1234567890' copyLast:4 |
|
3149 |
" |
|
3150 |
! |
|
3151 |
||
11841 | 3152 |
copyMapping:map |
3153 |
"return a copy, where elements all replaced via the given mapping. |
|
3154 |
If the element is present as key in map, it is replaced by a corresponding value from the map. |
|
3155 |
Otherwise, it is left unchanged." |
|
3156 |
||
3157 |
|s| |
|
3158 |
||
3159 |
s := WriteStream on:self species new. |
|
3160 |
self do:[:el | |
|
3161 |
s nextPut:(map at:el ifAbsent:el) |
|
3162 |
]. |
|
3163 |
^ s contents |
|
3164 |
||
3165 |
" |
|
3166 |
|map| |
|
3167 |
||
3168 |
map := Dictionary new |
|
3169 |
at:1 put:'one'; |
|
3170 |
at:2 put:'two'; |
|
3171 |
yourself. |
|
3172 |
||
3173 |
#(1 2 3 4 1 2 3 4) copyMapping:map |
|
3174 |
" |
|
3175 |
! |
|
3176 |
||
3177 |
copyMappingFrom:inMap to:outMap |
|
3178 |
"return a copy, where elements all replaced via the given mapping. |
|
3179 |
If the element is present in inmap, it is replaced by a corresponding value from outmap. |
|
3180 |
Otherwise, it is left unchanged." |
|
3181 |
||
3182 |
|d| |
|
3183 |
||
3184 |
d := Dictionary new. |
|
3185 |
inMap keysAndValuesDo:[:k :i | d at:i put:(outMap at:k)]. |
|
3186 |
^ self copyMapping:d |
|
3187 |
||
3188 |
" |
|
3189 |
#(1 2 3 4 1 2 3 4) copyMappingFrom:#(1 2) to:#('one' 'two') |
|
3190 |
'hello' copyMappingFrom:'eo' to:'**' |
|
3191 |
" |
|
3192 |
! |
|
3193 |
||
3653 | 3194 |
copyReplaceAll:oldElement with:newElement |
3195 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3196 |
have been replaced by newElement." |
|
3197 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3198 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3199 |
^ self copy replaceAll:oldElement with:newElement |
3200 |
||
3201 |
" |
|
8874 | 3202 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3203 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3204 |
" |
3205 |
||
3206 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3207 |
! |
|
3208 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3209 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3210 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3211 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3212 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3213 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3214 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3215 |
|
5489 | 3216 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3217 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3218 |
"/ '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
|
3219 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3220 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3221 |
self do:[:el | |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3222 |
el = oldObject ifTrue:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3223 |
s nextPutAll:aCollection |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3224 |
] ifFalse:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3225 |
s nextPut:el |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3226 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3227 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3228 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3229 |
|
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 |
args: oldObject : <object> |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3232 |
newObject : <collection> |
3992
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 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3235 |
" |
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 |
" |
8874 | 3238 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3239 |
#(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
|
3240 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3241 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3242 |
|
602 | 3243 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3244 |
"return a copy of the receiver, where the elements from startIndex to |
|
3245 |
endIndex have been replaced by the elements of aCollection" |
|
3246 |
||
3247 |
|newColl sz replSize| |
|
3248 |
||
3249 |
replSize := aCollection size. |
|
3250 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
3251 |
newColl := self copyEmptyAndGrow:sz. |
|
3252 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3253 |
newColl replaceFrom:startIndex with:aCollection. |
|
3254 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3255 |
^ newColl |
|
3256 |
||
3257 |
" |
|
3258 |
#(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 | 3259 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 3260 |
" |
3261 |
! |
|
3262 |
||
3263 |
copyReplaceFrom:startIndex with:aCollection |
|
3264 |
"return a copy of the receiver, where the elements from startIndex to |
|
3265 |
the end have been replaced by the elements of aCollection" |
|
3266 |
||
3267 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
3268 |
||
3269 |
" |
|
8874 | 3270 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
3271 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 3272 |
" |
3273 |
! |
|
3274 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3275 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3276 |
"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
|
3277 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3278 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3279 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3280 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3281 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3282 |
" |
8874 | 3283 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
3284 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3285 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3286 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3287 |
|
602 | 3288 |
copyThrough:element |
3289 |
"return a new collection consisting of the receiver elements |
|
8874 | 3290 |
up-to (AND including) the first occurrence of element." |
602 | 3291 |
|
3292 |
|idx| |
|
3293 |
||
3294 |
idx := self indexOf:element. |
|
3295 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
3296 |
^ self copyFrom:1 to:idx |
|
3297 |
||
3298 |
" |
|
3299 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
3300 |
'1234567890' copyThrough:$5 |
|
3301 |
'1234567890' copyThrough:$a |
|
3302 |
'1234567890' copyThrough:$1 |
|
3303 |
" |
|
3304 |
! |
|
3305 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3306 |
copyTo:stopIndex |
9234 | 3307 |
"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
|
3308 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3309 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3310 |
^ self copyFrom:1 to:stopIndex |
602 | 3311 |
|
3312 |
" |
|
3313 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
3314 |
'1234567890' copyTo:4 |
|
3315 |
" |
|
3316 |
! |
|
3317 |
||
5526 | 3318 |
copyToMax:stop |
3319 |
"return a new collection consisting of receivers elements |
|
8874 | 3320 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 3321 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
3322 |
||
3323 |
^ self copyFrom:1 to:(self size min:stop) |
|
3324 |
||
3325 |
" |
|
3326 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
3327 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
3328 |
" |
|
3329 |
! |
|
3330 |
||
11844 | 3331 |
copyTransliterating:oldElements to:newElements |
3332 |
"return a copy, where elements all transliterated via the given mapping. |
|
3333 |
The transiteraion works like perl's tr operator. |
|
3334 |
||
3335 |
Transliterates all occurrences of the characters found in the search list with the |
|
11880 | 3336 |
corresponding character in the replacement list. |
11844 | 3337 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
3338 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3339 |
||
3340 |
Note that tr does not do other character escapes such as \n. |
|
3341 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3342 |
||
3343 |
Note also that the whole range idea is rather unportable between character sets, |
|
3344 |
and even within character sets they may cause results you probably didn't expect. |
|
3345 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3346 |
case (a-e, A-E), or digits (0-4). |
|
3347 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3348 |
" |
|
3349 |
||
3350 |
^ self |
|
3351 |
copyTransliterating:oldElements to:newElements |
|
3352 |
complement:false squashDuplicates:false |
|
3353 |
||
3354 |
" |
|
3355 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
|
3356 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3357 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3358 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3359 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11880 | 3360 |
|
3361 |
can also be used to remove character: |
|
3362 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'' |
|
3363 |
||
3364 |
'abcdefg |
|
3365 |
hijkl1 |
|
3366 |
234567890' copyTransliterating:(Array with:Character cr) to:'' |
|
3367 |
||
11844 | 3368 |
" |
3369 |
! |
|
3370 |
||
3371 |
copyTransliterating:oldElements to:newElements |
|
3372 |
complement:complement squashDuplicates:squashDuplicates |
|
3373 |
||
3374 |
"return a copy, where elements all transliterated via the given mapping. |
|
3375 |
The transiteraion works like perl's tr operator. |
|
3376 |
||
3377 |
Transliterates all occurrences of the characters found in the search list with the |
|
3378 |
corresponding character in the replacement list. It returns the number of characters replaced |
|
3379 |
or deleted. |
|
3380 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
|
3381 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3382 |
||
3383 |
Note that tr does not do other character escapes such as \n. |
|
3384 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3385 |
||
3386 |
Note also that the whole range idea is rather unportable between character sets, |
|
3387 |
and even within character sets they may cause results you probably didn't expect. |
|
3388 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3389 |
case (a-e, A-E), or digits (0-4). |
|
3390 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3391 |
||
3392 |
If complement is true, non-matching chars are affected. |
|
3393 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
|
3394 |
" |
|
3395 |
||
11875 | 3396 |
|map s1 s2 range1 range2 c1 c2 outStream prevXlated last inc| |
11844 | 3397 |
|
3398 |
map := Dictionary new. |
|
3399 |
||
3400 |
s1 := oldElements readStream. |
|
3401 |
s2 := newElements readStream. |
|
3402 |
range1 := range2 := nil. |
|
3403 |
[ |
|
3404 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
|
3405 |
range1 notNil or:[ s1 atEnd not ] |
|
3406 |
] whileTrue:[ |
|
3407 |
range1 notNil ifTrue:[ |
|
3408 |
c1 := range1 next. |
|
3409 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3410 |
] ifFalse:[ |
|
3411 |
c1 := s1 next. |
|
3412 |
s1 peek == $\ ifTrue:[ |
|
3413 |
s1 next. |
|
3414 |
] ifFalse:[ |
|
3415 |
s1 peek == $- ifTrue:[ |
|
3416 |
s1 next. |
|
11875 | 3417 |
last := s1 next. |
3418 |
inc := c1 < last ifTrue:[1] ifFalse:[-1]. |
|
3419 |
range1 := (c1 to:last by:inc) readStream. |
|
11844 | 3420 |
c1 := range1 next. |
3421 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3422 |
]. |
|
3423 |
]. |
|
3424 |
]. |
|
3425 |
range2 notNil ifTrue:[ |
|
3426 |
c2 := range2 next. |
|
3427 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3428 |
] ifFalse:[ |
|
3429 |
s2 atEnd ifTrue:[ |
|
3430 |
"/ keep c2 |
|
3431 |
] ifFalse:[ |
|
3432 |
c2 := s2 next. |
|
3433 |
s2 peek == $\ ifTrue:[ |
|
3434 |
s1 next. |
|
3435 |
] ifFalse:[ |
|
3436 |
s2 peek == $- ifTrue:[ |
|
3437 |
s2 next. |
|
11875 | 3438 |
last := s2 next. |
3439 |
inc := c2 < last ifTrue:[1] ifFalse:[-1]. |
|
3440 |
range2 := (c2 to:last by:inc) readStream. |
|
11844 | 3441 |
c2 := range2 next. |
3442 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3443 |
]. |
|
3444 |
]. |
|
3445 |
]. |
|
3446 |
]. |
|
3447 |
map at:c1 put:c2 |
|
3448 |
]. |
|
3449 |
||
3450 |
outStream := WriteStream on:(self species new). |
|
3451 |
self do:[:el | |
|
3452 |
|xLated out| |
|
3453 |
||
3454 |
complement ifTrue:[ |
|
3455 |
(map includesKey:el) ifTrue:[ |
|
3456 |
xLated := out := el. |
|
3457 |
] ifFalse:[ |
|
3458 |
out := newElements at:1 ifAbsent:nil. |
|
3459 |
]. |
|
3460 |
] ifFalse:[ |
|
3461 |
(map includesKey:el) ifTrue:[ |
|
3462 |
xLated := out := map at:el. |
|
3463 |
] ifFalse:[ |
|
3464 |
out := el. |
|
3465 |
]. |
|
3466 |
]. |
|
3467 |
out notNil ifTrue:[ |
|
3468 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
|
3469 |
"/ ignore |
|
3470 |
] ifFalse:[ |
|
3471 |
outStream nextPut:out. |
|
3472 |
]. |
|
3473 |
prevXlated := out. |
|
3474 |
]. |
|
3475 |
]. |
|
3476 |
^ outStream contents |
|
3477 |
||
3478 |
" |
|
11875 | 3479 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
11844 | 3480 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
3481 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3482 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3483 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3484 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11875 | 3485 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
3486 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
|
3487 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
|
11844 | 3488 |
|
3489 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3490 |
complement:false squashDuplicates:false |
|
3491 |
||
3492 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3493 |
complement:false squashDuplicates:true |
|
3494 |
||
3495 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3496 |
complement:false squashDuplicates:false |
|
3497 |
||
3498 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3499 |
complement:true squashDuplicates:false |
|
3500 |
||
3501 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3502 |
complement:true squashDuplicates:false |
|
3503 |
||
3504 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3505 |
complement:false squashDuplicates:false |
|
3506 |
||
3507 |
- delete all a-zA-Z |
|
3508 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3509 |
complement:false squashDuplicates:false |
|
3510 |
||
3511 |
- delete all except a-zA-Z |
|
3512 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3513 |
complement:true squashDuplicates:false |
|
3514 |
||
3515 |
- squash multiple spaces |
|
3516 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
|
3517 |
complement:false squashDuplicates:true |
|
3518 |
||
3519 |
- squash multiple digits |
|
3520 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
|
3521 |
complement:false squashDuplicates:true |
|
3522 |
" |
|
3523 |
! |
|
3524 |
||
602 | 3525 |
copyUpTo:element |
3526 |
"return a new collection consisting of the receiver elements |
|
8874 | 3527 |
up-to (but excluding) the first occurrence of element; |
602 | 3528 |
or to the end, if element is not included" |
3529 |
||
3530 |
|idx| |
|
3531 |
||
3532 |
idx := self indexOf:element. |
|
3533 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3534 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
3535 |
^ self copyFrom:1 to:(idx-1) |
|
3536 |
||
3537 |
" |
|
3538 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
3539 |
'1234567890' copyUpTo:$5 |
|
3540 |
'1234567890' copyUpTo:$a |
|
3541 |
'1234567890' copyUpTo:$1 |
|
3542 |
'123456789' copyUpTo:$0 |
|
3543 |
" |
|
3544 |
! |
|
3545 |
||
1 | 3546 |
copyWith:newElement |
233 | 3547 |
"return a new collection containing the receivers elements |
8874 | 3548 |
and the single new element, newElement. |
233 | 3549 |
This is different from concatentation, which expects another collection |
3550 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3551 |
|
3552 |
|newCollection mySize newSize| |
|
3553 |
||
3554 |
mySize := self size. |
|
3555 |
newSize := mySize + 1. |
|
252 | 3556 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3557 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3558 |
newCollection at:newSize put:newElement. |
|
3559 |
^newCollection |
|
44 | 3560 |
|
95 | 3561 |
" |
3562 |
#(1 2 3 4 5) copyWith:$a |
|
3563 |
'abcdefg' copyWith:$h |
|
3564 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3565 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3566 |
" |
|
1 | 3567 |
! |
3568 |
||
11855 | 3569 |
copyWithFirst:newFirstElement |
3570 |
"return a new collection containing the receivers elements |
|
3571 |
and the single new element, newElement up front. |
|
3572 |
This is different from concatentation, which expects two collections |
|
3573 |
as argument, but equivalent to copy-and-addFirst." |
|
3574 |
||
3575 |
|newCollection mySize newSize| |
|
3576 |
||
3577 |
mySize := self size. |
|
3578 |
newSize := mySize + 1. |
|
3579 |
newCollection := self copyEmptyAndGrow:newSize. |
|
3580 |
newCollection replaceFrom:2 to:mySize+1 with:self startingAt:1. |
|
3581 |
newCollection at:1 put:newFirstElement. |
|
3582 |
^newCollection |
|
3583 |
||
3584 |
" |
|
3585 |
#(1 2 3 4 5) copyWithFirst:$a |
|
3586 |
'abcdefg' copyWithFirst:$h |
|
3587 |
'abcdefg' copyWithFirst:'123' -- will fail: string cannot be stored into string |
|
3588 |
'abcdefg' copyWithFirst:1 -- will fail: integer cannot be stored into string |
|
3589 |
" |
|
3590 |
! |
|
3591 |
||
61 | 3592 |
copyWithout:elementToSkip |
3593 |
"return a new collection consisting of a copy of the receiver, with |
|
3594 |
ALL elements equal to elementToSkip are left out. |
|
3595 |
No error is reported, if elementToSkip is not in the collection." |
|
3596 |
||
293 | 3597 |
|n "{ Class: SmallInteger }" |
3598 |
sz "{ Class: SmallInteger }" |
|
3599 |
srcIndex "{ Class: SmallInteger }" |
|
3600 |
dstIndex "{ Class: SmallInteger }" |
|
3601 |
skipIndex "{ Class: SmallInteger }" |
|
3602 |
copy l| |
|
61 | 3603 |
|
8874 | 3604 |
"the code below may look like overkill, |
61 | 3605 |
however, for big collections its better to move data |
3606 |
around in big chunks" |
|
3607 |
||
3608 |
n := self occurrencesOf:elementToSkip. |
|
3609 |
n == 0 ifTrue:[^ self copy]. |
|
3610 |
||
3611 |
sz := self size. |
|
252 | 3612 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3613 |
|
3614 |
srcIndex := 1. |
|
3615 |
dstIndex := 1. |
|
3616 |
||
3617 |
n timesRepeat:[ |
|
8874 | 3618 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3619 |
l := skipIndex - srcIndex. |
|
3620 |
l ~~ 0 ifTrue:[ |
|
3621 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3622 |
with:self startingAt:srcIndex. |
|
3623 |
dstIndex := dstIndex + l |
|
3624 |
]. |
|
3625 |
srcIndex := skipIndex + 1 |
|
61 | 3626 |
]. |
3627 |
l := sz - srcIndex. |
|
3628 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3629 |
with:self startingAt:srcIndex. |
61 | 3630 |
^ copy |
3631 |
||
95 | 3632 |
" |
3633 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3634 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3635 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3636 |
'abcdefghi' copyWithout:$h |
3637 |
'abcdefg' copyWithout:$h |
|
95 | 3638 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3639 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3640 |
#(90 80 70 60 50) copyWithout:70 |
|
3641 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3642 |
" |
|
61 | 3643 |
! |
3644 |
||
602 | 3645 |
copyWithoutFirst:elementToSkip |
3646 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3647 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3648 |
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
|
3649 |
Do not confuse this with copyButFirst:" |
602 | 3650 |
|
3651 |
|copy skipIndex sz| |
|
3652 |
||
3653 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3654 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3655 |
||
3656 |
sz := self size - 1. |
|
3657 |
copy := self copyEmptyAndGrow:sz. |
|
3658 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3659 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3660 |
^ copy |
|
3661 |
||
3662 |
" |
|
3663 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3664 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3665 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3666 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3667 |
" |
|
3668 |
! |
|
3669 |
||
8673 | 3670 |
copyWithoutIdentical:elementToSkip |
3671 |
"return a new collection consisting of a copy of the receiver, with |
|
3672 |
ALL elements identical to elementToSkip are left out. |
|
3673 |
No error is reported, if elementToSkip is not in the collection." |
|
3674 |
||
3675 |
|n "{ Class: SmallInteger }" |
|
3676 |
sz "{ Class: SmallInteger }" |
|
3677 |
srcIndex "{ Class: SmallInteger }" |
|
3678 |
dstIndex "{ Class: SmallInteger }" |
|
3679 |
skipIndex "{ Class: SmallInteger }" |
|
3680 |
copy l| |
|
3681 |
||
8874 | 3682 |
"the code below may look like overkill, |
8673 | 3683 |
however, for big collections its better to move data |
3684 |
around in big chunks" |
|
3685 |
||
3686 |
n := self count:[:el | el == elementToSkip]. |
|
3687 |
n == 0 ifTrue:[^ self copy]. |
|
3688 |
||
3689 |
sz := self size. |
|
3690 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3691 |
||
3692 |
srcIndex := 1. |
|
3693 |
dstIndex := 1. |
|
3694 |
||
3695 |
n timesRepeat:[ |
|
8874 | 3696 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3697 |
l := skipIndex - srcIndex. |
|
3698 |
l ~~ 0 ifTrue:[ |
|
3699 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3700 |
with:self startingAt:srcIndex. |
|
3701 |
dstIndex := dstIndex + l |
|
3702 |
]. |
|
3703 |
srcIndex := skipIndex + 1 |
|
8673 | 3704 |
]. |
3705 |
l := sz - srcIndex. |
|
3706 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3707 |
with:self startingAt:srcIndex. |
8673 | 3708 |
^ copy |
3709 |
||
3710 |
" |
|
8874 | 3711 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3712 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3713 |
" |
3714 |
! |
|
3715 |
||
61 | 3716 |
copyWithoutIndex:omitIndex |
3717 |
"return a new collection consisting of receivers elements |
|
3718 |
without the argument stored at omitIndex" |
|
3719 |
||
3720 |
|copy sz| |
|
3721 |
||
202 | 3722 |
sz := self size - 1. |
252 | 3723 |
copy := self copyEmptyAndGrow:sz. |
293 | 3724 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3725 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3726 |
^ copy |
3727 |
||
95 | 3728 |
" |
3729 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3730 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3731 |
" |
|
1 | 3732 |
! |
3733 |
||
202 | 3734 |
copyWithoutLast:count |
3735 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3736 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3737 |
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
|
3738 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3739 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3740 |
|
9250 | 3741 |
<resource:#obsolete> |
3742 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3743 |
^ self copyTo:(self size - count) |
202 | 3744 |
|
3745 |
" |
|
8874 | 3746 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3747 |
'1234567890' copyWithoutLast:4 |
|
202 | 3748 |
" |
5231 | 3749 |
! |
3750 |
||
12750
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3751 |
repeatedConcatenation:nTimes |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3752 |
"return a new collection consisting of nTimes the concatenation of the receiver" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3753 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3754 |
|newColl idx| |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3755 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3756 |
newColl := self species new:(self size * nTimes). |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3757 |
idx := 1. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3758 |
nTimes timesRepeat:[ |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3759 |
newColl replaceFrom:idx with:self startingAt:1. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3760 |
idx := idx + self size. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3761 |
]. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3762 |
^ newColl |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3763 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3764 |
" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3765 |
'hello' repeatedConcatenation:5 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3766 |
#[1 2 3] repeatedConcatenation:3 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3767 |
#(1 2 3) repeatedConcatenation:1 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3768 |
" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3769 |
! |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3770 |
|
5231 | 3771 |
restAfter:anElement |
8366 | 3772 |
"return a new collection consisting of the receivers elements after |
5231 | 3773 |
(but excluding) anElement. |
8366 | 3774 |
If anElement is not in the receiver, the returned collection |
3775 |
will be empty. |
|
3776 |
See also #upTo:." |
|
5231 | 3777 |
|
3778 |
|pos| |
|
3779 |
||
3780 |
pos := self indexOf:anElement. |
|
8366 | 3781 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3782 |
||
5231 | 3783 |
^ self copyFrom:(pos + 1) |
3784 |
||
3785 |
" |
|
8874 | 3786 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3787 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3788 |
'hello world' upTo:Character space |
|
3789 |
'hello world' restAfter:Character space |
|
3790 |
'1234.5678' upTo:$. |
|
3791 |
'1234.5678' restAfter:$. |
|
8366 | 3792 |
" |
5231 | 3793 |
! |
3794 |
||
12187 | 3795 |
trimForWhich:aCheckBlock |
3796 |
"return a copy of myself without leading and trailing elements for which aCheckBlock returns true. |
|
3797 |
Normally, this is mostly used with string receivers." |
|
3798 |
||
3799 |
|startIndex "{ Class: SmallInteger }" |
|
3800 |
endIndex "{ Class: SmallInteger }" |
|
3801 |
sz| |
|
3802 |
||
3803 |
sz := self size. |
|
3804 |
startIndex := 1. |
|
3805 |
endIndex := sz. |
|
3806 |
||
12320 | 3807 |
[(startIndex <= endIndex) and:[aCheckBlock value:(self at:startIndex)]] whileTrue:[ |
12187 | 3808 |
startIndex := startIndex + 1 |
3809 |
]. |
|
3810 |
[(endIndex > 1) and:[aCheckBlock value:(self at:endIndex)]] whileTrue:[ |
|
3811 |
endIndex := endIndex - 1 |
|
3812 |
]. |
|
3813 |
startIndex > endIndex ifTrue:[ |
|
3814 |
^ self class new |
|
3815 |
]. |
|
3816 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
|
3817 |
^ self |
|
3818 |
]. |
|
3819 |
^ self copyFrom:startIndex to:endIndex |
|
3820 |
||
3821 |
" |
|
12320 | 3822 |
' foo ' trimForWhich:[:ch | ch isSeparator] |
3823 |
' ' trimForWhich:[:ch | ch isSeparator] |
|
12187 | 3824 |
#(0 0 0 1 2 3 0 0 0) trimForWhich:[:e | e = 0] |
3825 |
" |
|
3826 |
! |
|
3827 |
||
5231 | 3828 |
upTo:anElement |
3829 |
"return a new collection consisting of the receivers elements upTo |
|
3830 |
(but excluding) anElement. |
|
3831 |
If anElement is not in the receiver, the returned collection |
|
3832 |
will consist of all elements of the receiver. |
|
9307 | 3833 |
See also #restAfter: , #copyFrom:index." |
5231 | 3834 |
|
3835 |
|pos| |
|
3836 |
||
3837 |
pos := self indexOf:anElement. |
|
3838 |
pos == 0 ifTrue:[^ self copy]. |
|
3839 |
||
3840 |
^ self copyFrom:1 to:(pos - 1) |
|
3841 |
||
3842 |
" |
|
8874 | 3843 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3844 |
'hello world' upTo:Character space |
|
3845 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3846 |
'1234.5678' upTo:$. |
|
3847 |
'1234' upTo:$. |
|
5231 | 3848 |
|
3849 |
raises an error: |
|
3850 |
||
8874 | 3851 |
(Dictionary new |
9307 | 3852 |
at:#foo put:'foo'; |
3853 |
at:#bar put:'bar'; |
|
3854 |
at:#baz put:'baz'; |
|
3855 |
yourself) upTo:#bar |
|
8874 | 3856 |
|
5231 | 3857 |
" |
3858 |
||
3859 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3860 |
! |
|
3861 |
||
3862 |
upTo:anElement count:count |
|
3863 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3864 |
(but excluding) count found anElements, i.e. the first found count-1 |
3865 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3866 |
procedure is done from the end of the collection backwards. |
3867 |
If anElement is not in the receiver, the returned collection |
|
3868 |
will consist of all elements of the receiver" |
|
3869 |
||
3870 |
|startPos endPos pos found| |
|
3871 |
||
3872 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3873 |
^ self upTo:anElement |
5231 | 3874 |
]. |
3875 |
||
3876 |
startPos := 1. |
|
3877 |
endPos := self size. |
|
3878 |
found := 0. |
|
3879 |
||
3880 |
count > 1 ifTrue:[ |
|
8874 | 3881 |
pos := 0. |
3882 |
[pos < self size and: [found < count]] |
|
3883 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3884 |
found == count ifTrue:[ |
|
3885 |
endPos := pos - 1 |
|
3886 |
] |
|
3887 |
]. |
|
5231 | 3888 |
|
3889 |
count < 0 ifTrue:[ |
|
8874 | 3890 |
pos := self size + 1. |
3891 |
[pos > 1 and: [found < count abs]] |
|
3892 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3893 |
found == count abs ifTrue:[ |
|
3894 |
startPos := pos + 1 |
|
3895 |
] |
|
3896 |
]. |
|
5231 | 3897 |
|
3898 |
^ self copyFrom: startPos to: endPos |
|
3899 |
||
3900 |
" |
|
8874 | 3901 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3902 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3903 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3904 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3905 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3906 |
" |
|
5563 | 3907 |
! |
3908 |
||
3909 |
upToAny:aCollectionOfObjects |
|
3910 |
"return a new collection consisting of the receivers elements upTo |
|
3911 |
(but excluding) any in aCollectionOfObjects. |
|
3912 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3913 |
will consist of all elements of the receiver. |
|
3914 |
See also #upTo:" |
|
3915 |
||
3916 |
|pos| |
|
3917 |
||
3918 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3919 |
pos == 0 ifTrue:[^ self copy]. |
|
3920 |
||
3921 |
^ self copyFrom:1 to:(pos - 1) |
|
3922 |
||
3923 |
" |
|
8874 | 3924 |
'hello world' upToAny:#($/ $:) |
3925 |
'hello:world' upToAny:#($/ $:) |
|
3926 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3927 |
" |
12187 | 3928 |
! |
3929 |
||
3930 |
withoutLeadingForWhich:aCheckBlock |
|
3931 |
"return a copy of myself without leading eleents for which aCheckBlock returns true. |
|
3932 |
Returns an empty collection, if the receiver consist only of matching elements. |
|
3933 |
Normally, this is mostly used with string receivers." |
|
3934 |
||
3935 |
|index sz| |
|
3936 |
||
3937 |
sz := self size. |
|
3938 |
index := 1. |
|
3939 |
[index <= sz] whileTrue:[ |
|
3940 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
3941 |
index == 1 ifTrue:[^ self]. |
|
3942 |
^ self copyFrom:index |
|
3943 |
]. |
|
3944 |
index := index + 1 |
|
3945 |
]. |
|
3946 |
^ self class new |
|
3947 |
||
3948 |
" |
|
3949 |
'****foo****' withoutLeadingForWhich:[:ch | ch isLetter not] |
|
3950 |
#( 0 0 0 1 2 3 4 0 0 0) withoutLeadingForWhich:[:e | e = 0] |
|
3951 |
" |
|
3952 |
! |
|
3953 |
||
3954 |
withoutTrailingForWhich:aCheckBlock |
|
3955 |
"return a copy of myself without trailing characters for which aCheckBlock returns true. |
|
3956 |
Returns an empty collection, if the receiver consist only of matching chars." |
|
3957 |
||
3958 |
|index sz| |
|
3959 |
||
3960 |
index := sz := self size. |
|
3961 |
[index ~~ 0] whileTrue:[ |
|
3962 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
3963 |
index == sz ifTrue:[^ self]. |
|
3964 |
^ self copyTo:index |
|
3965 |
]. |
|
3966 |
index := index - 1 |
|
3967 |
]. |
|
3968 |
^ self class new |
|
3969 |
||
3970 |
" |
|
3971 |
' foo....' withoutTrailingForWhich:[:ch | ch isLetter not]. |
|
3972 |
#( 0 0 0 1 2 3 4 0 0 0) withoutTrailingForWhich:[:e | e = 0] |
|
3973 |
" |
|
602 | 3974 |
! ! |
3975 |
||
3976 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3977 |
||
3978 |
collect:aBlock |
|
3979 |
"evaluate the argument, aBlock for every element in the collection |
|
3980 |
and return a collection of the results" |
|
3981 |
||
3982 |
|newCollection |
|
3983 |
sz "{ Class:SmallInteger }"| |
|
3984 |
||
3985 |
sz := self size. |
|
3986 |
newCollection := self copyEmptyAndGrow:sz. |
|
3987 |
1 to:sz do:[:index | |
|
8874 | 3988 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3989 |
]. |
3990 |
^ newCollection |
|
3991 |
||
3992 |
" |
|
8874 | 3993 |
#(one two three four five six) collect:[:element | element asUppercase] |
3994 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3995 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3996 |
" |
202 | 3997 |
! |
3998 |
||
602 | 3999 |
do:aBlock |
4000 |
"evaluate the argument, aBlock for every element in the collection." |
|
4001 |
||
4002 |
|stop "{ Class:SmallInteger }"| |
|
4003 |
||
4004 |
stop := self size. |
|
4005 |
1 to:stop do:[:index | |
|
8874 | 4006 |
aBlock value:(self at:index). |
602 | 4007 |
] |
2927 | 4008 |
|
602 | 4009 |
" |
1422 | 4010 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 4011 |
" |
4012 |
! |
|
4013 |
||
2927 | 4014 |
do:aBlock separatedBy:sepBlock |
4015 |
"evaluate the argument, aBlock for every element in the collection. |
|
4016 |
Between each element (i.e. not before the first element and not after the |
|
4017 |
last element), evaluate sepBlock. |
|
4018 |
This supports printing of collections with elements separated by some |
|
4019 |
string." |
|
4020 |
||
4021 |
|stop "{ Class:SmallInteger }"| |
|
4022 |
||
4023 |
stop := self size. |
|
4024 |
stop < 1 ifTrue:[^ self]. |
|
4025 |
aBlock value:(self at:1). |
|
4026 |
2 to:stop do:[:index | |
|
8874 | 4027 |
sepBlock value. |
4028 |
aBlock value:(self at:index) |
|
2927 | 4029 |
]. |
4030 |
||
4031 |
" |
|
4032 |
#(one two three four five six) |
|
8874 | 4033 |
do:[:each | Transcript show:each] |
4034 |
separatedBy:[Transcript show:' , '] |
|
2927 | 4035 |
" |
4036 |
! |
|
3003 | 4037 |
|
3044 | 4038 |
doWithIndex:aBlock |
4039 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
4040 |
the index as second argument." |
|
4041 |
||
4042 |
self keysAndValuesDo:[:index :value | |
|
8874 | 4043 |
aBlock value:value value:index |
3044 | 4044 |
]. |
4045 |
||
4046 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
4047 |
! |
|
4048 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4049 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4050 |
"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
|
4051 |
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
|
4052 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4053 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4054 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4055 |
" |
8874 | 4056 |
#(one two three four five six) |
4057 |
from:2 |
|
4058 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4059 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4060 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4061 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4062 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4063 |
|
602 | 4064 |
from:startIndex do:aBlock |
4065 |
"evaluate the argument, aBlock for the elements starting with the |
|
4066 |
element at startIndex to the end." |
|
4067 |
||
4068 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 4069 |
|
95 | 4070 |
" |
8874 | 4071 |
#(one two three four five six) |
4072 |
from:3 |
|
4073 |
do:[:element | Transcript showCR:element] |
|
602 | 4074 |
" |
4075 |
! |
|
4076 |
||
7490 | 4077 |
from:startIndex keysAndValuesDo:aBlock |
4078 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
4079 |
element at startIndex to the end." |
|
4080 |
||
4081 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
4082 |
||
4083 |
" |
|
8874 | 4084 |
#(one two three four five six) |
4085 |
from:3 |
|
4086 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 4087 |
" |
4088 |
! |
|
4089 |
||
7337 | 4090 |
from:index1 to:index2 by:stepArg do:aBlock |
4091 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
4092 |
||
4093 |
|start "{ Class:SmallInteger }" |
|
4094 |
stop "{ Class:SmallInteger }" |
|
4095 |
step "{ Class:SmallInteger }" | |
|
4096 |
||
4097 |
start := index1. "/ these assignments force type checking... |
|
4098 |
stop := index2. "/ and guarantee inline loop code below. |
|
4099 |
step := stepArg. |
|
4100 |
||
4101 |
start to:stop by:step do:[:index | |
|
8874 | 4102 |
aBlock value:(self at:index). |
7337 | 4103 |
] |
4104 |
||
4105 |
" |
|
8874 | 4106 |
#(one two three four five six seven eight nine ten) |
4107 |
from:2 |
|
4108 |
to:10 |
|
4109 |
by:2 |
|
4110 |
do:[:element | Transcript showCR:element] |
|
7337 | 4111 |
" |
4112 |
! |
|
4113 |
||
602 | 4114 |
from:start to:stop collect:aBlock |
4115 |
"evaluate the argument, aBlock for the elements indexed by start |
|
4116 |
to stop in the collection and return a collection of the results" |
|
4117 |
||
4118 |
|newCollection sz |
|
4119 |
idx "{ Class:SmallInteger }"| |
|
4120 |
||
4121 |
sz := stop - start + 1. |
|
4122 |
newCollection := self copyEmptyAndGrow:sz. |
|
4123 |
idx := 1. |
|
4124 |
start to:stop do:[:index | |
|
8874 | 4125 |
newCollection at:idx put:(aBlock value:(self at:index)). |
4126 |
idx := idx + 1 |
|
602 | 4127 |
]. |
4128 |
^ newCollection |
|
4129 |
||
4130 |
" |
|
8874 | 4131 |
#(one two three four five six) |
4132 |
from:2 |
|
4133 |
to:4 |
|
4134 |
collect:[:element | element asUppercase] |
|
602 | 4135 |
" |
4136 |
! |
|
4137 |
||
4138 |
from:index1 to:index2 do:aBlock |
|
4139 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4140 |
index2 in the collection" |
|
4141 |
||
4142 |
|start "{ Class:SmallInteger }" |
|
4143 |
stop "{ Class:SmallInteger }" | |
|
4144 |
||
7337 | 4145 |
start := index1. "/ these assignments force type checking... |
4146 |
stop := index2. "/ and guarantee inline loop code below. |
|
4147 |
||
602 | 4148 |
start to:stop do:[:index | |
8874 | 4149 |
aBlock value:(self at:index). |
602 | 4150 |
] |
4151 |
||
4152 |
" |
|
8874 | 4153 |
#(one two three four five six) |
4154 |
from:3 |
|
4155 |
to:5 |
|
4156 |
do:[:element | Transcript showCR:element] |
|
602 | 4157 |
" |
4158 |
! |
|
4159 |
||
6868 | 4160 |
from:index1 to:index2 keysAndValuesDo:aBlock |
4161 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4162 |
index2 in the collection; pass both index and value." |
|
4163 |
||
4164 |
|start "{ Class:SmallInteger }" |
|
4165 |
stop "{ Class:SmallInteger }" | |
|
4166 |
||
7337 | 4167 |
start := index1. "/ these assignments force type checking... |
4168 |
stop := index2. "/ and guarantee inline loop code below. |
|
4169 |
||
6868 | 4170 |
start to:stop do:[:index | |
8874 | 4171 |
aBlock value:index value:(self at:index). |
6868 | 4172 |
] |
4173 |
||
4174 |
" |
|
8874 | 4175 |
#(one two three four five six) |
4176 |
from:3 |
|
4177 |
to:5 |
|
4178 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 4179 |
" |
4180 |
! |
|
4181 |
||
602 | 4182 |
from:index1 to:index2 reverseDo:aBlock |
4183 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4184 |
index2 in the collection. Step in reverse order" |
|
4185 |
||
4186 |
|start "{ Class:SmallInteger }" |
|
4187 |
stop "{ Class:SmallInteger }" | |
|
4188 |
||
7337 | 4189 |
start := index1. "/ these assignments force type checking... |
4190 |
stop := index2. "/ and guarantee inline loop code below. |
|
4191 |
||
602 | 4192 |
stop to:start by:-1 do:[:index | |
8874 | 4193 |
aBlock value:(self at:index). |
602 | 4194 |
] |
4195 |
||
4196 |
" |
|
8874 | 4197 |
#(one two three four five six) |
4198 |
from:3 |
|
4199 |
to:5 |
|
4200 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 4201 |
" |
61 | 4202 |
! |
4203 |
||
6027 | 4204 |
from:startIndex to:stopIndex select:aBlock |
4205 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
4206 |
and return a collection of those elements for which the block return true." |
|
4207 |
||
4208 |
|element newColl species needCopy |
|
4209 |
n "{ Class:SmallInteger }"| |
|
4210 |
||
4211 |
n := stopIndex - startIndex + 1. |
|
4212 |
species := self species. |
|
4213 |
species growIsCheap ifFalse:[ |
|
8874 | 4214 |
newColl := OrderedCollection new:n. |
4215 |
needCopy := true |
|
6027 | 4216 |
] ifTrue:[ |
8874 | 4217 |
newColl := self copyEmpty:n. |
4218 |
needCopy := false |
|
6027 | 4219 |
]. |
4220 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4221 |
element := self at:index. |
4222 |
(aBlock value:element) ifTrue:[ |
|
4223 |
newColl add:element |
|
4224 |
]. |
|
6027 | 4225 |
]. |
4226 |
needCopy ifTrue:[ |
|
8874 | 4227 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 4228 |
]. |
4229 |
^ newColl |
|
4230 |
||
4231 |
" |
|
8874 | 4232 |
#(faba one two three four five six) |
4233 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 4234 |
" |
4235 |
! |
|
4236 |
||
10150 | 4237 |
inGroupsOf:n collect:anNArgBlock |
4238 |
"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
|
4239 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4240 |
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
|
4241 |
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
|
4242 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4243 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4244 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4245 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4246 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4247 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4248 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4249 |
|
10150 | 4250 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
4251 |
"/ 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
|
4252 |
n == 2 ifTrue:[ |
10150 | 4253 |
1 to:stop by:n do:[:index | |
4254 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4255 |
newCollection at:dstIdx put:rslt. |
|
4256 |
dstIdx := dstIdx + 1. |
|
4257 |
]. |
|
4258 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4259 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4260 |
n == 3 ifTrue:[ |
10150 | 4261 |
1 to:stop by:n do:[:index | |
4262 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4263 |
newCollection at:dstIdx put:rslt. |
|
4264 |
dstIdx := dstIdx + 1. |
|
4265 |
]. |
|
4266 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4267 |
]. |
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 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4270 |
1 to:stop by:n do:[:index | |
10150 | 4271 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4272 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
4273 |
newCollection at:dstIdx put:rslt. |
|
4274 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4275 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4276 |
^ newCollection |
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 |
"for groups of 2, this is the same as: |
8874 | 4279 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4280 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 4281 |
|
4282 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4283 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
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 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4286 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
4287 |
" |
|
4288 |
||
4289 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
4290 |
! |
|
4291 |
||
4292 |
inGroupsOf:n do:anNArgBlock |
|
4293 |
"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
|
4294 |
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
|
4295 |
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
|
4296 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4297 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4298 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4299 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4300 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4301 |
|
10150 | 4302 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
4303 |
"/ 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
|
4304 |
n == 2 ifTrue:[ |
10150 | 4305 |
1 to:stop by:n do:[:index | |
4306 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4307 |
]. |
|
4308 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4309 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4310 |
n == 3 ifTrue:[ |
10150 | 4311 |
1 to:stop by:n do:[:index | |
4312 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4313 |
]. |
|
4314 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4315 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4316 |
n == 4 ifTrue:[ |
10150 | 4317 |
1 to:stop by:n do:[:index | |
4318 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
4319 |
]. |
|
4320 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4321 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4322 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4323 |
argVector := Array new:n. |
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 |
1 to:stop by:n do:[:index | |
10150 | 4326 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4327 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4328 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4329 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4330 |
"for groups of 2, this is the same as: |
8874 | 4331 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4332 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4333 |
|
4334 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4335 |
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
|
4336 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4337 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4338 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
4339 |
" |
|
4340 |
||
4341 |
"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
|
4342 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4343 |
|
8853 | 4344 |
keysAndValuesCollect:aTwoArgBlock |
4345 |
"evaluate the argument, aBlock for every element in the collection, |
|
4346 |
passing both index and element as arguments. |
|
4347 |
Collect the returned values and return them." |
|
4348 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4349 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4350 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4351 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4352 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4353 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4354 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 4355 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4356 |
|
8853 | 4357 |
^ newCollection |
4358 |
||
4359 |
" |
|
8874 | 4360 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4361 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4362 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 4363 |
|
4364 |
||
4365 |
|d| |
|
4366 |
||
4367 |
d := Dictionary new. |
|
4368 |
d at:'a' put:'A'. |
|
4369 |
d at:'b' put:'B'. |
|
4370 |
d at:'c' put:'C'. |
|
4371 |
d at:'d' put:'D'. |
|
4372 |
d at:'e' put:'E'. |
|
8874 | 4373 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4374 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4375 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4376 |
ifFalse:[element asLowercase]] |
8853 | 4377 |
" |
4378 |
! |
|
4379 |
||
602 | 4380 |
keysAndValuesDo:aTwoArgBlock |
4381 |
"evaluate the argument, aBlock for every element in the collection, |
|
4382 |
passing both index and element as arguments." |
|
4383 |
||
4384 |
|stop "{ Class:SmallInteger }"| |
|
4385 |
||
4386 |
stop := self size. |
|
4387 |
1 to:stop do:[:index | |
|
8874 | 4388 |
aTwoArgBlock value:index value:(self at:index). |
602 | 4389 |
] |
4390 |
" |
|
8874 | 4391 |
#(one two three four five six) |
4392 |
keysAndValuesDo:[:key :element | |
|
4393 |
Transcript show:key; space; showCR:element |
|
4394 |
] |
|
602 | 4395 |
" |
4396 |
! |
|
4397 |
||
1359 | 4398 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 4399 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 4400 |
in the collection, passing both index and element as arguments." |
4401 |
||
4402 |
|stop "{ Class:SmallInteger }"| |
|
4403 |
||
4404 |
stop := self size. |
|
4405 |
stop to:1 by:-1 do:[:index | |
|
8874 | 4406 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 4407 |
] |
4408 |
||
4409 |
" |
|
8874 | 4410 |
#(one two three four five six) |
4411 |
keysAndValuesReverseDo:[:key :element | |
|
4412 |
Transcript show:key; space; showCR:element |
|
4413 |
] |
|
1359 | 4414 |
" |
4415 |
||
4416 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
4417 |
! |
|
4418 |
||
602 | 4419 |
nonNilElementsDo:aBlock |
4420 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
4421 |
||
4422 |
|stop "{ Class:SmallInteger }" |
|
4423 |
element| |
|
4424 |
||
4425 |
stop := self size. |
|
4426 |
1 to:stop do:[:index | |
|
8874 | 4427 |
(element := self at:index) notNil ifTrue:[ |
4428 |
aBlock value:element. |
|
4429 |
] |
|
602 | 4430 |
] |
4431 |
" |
|
1422 | 4432 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 4433 |
" |
4434 |
! |
|
4435 |
||
10150 | 4436 |
pairWiseCollect:aTwoArgBlock |
4437 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 4438 |
The block is called with 2 arguments for each 2 elements in the receiver. |
4439 |
An error will be reported, if the number of elements in the receiver |
|
4440 |
is not a multiple of 2. |
|
4441 |
Collect the results and return a new collection containing those." |
|
4442 |
||
10150 | 4443 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 4444 |
|
95 | 4445 |
" |
8874 | 4446 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4447 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 4448 |
|
4449 |
||
4450 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4451 |
pairWiseCollect:[:x :y | x@y] |
4452 |
" |
|
4453 |
||
4454 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
4455 |
! |
|
4456 |
||
4457 |
pairWiseDo:aTwoArgBlock |
|
10385 | 4458 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
4459 |
An error will be reported, if the number of elements in the receiver |
|
12346 | 4460 |
is not a multiple of 2. |
4461 |
CONFUSION ATTACK: |
|
4462 |
this is different from pairWiseDo:. |
|
4463 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
|
4464 |
(sigh: but we were first, so they should have adapted...)" |
|
602 | 4465 |
|
10150 | 4466 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4467 |
|
602 | 4468 |
" |
8874 | 4469 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4470 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4471 |
|
4472 |
||
4473 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4474 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
4475 |
" |
|
4476 |
||
10385 | 4477 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 4478 |
! |
4479 |
||
602 | 4480 |
reverseDo:aBlock |
4481 |
"evaluate the argument, aBlock for every element in the collection |
|
4482 |
in reverse order" |
|
4483 |
||
4484 |
|sz "{ Class:SmallInteger }"| |
|
4485 |
||
4486 |
sz := self size. |
|
4487 |
sz to:1 by:-1 do:[:index | |
|
8874 | 4488 |
aBlock value:(self at:index). |
602 | 4489 |
] |
4490 |
||
4491 |
" |
|
1422 | 4492 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 4493 |
" |
4494 |
! |
|
4495 |
||
4496 |
select:aBlock |
|
4497 |
"evaluate the argument, aBlock for every element in the collection |
|
4498 |
and return a collection of all elements for which the block return |
|
4499 |
true" |
|
4500 |
||
4501 |
|element newColl species needCopy |
|
4502 |
sz "{ Class:SmallInteger }"| |
|
4503 |
||
4504 |
sz := self size. |
|
4505 |
species := self species. |
|
4506 |
species growIsCheap ifFalse:[ |
|
8874 | 4507 |
newColl := OrderedCollection new:sz. |
4508 |
needCopy := true |
|
602 | 4509 |
] ifTrue:[ |
8874 | 4510 |
newColl := self copyEmpty:sz. |
4511 |
needCopy := false |
|
602 | 4512 |
]. |
4513 |
1 to:sz do:[:index | |
|
8874 | 4514 |
element := self at:index. |
4515 |
(aBlock value:element) ifTrue:[ |
|
4516 |
newColl add:element |
|
4517 |
]. |
|
602 | 4518 |
]. |
4519 |
needCopy ifTrue:[ |
|
8874 | 4520 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 4521 |
]. |
61 | 4522 |
^ newColl |
4523 |
||
95 | 4524 |
" |
8874 | 4525 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
4526 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
4527 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
4528 |
(1 to:9) select:[:element | element odd] |
|
4529 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 4530 |
" |
186 | 4531 |
! |
4532 |
||
602 | 4533 |
with:aSequenceableCollection do:aTwoArgBlock |
4534 |
"evaluate the argument, aBlock for successive elements from |
|
4535 |
each the receiver and the argument, aSequenceableCollection. |
|
4536 |
The second argument, aBlock must be a two-argument block. |
|
4537 |
The collection argument must implement access via a numeric key." |
|
4538 |
||
4539 |
|stop "{ Class: SmallInteger }" | |
|
4540 |
||
4541 |
stop := self size. |
|
4542 |
1 to:stop do:[:index | |
|
8874 | 4543 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 4544 |
] |
186 | 4545 |
" |
8874 | 4546 |
#(one two three four five six) |
4547 |
with:(1 to:10) |
|
4548 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 4549 |
" |
9454 | 4550 |
! |
4551 |
||
4552 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
4553 |
"evaluate the argument, aBlock for successive elements from |
|
4554 |
each the receiver and the argument, aSequenceableCollection. |
|
4555 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4556 |
the index as arguments. |
|
4557 |
The collection argument must implement access via a numeric key." |
|
4558 |
||
4559 |
|stop "{ Class: SmallInteger }" | |
|
4560 |
||
4561 |
stop := self size. |
|
4562 |
1 to:stop do:[:index | |
|
4563 |
aThreeArgBlock |
|
4564 |
value:(self at:index) |
|
4565 |
value:(aSequenceableCollection at:index) |
|
4566 |
value:index. |
|
4567 |
] |
|
4568 |
" |
|
4569 |
#(one two three four five six) |
|
4570 |
with:(1 to:10) |
|
4571 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4572 |
" |
|
4573 |
||
4574 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 4575 |
! ! |
4576 |
||
4154 | 4577 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 4578 |
|
602 | 4579 |
atAllPut:anObject |
4580 |
"replace all elements of the collection by the argument, anObject. |
|
4581 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4582 |
therefore the change may affect all others referencing the receiver." |
|
4583 |
||
4584 |
self from:1 to:(self size) put:anObject |
|
4585 |
||
4586 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4587 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4588 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4589 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4590 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4591 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4592 |
" |
602 | 4593 |
(Array new:10) atAllPut:1 |
4594 |
(String new:10) atAllPut:$a |
|
4595 |
" |
|
3468
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 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 4598 |
! |
4599 |
||
1 | 4600 |
from:index1 to:index2 put:anObject |
4601 |
"replace the elements from index1 to index2 of the collection |
|
44 | 4602 |
by the argument, anObject. |
4603 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4604 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4605 |
|
4606 |
|index "{ Class: SmallInteger }" |
|
4607 |
end "{ Class: SmallInteger }"| |
|
4608 |
||
4609 |
index := index1. |
|
4610 |
end := index2. |
|
4611 |
[index <= end] whileTrue:[ |
|
8874 | 4612 |
self at:index put:anObject. |
4613 |
index := index + 1 |
|
1 | 4614 |
] |
44 | 4615 |
|
95 | 4616 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4617 |
args: index1 : <integer> |
8874 | 4618 |
index2 : <integer> |
4619 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4620 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4621 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4622 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4623 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4624 |
" |
95 | 4625 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
4626 |
'abcdefghijkl' from:3 to:6 put:$X |
|
4627 |
" |
|
3468
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 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 4630 |
! |
4631 |
||
3003 | 4632 |
replaceAll:oldObject by:newObject |
4633 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
4634 |
Pleace do no longer use this one; |
|
4635 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
4636 |
||
5872 | 4637 |
<resource:#obsolete> |
4638 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4639 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 4640 |
^ self replaceAll:oldObject with:newObject |
4641 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4642 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4643 |
args: oldObject : <object> |
8874 | 4644 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4645 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4646 |
returns: self |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4649 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 4650 |
! |
4651 |
||
4652 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
4653 |
"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
|
4654 |
Please do no longer use this one; |
3003 | 4655 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
4656 |
||
5872 | 4657 |
<resource:#obsolete> |
4658 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4659 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 4660 |
^ 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
|
4661 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4662 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4663 |
args: oldObject : <object> |
8874 | 4664 |
newObject : <object> |
4665 |
startIndex : <integer> |
|
4666 |
stopIndex : <integer> |
|
4667 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4668 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4669 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4670 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4671 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 4672 |
! |
4673 |
||
2927 | 4674 |
replaceAll:oldObject with:newObject |
44 | 4675 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4676 |
|
44 | 4677 |
Notice: This operation modifies the receiver, NOT a copy; |
4678 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4679 |
|
8874 | 4680 |
^ self |
4681 |
replaceAll:oldObject |
|
4682 |
with:newObject |
|
4683 |
from:1 |
|
4684 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4685 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4686 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4687 |
args: oldObject : <object> |
8874 | 4688 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4689 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4690 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4691 |
" |
362 | 4692 |
|
4693 |
" |
|
8874 | 4694 |
'123123abc123' replaceAll:$1 with:$* |
4695 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4696 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4697 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4698 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4699 |
! |
293 | 4700 |
|
2927 | 4701 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4702 |
"replace all oldObjects found between startIndex and endIndex, |
4703 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4704 |
|
362 | 4705 |
Notice: This operation modifies the receiver, NOT a copy; |
4706 |
therefore the change may affect all others referencing the receiver." |
|
4707 |
||
4708 |
|start "{ Class: SmallInteger }" |
|
4709 |
stop "{ Class: SmallInteger }"| |
|
4710 |
||
4711 |
start := startIndex. |
|
4712 |
stop := stopIndex. |
|
4713 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4714 |
(self at:index) = oldObject ifTrue:[ |
4715 |
self at:index put:newObject |
|
4716 |
] |
|
1 | 4717 |
] |
44 | 4718 |
|
95 | 4719 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4720 |
args: oldObject : <object> |
8874 | 4721 |
newObject : <object> |
4722 |
startIndex : <integer> |
|
4723 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4724 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4725 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4726 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4727 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4728 |
" |
2980 | 4729 |
'123123abc123' replaceAll:$1 with:$* |
4730 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4731 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4732 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4733 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4734 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4735 |
! |
4736 |
||
7615 | 4737 |
replaceAllForWhich:aConditionBlock with:newObject |
4738 |
"replace all elements for which aConditionBlock returns true |
|
4739 |
between startIndex and endIndex, by newObject in the receiver. |
|
4740 |
||
4741 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4742 |
therefore the change may affect all others referencing the receiver." |
|
4743 |
||
8874 | 4744 |
self |
4745 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4746 |
from:1 to:(self size) |
|
7615 | 4747 |
|
4748 |
" |
|
4749 |
args: aConditionBlock : <block> |
|
8874 | 4750 |
newObject : <object> |
7615 | 4751 |
|
4752 |
returns: self |
|
4753 |
" |
|
4754 |
! |
|
4755 |
||
4756 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4757 |
"replace all elements for which aConditionBlock returns true |
|
4758 |
between startIndex and endIndex, by newObject in the receiver. |
|
4759 |
||
4760 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4761 |
therefore the change may affect all others referencing the receiver." |
|
4762 |
||
4763 |
|start "{ Class: SmallInteger }" |
|
4764 |
stop "{ Class: SmallInteger }"| |
|
4765 |
||
4766 |
start := startIndex. |
|
4767 |
stop := stopIndex. |
|
4768 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4769 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4770 |
self at:index put:newObject |
|
4771 |
] |
|
7615 | 4772 |
] |
4773 |
||
4774 |
" |
|
4775 |
args: aConditionBlock : <block> |
|
8874 | 4776 |
newObject : <object> |
4777 |
startIndex : <integer> |
|
4778 |
stopIndex : <integer> |
|
7615 | 4779 |
|
4780 |
returns: self |
|
4781 |
" |
|
4782 |
! |
|
4783 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4784 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4785 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4786 |
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
|
4787 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4788 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4789 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4790 |
|
8874 | 4791 |
^ self |
4792 |
replaceAllIdentical:oldObject |
|
4793 |
with:newObject |
|
4794 |
from:1 |
|
4795 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4796 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4797 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4798 |
args: oldObject : <object> |
8874 | 4799 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4800 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4801 |
returns: self |
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 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4804 |
" |
8874 | 4805 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4806 |
#(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
|
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 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4810 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4811 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4812 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4813 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4814 |
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
|
4815 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4816 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4817 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4818 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4819 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4820 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4821 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4822 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4823 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4824 |
startIndex to:stopIndex do:[:index | |
8874 | 4825 |
(self at:index) == oldObject ifTrue:[ |
4826 |
self at:index put:newObject |
|
4827 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4828 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4829 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4830 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4831 |
args: oldObject : <object> |
8874 | 4832 |
newObject : <object> |
4833 |
startIndex : <integer> |
|
4834 |
stopIndex : <integer> |
|
5320
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 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4837 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4838 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4839 |
" |
8874 | 4840 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4841 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4842 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4843 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4844 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4845 |
|
3003 | 4846 |
replaceAny:aCollection by:newObject |
4847 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4848 |
Pleace do no longer use this one; |
3003 | 4849 |
instead use #replaceAny:with: for naming consistence." |
4850 |
||
5872 | 4851 |
<resource:#obsolete> |
4852 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4853 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4854 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4855 |
|
4856 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4857 |
args: aCollection : <colleciton of <object> > |
8874 | 4858 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4859 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4860 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4861 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4862 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4863 |
" |
3003 | 4864 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4865 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4866 |
" |
|
4867 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4868 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4869 |
! |
4870 |
||
3003 | 4871 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4872 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4873 |
Pleace do no longer use this one; |
3003 | 4874 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4875 |
|
5872 | 4876 |
<resource:#obsolete> |
4877 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4878 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4879 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4880 |
|
4881 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4882 |
args: aCollection : <colleciton of <object> > |
8874 | 4883 |
newObject : <object> |
4884 |
startIndex : <integer> |
|
4885 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4886 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4887 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4888 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4889 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4890 |
" |
8874 | 4891 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4892 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4893 |
" |
4894 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4895 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4896 |
! |
4897 |
||
4898 |
replaceAny:aCollection with:newObject |
|
4899 |
"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
|
4900 |
|
2927 | 4901 |
Notice: This operation modifies the receiver, NOT a copy; |
4902 |
therefore the change may affect all others referencing the receiver." |
|
4903 |
||
8874 | 4904 |
^ self |
10439 | 4905 |
replaceAny:aCollection |
4906 |
with:newObject |
|
4907 |
from:1 |
|
4908 |
to:self size |
|
4909 |
||
4910 |
" |
|
4911 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
4912 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
4913 |
" |
|
4914 |
||
4915 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 4916 |
! |
4917 |
||
2927 | 4918 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4919 |
"replace all elements within a range, |
4920 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4921 |
|
362 | 4922 |
Notice: This operation modifies the receiver, NOT a copy; |
4923 |
therefore the change may affect all others referencing the receiver." |
|
4924 |
||
4925 |
|start "{ Class: SmallInteger }" |
|
4926 |
stop "{ Class: SmallInteger }"| |
|
4927 |
||
4928 |
start := startIndex. |
|
4929 |
stop := stopIndex. |
|
4930 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4931 |
(aCollection includes:(self at:index)) ifTrue:[ |
4932 |
self at:index put:newObject |
|
4933 |
] |
|
95 | 4934 |
] |
4935 |
||
4936 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4937 |
args: aCollection : <colleciton of <object> > |
8874 | 4938 |
newObject : <object> |
4939 |
startIndex : <integer> |
|
4940 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4941 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4942 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4943 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4944 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4945 |
" |
8874 | 4946 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4947 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4948 |
" |
3468
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 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4951 |
! |
4952 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4953 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4954 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4955 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4956 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4957 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4958 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4959 |
|
8874 | 4960 |
^ self |
4961 |
replaceFrom:startIndex |
|
4962 |
to:(startIndex + numberOfElements - 1) |
|
4963 |
with:replacementCollection |
|
4964 |
startingAt:1 |
|
4965 |
||
4966 |
" |
|
4967 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4968 |
#($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
|
4969 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4970 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4971 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4972 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4973 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4974 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4975 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4976 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4977 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4978 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4979 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4980 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4981 |
^ self |
8874 | 4982 |
replaceFrom:startIndex |
4983 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4984 |
with:replacementCollection |
|
4985 |
startingAt:repStartIndex |
|
4986 |
||
4987 |
" |
|
4988 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4989 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4990 |
#($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
|
4991 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4992 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4993 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4994 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4995 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4996 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4997 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 4998 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4999 |
|
44 | 5000 |
Notice: This operation modifies the receiver, NOT a copy; |
5001 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5002 |
|
8874 | 5003 |
^ self |
5004 |
replaceFrom:startIndex |
|
5005 |
to:stopIndex |
|
5006 |
with:replacementCollection |
|
5007 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5008 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5009 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5010 |
args: startIndex : <integer> |
8874 | 5011 |
stopIndex : <integer> |
5012 |
replacementCollection : <colleciton of <object> > |
|
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5015 |
" |
61 | 5016 |
|
95 | 5017 |
" |
5018 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
5019 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
5020 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5021 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5022 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 5023 |
! |
5024 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5025 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5026 |
"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
|
5027 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 5028 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5029 |
|
44 | 5030 |
Notice: This operation modifies the receiver, NOT a copy; |
5031 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5032 |
|
5033 |
|srcIndex "{ Class: SmallInteger }" |
|
5034 |
dstIndex "{ Class: SmallInteger }" |
|
5035 |
end "{ Class: SmallInteger }" | |
|
5036 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5037 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 5038 |
" must do reverse copy " |
5039 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
5040 |
dstIndex := stopIndex. |
|
5041 |
end := startIndex. |
|
5042 |
[dstIndex >= end] whileTrue:[ |
|
5043 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
5044 |
srcIndex := srcIndex - 1. |
|
5045 |
dstIndex := dstIndex - 1 |
|
5046 |
]. |
|
5047 |
^ self |
|
1 | 5048 |
]. |
5049 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5050 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5051 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5052 |
end := stopIndex. |
1 | 5053 |
[dstIndex <= end] whileTrue:[ |
8874 | 5054 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
5055 |
srcIndex := srcIndex + 1. |
|
5056 |
dstIndex := dstIndex + 1 |
|
1 | 5057 |
] |
61 | 5058 |
|
95 | 5059 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5060 |
args: startIndex : <integer> |
8874 | 5061 |
stopIndex : <integer> |
5062 |
replacementCollection : <colleciton of <object> > |
|
5063 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5064 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5065 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5066 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5067 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5068 |
" |
8874 | 5069 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
5070 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
5071 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 5072 |
|
5073 |
|c| |
|
5074 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5075 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 5076 |
|
5077 |
|c| |
|
5078 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5079 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 5080 |
" |
3234 | 5081 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5082 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 5083 |
! |
5084 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5085 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5086 |
"replace elements in the receiver starting at startIndex, |
602 | 5087 |
with elements taken from replacementCollection starting at 1 |
5088 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5089 |
|
602 | 5090 |
Notice: This operation modifies the receiver, NOT a copy; |
5091 |
therefore the change may affect all others referencing the receiver." |
|
5092 |
||
8874 | 5093 |
^ self |
5094 |
replaceFrom:startIndex |
|
5095 |
to:(startIndex + replacementCollection size - 1) |
|
5096 |
with:replacementCollection |
|
5097 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5098 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5099 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5100 |
args: startIndex : <integer> |
8874 | 5101 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5102 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5103 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5104 |
" |
602 | 5105 |
|
5106 |
" |
|
5107 |
'1234567890' replaceFrom:5 with:'abc' |
|
5108 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
5109 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5110 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5111 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 5112 |
! |
5113 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5114 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5115 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5116 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 5117 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5118 |
|
602 | 5119 |
Notice: This operation modifies the receiver, NOT a copy; |
5120 |
therefore the change may affect all others referencing the receiver." |
|
5121 |
||
8874 | 5122 |
^ self |
5123 |
replaceFrom:startIndex |
|
5124 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
5125 |
with:replacementCollection |
|
5126 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5127 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5128 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5129 |
args: startIndex : <integer> |
8874 | 5130 |
replacementCollection : <colleciton of <object> > |
5131 |
offset : <integer> |
|
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5134 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5135 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5136 |
" |
8874 | 5137 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 5138 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
5139 |
" |
|
3468
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 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 5142 |
! |
5143 |
||
61 | 5144 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
5145 |
"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
|
5146 |
|
61 | 5147 |
Notice: This operation modifies the destination collection, NOT a copy; |
5148 |
therefore the change may affect all others referencing this object." |
|
5149 |
||
8874 | 5150 |
destColl |
5151 |
replaceFrom:destStartIndex |
|
5152 |
to:destEndIndex |
|
5153 |
with:self |
|
5154 |
startingAt:sourceStart |
|
61 | 5155 |
|
5156 |
" |
|
5157 |
|s| |
|
5158 |
s := 'abcdefghijklmnop'. |
|
5159 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
5160 |
s'123defghijklmnop' |
|
5161 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5162 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5163 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 5164 |
! ! |
5165 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5166 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5167 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5168 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5169 |
"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
|
5170 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5171 |
Elements on the left are filled with padElement. |
8874 | 5172 |
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
|
5173 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5174 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5175 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5176 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5177 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5178 |
(len < size) ifTrue:[ |
8874 | 5179 |
s := self species new:size withAll:padElement. |
5180 |
s replaceFrom:(size - len + 1) with:self. |
|
5181 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5182 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5183 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5184 |
" |
8874 | 5185 |
'foo' leftPaddedTo:10 with:$. |
5186 |
'fooBar' leftPaddedTo:5 with:$. |
|
5187 |
123 printString leftPaddedTo:10 with:$. |
|
5188 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
5189 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
5190 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
5191 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
5192 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
5193 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
5194 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5195 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5196 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5197 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5198 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5199 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5200 |
plus pad elements up to length. |
8874 | 5201 |
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
|
5202 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5203 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5204 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5205 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5206 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5207 |
len < newSize ifTrue:[ |
8874 | 5208 |
s := self species new:newSize withAll:padElement. |
5209 |
s replaceFrom:1 to:len with:self. |
|
5210 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5211 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5212 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5213 |
" |
8874 | 5214 |
'foo' paddedTo:10 with:$. |
5215 |
123 printString paddedTo:10 with:$* |
|
5216 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
5217 |
(Float pi printString) paddedTo:15 with:$- |
|
5218 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
5219 |
#[1 2 3 4] paddedTo:6 with:0 |
|
5220 |
#[1 2 3 4] paddedTo:10 with:99 |
|
5221 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5222 |
" |
7116 | 5223 |
! |
5224 |
||
5225 |
paddedToMultipleOf:sizeModulu with:padElement |
|
5226 |
|mySize mod numPad| |
|
5227 |
||
5228 |
mySize := self size. |
|
5229 |
mod := (mySize \\ sizeModulu). |
|
5230 |
mod == 0 ifTrue:[^ self]. |
|
5231 |
||
5232 |
numPad := sizeModulu - mod. |
|
5233 |
^ self , (self species new:numPad withAll:padElement). |
|
5234 |
||
5235 |
" |
|
8874 | 5236 |
#[] paddedToMultipleOf:3 with:0 |
5237 |
#[1] paddedToMultipleOf:3 with:0 |
|
5238 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
5239 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 5240 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
5241 |
||
5242 |
#() paddedToMultipleOf:3 with:nil |
|
5243 |
#(1) paddedToMultipleOf:3 with:nil |
|
5244 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
5245 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
5246 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
5247 |
||
5248 |
'' paddedToMultipleOf:3 with:Character space |
|
5249 |
'a' paddedToMultipleOf:3 with:Character space |
|
5250 |
'ab' paddedToMultipleOf:3 with:Character space |
|
5251 |
'abc' paddedToMultipleOf:3 with:Character space |
|
5252 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
5253 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5254 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5255 |
|
7260 | 5256 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 5257 |
|
5358 | 5258 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 5259 |
"Private!! |
5358 | 5260 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
5261 |
||
5262 |
| i1 i2 val1 val2 out | |
|
5263 |
||
5264 |
i1 := first. |
|
5265 |
i2 := middle + 1. |
|
5266 |
val1 := self at: i1. |
|
5267 |
val2 := self at: i2. |
|
5268 |
out := first - 1. "will be pre-incremented" |
|
5269 |
||
5270 |
"select 'lower' half of the elements based on comparator" |
|
5271 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 5272 |
(aBlock value: val1 value: val2) |
5273 |
ifTrue: [ |
|
5274 |
dst at: (out := out + 1) put: val1. |
|
5275 |
val1 := self at: (i1 := i1 + 1)] |
|
5276 |
ifFalse: [ |
|
5277 |
dst at: (out := out + 1) put: val2. |
|
5278 |
i2 := i2 + 1. |
|
5279 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 5280 |
|
5281 |
"copy the remaining elements" |
|
5282 |
i1 <= middle |
|
8874 | 5283 |
ifTrue: [ |
5284 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
5285 |
ifFalse: [ |
|
5286 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 5287 |
|
5288 |
! |
|
5289 |
||
5290 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
5291 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
5292 |
||
5293 |
| middle | |
|
5294 |
||
5295 |
first = last ifTrue: [^ self]. |
|
5296 |
middle := (first + last) // 2. |
|
5297 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
5298 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
5299 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
5300 |
||
5301 |
||
5302 |
! |
|
5303 |
||
2069 | 5304 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5305 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5306 |
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
|
5307 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5308 |
|
8874 | 5309 |
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
|
5310 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5311 |
|
5312 |
|begin "{ Class: SmallInteger }" |
|
5313 |
end "{ Class: SmallInteger }" |
|
5314 |
b "{ Class: SmallInteger }" |
|
5315 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5316 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5317 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5318 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5319 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5320 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5321 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5322 |
[stack notEmpty] whileTrue:[ |
8874 | 5323 |
end := stack removeLast. |
5324 |
begin := stack removeLast. |
|
5325 |
||
5326 |
b := begin. |
|
5327 |
e := end. |
|
5328 |
middleElement := self at:((b + e) // 2). |
|
5329 |
||
5330 |
[b < e] whileTrue:[ |
|
5331 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5332 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5333 |
||
5334 |
(b <= e) ifTrue:[ |
|
5335 |
(b == e) ifFalse:[ |
|
5336 |
temp1 := self at:b. temp2 := self at:e. |
|
5337 |
self at:b put:temp2. self at:e put:temp1 |
|
5338 |
]. |
|
5339 |
b := b + 1. |
|
5340 |
e := e - 1 |
|
5341 |
] |
|
5342 |
]. |
|
5343 |
(begin < e) ifTrue:[ |
|
5344 |
stack add:begin. |
|
5345 |
stack add:e. |
|
5346 |
]. |
|
5347 |
(b < end) ifTrue:[ |
|
5348 |
stack add:b. |
|
5349 |
stack add:end. |
|
5350 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5351 |
] |
2069 | 5352 |
! |
5353 |
||
5354 |
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
|
5355 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5356 |
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
|
5357 |
with degenerated collections." |
2069 | 5358 |
|
5359 |
|begin "{ Class: SmallInteger }" |
|
5360 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5361 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5362 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5363 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5364 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5365 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5366 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5367 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5368 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5369 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5370 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5371 |
[stack notEmpty] whileTrue:[ |
8874 | 5372 |
end := stack removeLast. |
5373 |
begin := stack removeLast. |
|
5374 |
||
5375 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5376 |
elB := self at:begin. |
|
5377 |
elE := self at:end. |
|
5378 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5379 |
temp := elB. elB := elE. elE := temp. |
|
5380 |
self at:begin put:elB. |
|
5381 |
self at:end put:elE. |
|
5382 |
]. |
|
5383 |
||
5384 |
nToSort > 2 ifTrue:[ |
|
5385 |
m := (begin + end) // 2. |
|
5386 |
elM := self at:m. |
|
5387 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5388 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5389 |
temp := self at:end. |
|
5390 |
self at:end put:(self at:m). |
|
5391 |
self at:m put:temp. |
|
5392 |
elM := elE |
|
5393 |
] |
|
5394 |
] ifFalse:[ |
|
5395 |
temp := self at:begin. |
|
5396 |
self at:begin put:(self at:m). |
|
5397 |
self at:m put:temp. |
|
5398 |
elM := elB |
|
5399 |
]. |
|
5400 |
nToSort > 3 ifTrue:[ |
|
5401 |
bRun := begin. |
|
5402 |
eRun := end. |
|
5403 |
[ |
|
5404 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5405 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5406 |
bRun <= eRun |
|
5407 |
] whileTrue:[ |
|
5408 |
temp := self at:bRun. |
|
5409 |
self at:bRun put:(self at:eRun). |
|
5410 |
self at:eRun put:temp. |
|
5411 |
]. |
|
5412 |
stack add:begin. |
|
5413 |
stack add:eRun. |
|
5414 |
||
5415 |
stack add:bRun. |
|
5416 |
stack add:end. |
|
5417 |
] |
|
5418 |
] |
|
5419 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5420 |
] |
2069 | 5421 |
! |
5422 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5423 |
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
|
5424 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5425 |
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
|
5426 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5427 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5428 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5429 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5430 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5431 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5432 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5433 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5434 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5435 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5436 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5437 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5438 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5439 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5440 |
[stack notEmpty] whileTrue:[ |
8874 | 5441 |
end := stack removeLast. |
5442 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5443 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5444 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 5445 |
elB := self at:begin. |
5446 |
elE := self at:end. |
|
5447 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
5448 |
temp := elB. elB := elE. elE := temp. |
|
5449 |
self at:begin put:elB. |
|
5450 |
self at:end put:elE. |
|
5451 |
]. |
|
5452 |
||
5453 |
nToSort > 2 ifTrue:[ |
|
5454 |
m := (begin + end) // 2. |
|
5455 |
elM := self at:m. |
|
5456 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
5457 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
5458 |
temp := self at:end. |
|
5459 |
self at:end put:(self at:m). |
|
5460 |
self at:m put:temp. |
|
5461 |
elM := elE |
|
5462 |
] |
|
5463 |
] ifFalse:[ |
|
5464 |
temp := self at:begin. |
|
5465 |
self at:begin put:(self at:m). |
|
5466 |
self at:m put:temp. |
|
5467 |
elM := elB |
|
5468 |
]. |
|
5469 |
nToSort > 3 ifTrue:[ |
|
5470 |
bRun := begin. |
|
5471 |
eRun := end. |
|
5472 |
[ |
|
5473 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
5474 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
5475 |
bRun <= eRun |
|
5476 |
] whileTrue:[ |
|
5477 |
temp := self at:bRun. |
|
5478 |
self at:bRun put:(self at:eRun). |
|
5479 |
self at:eRun put:temp. |
|
5480 |
]. |
|
5481 |
stack add:begin. |
|
5482 |
stack add:eRun. |
|
5483 |
||
5484 |
stack add:bRun. |
|
5485 |
stack add:end. |
|
5486 |
] |
|
5487 |
] |
|
5488 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5489 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5490 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5491 |
|
2069 | 5492 |
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
|
5493 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5494 |
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
|
5495 |
with degenerated collections." |
2069 | 5496 |
|
5497 |
|begin "{ Class: SmallInteger }" |
|
5498 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5499 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5500 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5501 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5502 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5503 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5504 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5505 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5506 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5507 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5508 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5509 |
[stack notEmpty] whileTrue:[ |
8874 | 5510 |
end := stack removeLast. |
5511 |
begin := stack removeLast. |
|
5512 |
||
5513 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5514 |
||
5515 |
elB := self at:begin. |
|
5516 |
elE := self at:end. |
|
5517 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5518 |
temp := elB. elB := elE. elE := temp. |
|
5519 |
self at:begin put:elB. |
|
5520 |
self at:end put:elE. |
|
5521 |
||
5522 |
temp := aCollection at:end. |
|
5523 |
aCollection at:end put:(aCollection at:begin). |
|
5524 |
aCollection at:begin put:temp. |
|
5525 |
]. |
|
5526 |
||
5527 |
nToSort > 2 ifTrue:[ |
|
5528 |
m := (begin + end) // 2. |
|
5529 |
elM := self at:m. |
|
5530 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5531 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5532 |
temp := self at:end. |
|
5533 |
self at:end put:(self at:m). |
|
5534 |
self at:m put:temp. |
|
5535 |
||
5536 |
temp := aCollection at:end. |
|
5537 |
aCollection at:end put:(aCollection at:m). |
|
5538 |
aCollection at:m put:temp. |
|
5539 |
||
5540 |
elM := elE |
|
5541 |
] |
|
5542 |
] ifFalse:[ |
|
5543 |
temp := self at:begin. |
|
5544 |
self at:begin put:(self at:m). |
|
5545 |
self at:m put:temp. |
|
5546 |
||
5547 |
temp := aCollection at:begin. |
|
5548 |
aCollection at:begin put:(aCollection at:m). |
|
5549 |
aCollection at:m put:temp. |
|
5550 |
elM := elB |
|
5551 |
]. |
|
5552 |
nToSort > 3 ifTrue:[ |
|
5553 |
bRun := begin. |
|
5554 |
eRun := end. |
|
5555 |
[ |
|
5556 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5557 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5558 |
bRun <= eRun |
|
5559 |
] whileTrue:[ |
|
5560 |
temp := self at:bRun. |
|
5561 |
self at:bRun put:(self at:eRun). |
|
5562 |
self at:eRun put:temp. |
|
5563 |
||
5564 |
temp := aCollection at:bRun. |
|
5565 |
aCollection at:bRun put:(aCollection at:eRun). |
|
5566 |
aCollection at:eRun put:temp. |
|
5567 |
]. |
|
5568 |
stack add:begin. |
|
5569 |
stack add:eRun. |
|
5570 |
||
5571 |
stack add:bRun. |
|
5572 |
stack add:end. |
|
5573 |
] |
|
5574 |
] |
|
5575 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5576 |
] |
2069 | 5577 |
! |
5578 |
||
5579 |
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
|
5580 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5581 |
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
|
5582 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5583 |
|
8874 | 5584 |
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
|
5585 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5586 |
|
5587 |
|begin "{ Class: SmallInteger }" |
|
5588 |
end "{ Class: SmallInteger }" |
|
5589 |
b "{ Class: SmallInteger }" |
|
5590 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5591 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5592 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5593 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5594 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5595 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5596 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5597 |
[stack notEmpty] whileTrue:[ |
8874 | 5598 |
end := stack removeLast. |
5599 |
begin := stack removeLast. |
|
5600 |
||
5601 |
b := begin. |
|
5602 |
e := end. |
|
5603 |
middleElement := self at:((b + e) // 2). |
|
5604 |
||
5605 |
[b < e] whileTrue:[ |
|
5606 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5607 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5608 |
||
5609 |
(b <= e) ifTrue:[ |
|
5610 |
(b == e) ifFalse:[ |
|
5611 |
temp1 := self at:b. temp2 := self at:e. |
|
5612 |
self at:b put:temp2. self at:e put:temp1. |
|
5613 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5614 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5615 |
]. |
|
5616 |
b := b + 1. |
|
5617 |
e := e - 1 |
|
5618 |
] |
|
5619 |
]. |
|
5620 |
(begin < e) ifTrue:[ |
|
5621 |
stack add:begin. |
|
5622 |
stack add:e. |
|
5623 |
]. |
|
5624 |
(b < end) ifTrue:[ |
|
5625 |
stack add:b. |
|
5626 |
stack add:end. |
|
5627 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5628 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5629 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5630 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5631 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5632 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5633 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5634 |
the worst case O-square situation of quickSort. |
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 |
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
|
5637 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5638 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5639 |
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
|
5640 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5641 |
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
|
5642 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5643 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5644 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5645 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5646 |
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
|
5647 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5648 |
"/ 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
|
5649 |
^ 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
|
5650 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5651 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5652 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5653 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5654 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5655 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5656 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5657 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5658 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5659 |
"/ 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
|
5660 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5661 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5662 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5663 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5664 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5665 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5666 |
"/ "/ 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
|
5667 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5668 |
"/ 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
|
5669 |
"/ 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
|
5670 |
"/ 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
|
5671 |
"/ aCollection notNil ifTrue:[ |
8874 | 5672 |
"/ 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
|
5673 |
"/ 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
|
5674 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5675 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5676 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5677 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5678 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5679 |
"/ 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
|
5680 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5681 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5682 |
"/ [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
|
5683 |
"/ [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
|
5684 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5685 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5686 |
"/ (b == e) ifFalse:[ |
8874 | 5687 |
"/ 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
|
5688 |
"/ 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
|
5689 |
"/ aCollection notNil ifTrue:[ |
8874 | 5690 |
"/ 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
|
5691 |
"/ 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
|
5692 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5693 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5694 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5695 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5696 |
"/ ] |
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 |
"/ (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
|
5699 |
"/ (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
|
5700 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5701 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5702 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5703 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5704 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5705 |
the worst case O-square situation of quickSort. |
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 |
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
|
5708 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5709 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5710 |
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
|
5711 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5712 |
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
|
5713 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5714 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5715 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5716 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5717 |
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
|
5718 |
"/ 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
|
5719 |
^ 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
|
5720 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5721 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5722 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5723 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5724 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5725 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5726 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5727 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5728 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5729 |
"/ 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
|
5730 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5731 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5732 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5733 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5734 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5735 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5736 |
"/ "/ 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
|
5737 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5738 |
"/ 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
|
5739 |
"/ 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
|
5740 |
"/ 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
|
5741 |
"/ aCollection notNil ifTrue:[ |
8874 | 5742 |
"/ 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
|
5743 |
"/ 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
|
5744 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5745 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5746 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5747 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5748 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5749 |
"/ 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
|
5750 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5751 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5752 |
"/ [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
|
5753 |
"/ [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
|
5754 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5755 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5756 |
"/ (b == e) ifFalse:[ |
8874 | 5757 |
"/ 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
|
5758 |
"/ 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
|
5759 |
"/ aCollection notNil ifTrue:[ |
8874 | 5760 |
"/ 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
|
5761 |
"/ 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
|
5762 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5763 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5764 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5765 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5766 |
"/ ] |
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 |
"/ (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
|
5769 |
"/ (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
|
5770 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5771 |
"/ "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
|
5772 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5773 |
! ! |
5774 |
||
602 | 5775 |
!SequenceableCollection methodsFor:'queries'! |
5776 |
||
5777 |
firstIndex |
|
5778 |
"return the first elements index" |
|
5779 |
||
5780 |
^ 1 |
|
5781 |
! |
|
5782 |
||
5783 |
isSequenceable |
|
5784 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5785 |
||
5786 |
^ true |
|
1 | 5787 |
! |
5788 |
||
602 | 5789 |
isSequenceableCollection |
5790 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5791 |
This method is a historic leftover and will be removed." |
|
5792 |
||
5872 | 5793 |
<resource:#obsolete> |
5794 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5795 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5796 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5797 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5798 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5799 |
! |
5800 |
||
5358 | 5801 |
isSorted |
5802 |
"return true. if my elements are sorted (already)" |
|
5803 |
||
5804 |
|lastEl el| |
|
5805 |
||
5806 |
lastEl := self first. |
|
5807 |
2 to:self size do:[:i | |
|
8874 | 5808 |
el := self at:i. |
5809 |
el >= lastEl ifFalse: [^ false]. |
|
5810 |
lastEl := el |
|
5358 | 5811 |
]. |
5812 |
^ true |
|
5813 |
||
5814 |
" |
|
8874 | 5815 |
#(1 2 3 5 10 100) isSorted |
5816 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5817 |
" |
5818 |
! |
|
5819 |
||
5820 |
isSortedBy:aBlock |
|
5821 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5822 |
||
5823 |
|lastEl el| |
|
5824 |
||
5825 |
lastEl := self first. |
|
5826 |
2 to:self size do:[:i | |
|
8874 | 5827 |
el := self at:i. |
5828 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5829 |
lastEl := el |
|
5358 | 5830 |
]. |
5831 |
^ true |
|
5832 |
||
5833 |
" |
|
8874 | 5834 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5835 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5836 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5837 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5838 |
" |
5839 |
||
5840 |
! |
|
5841 |
||
602 | 5842 |
keys |
5843 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5844 |
||
5845 |
|sz| |
|
5846 |
||
1 | 5847 |
sz := self size. |
602 | 5848 |
sz == 0 ifTrue:[ |
8874 | 5849 |
^ #() |
61 | 5850 |
]. |
602 | 5851 |
^ 1 to:sz |
1 | 5852 |
! |
5853 |
||
602 | 5854 |
lastIndex |
5855 |
"return the last elements index" |
|
5856 |
||
5857 |
^ self size |
|
61 | 5858 |
! |
5859 |
||
602 | 5860 |
size |
5861 |
"return the number of elements in the collection. |
|
5862 |
concrete implementations must define this" |
|
5863 |
||
5864 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5865 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5866 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5867 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5868 |
"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
|
5869 |
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
|
5870 |
ST-compatibility" |
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 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5873 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5874 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5875 |
! ! |
5876 |
||
12647 | 5877 |
|
1 | 5878 |
!SequenceableCollection methodsFor:'searching'! |
5879 |
||
3887 | 5880 |
detect:aBlock startingAt:startIndex |
5881 |
"find the first element, for which evaluation of the argument, aBlock |
|
5882 |
return true, starting the search with startIndex. |
|
5883 |
If none does so, report an error" |
|
5884 |
||
5885 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5886 |
||
5887 |
" |
|
8874 | 5888 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5889 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5890 |
" |
5891 |
||
5892 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5893 |
! |
|
5894 |
||
5895 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5896 |
"find the first element, for which evaluation of the argument, aBlock |
|
5897 |
return true, starting the search with startIndex. |
|
5717 | 5898 |
If none does so, return the evaluation of exceptionBlock" |
5899 |
||
5900 |
|stop| |
|
3887 | 5901 |
|
5902 |
stop := self size. |
|
5717 | 5903 |
self from:startIndex to:stop do:[:el | |
8874 | 5904 |
(aBlock value:el) ifTrue:[ |
5905 |
^ el |
|
5906 |
]. |
|
3887 | 5907 |
]. |
5908 |
^ exceptionBlock value |
|
5909 |
||
5910 |
" |
|
8874 | 5911 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5912 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5913 |
" |
5914 |
||
5915 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5916 |
! |
|
5917 |
||
11705 | 5918 |
findFirst:aBlock ifNone:exceptionalValue |
10453 | 5919 |
"find the index of the first element, for which evaluation of the argument, aBlock |
11705 | 5920 |
returns true; return its index or the value from exceptionalValue if none detected. |
3887 | 5921 |
This is much like #detect, however, here an INDEX is returned, |
5922 |
while #detect returns the element." |
|
602 | 5923 |
|
5924 |
|stop "{ Class: SmallInteger }" | |
|
5925 |
||
32 | 5926 |
stop := self size. |
602 | 5927 |
1 to:stop do:[:index | |
10453 | 5928 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5929 |
]. |
11705 | 5930 |
^ exceptionalValue value |
5931 |
||
5932 |
" |
|
5933 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
5934 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
5935 |
" |
|
1 | 5936 |
! |
5937 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5938 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5939 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5940 |
returns true; return its index or 0 if none detected. |
5941 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5942 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5943 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5944 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5945 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5946 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5947 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5948 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5949 |
start to:stop do:[:index | |
8874 | 5950 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5951 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5952 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5953 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5954 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5955 |
#(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
|
5956 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5957 |
" |
3887 | 5958 |
|
5959 |
"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
|
5960 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5961 |
|
602 | 5962 |
findLast:aBlock |
5963 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5964 |
returns true. |
5965 |
Return its index or 0 if none detected." |
|
602 | 5966 |
|
5967 |
|start "{ Class: SmallInteger }"| |
|
5968 |
||
5969 |
start := self size. |
|
5970 |
start to:1 by:-1 do:[:index | |
|
8874 | 5971 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5972 |
]. |
5973 |
^ 0 |
|
5974 |
||
5975 |
" |
|
602 | 5976 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5977 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5978 |
" |
5979 |
! |
|
5980 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5981 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5982 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5983 |
returns true. Start the search at startIndex. |
5984 |
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
|
5985 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5986 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5987 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5988 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5989 |
start to:1 by:-1 do:[:index | |
8874 | 5990 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
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 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5993 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5994 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5995 |
#(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
|
5996 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
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 |
|
8193 | 6000 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
6001 |
"find the last element, for which evaluation of the argument, aBlock |
|
6002 |
returns true. Start the search at startIndex. |
|
6003 |
End the search at endIndex or when an element is found. |
|
6004 |
Return its index or 0 if none detected." |
|
6005 |
||
6006 |
|start "{ Class: SmallInteger }"| |
|
6007 |
||
6008 |
start := startIndex. |
|
6009 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 6010 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 6011 |
]. |
6012 |
^ 0 |
|
6013 |
||
6014 |
" |
|
6015 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
6016 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
6017 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
6018 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
6019 |
" |
|
6020 |
! |
|
6021 |
||
159 | 6022 |
indexOfAny:aCollection |
6023 |
"search the collection for an element in aCollection. |
|
6024 |
if found, return the index otherwise return 0. |
|
8874 | 6025 |
The comparison is done using = |
159 | 6026 |
(i.e. equality test - not identity test). |
6027 |
||
6028 |
Notice, that for big collections, the runtime of this search |
|
6029 |
grows proportional to size(receiver) * size(aCollection). |
|
6030 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6031 |
||
6032 |
^ self indexOfAny:aCollection startingAt:1 |
|
6033 |
||
6034 |
" |
|
6035 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
6036 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
6037 |
" |
|
6038 |
! |
|
6039 |
||
6040 |
indexOfAny:aCollection startingAt:start |
|
6041 |
"search the collection for an element in aCollection, |
|
6042 |
starting the search at index start; |
|
6043 |
if found, return the index otherwise return 0. |
|
8874 | 6044 |
The comparison is done using = |
159 | 6045 |
(i.e. equality test - not identity test). |
6046 |
||
6047 |
Notice, that for big collections, the runtime of this search |
|
6048 |
grows proportional to size(receiver) * size(aCollection). |
|
6049 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6050 |
||
6051 |
||
6052 |
|startIndex "{ Class: SmallInteger }" |
|
6053 |
stop "{ Class: SmallInteger }" | |
|
6054 |
||
6055 |
startIndex := start. |
|
6056 |
stop := self size. |
|
6057 |
startIndex to:stop do:[:index | |
|
8874 | 6058 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 6059 |
]. |
6060 |
^ 0 |
|
6061 |
" |
|
8874 | 6062 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
6063 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 6064 |
" |
1 | 6065 |
! |
6066 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6067 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6068 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6069 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6070 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 6071 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6072 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6073 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6074 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6075 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6076 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
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 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6079 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6080 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6081 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6082 |
val == 0 ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 6086 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
6087 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
6088 |
#(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
|
6089 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6090 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6091 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6092 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6093 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6094 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6095 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6096 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6097 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6098 |
#(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
|
6099 |
#(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
|
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 |
|
8874 | 6103 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6104 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
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 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6107 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6108 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6109 |
#(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
|
6110 |
#(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
|
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 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6114 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6115 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
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 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6118 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6119 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6120 |
|
368 | 6121 |
indexOfSubCollection:aCollection |
9234 | 6122 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 6123 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6124 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 6125 |
|
6126 |
" |
|
8874 | 6127 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
6128 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
6129 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
6130 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
6131 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
6132 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
6133 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
6134 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 6135 |
" |
6136 |
! |
|
6137 |
||
602 | 6138 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
6139 |
"find a subcollection. If found, return the index; |
|
6140 |
if not found, return the result of evaluating exceptionBlock." |
|
6141 |
||
6142 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
6143 |
! |
|
6144 |
||
368 | 6145 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6146 |
"find a subcollection starting at index. |
368 | 6147 |
If found, return the index; if not found, return 0." |
6148 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6149 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 6150 |
! |
6151 |
||
6152 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
6153 |
"find a subcollection, starting at index. If found, return the index; |
|
6154 |
if not found, return the result of evaluating exceptionBlock. |
|
6155 |
This is a q&d hack - not very efficient" |
|
6156 |
||
8874 | 6157 |
|same first |
368 | 6158 |
mySize "{Class: SmallInteger }" |
6159 |
checkIndex "{Class: SmallInteger }" |
|
6160 |
cmpIndex "{Class: SmallInteger }" |
|
6161 |
sz "{Class: SmallInteger }"| |
|
6162 |
||
6163 |
mySize := self size. |
|
6164 |
sz := aCollection size. |
|
6165 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
6166 |
first := aCollection at:1. |
|
6167 |
||
8874 | 6168 |
checkIndex := startIndex - 1. |
368 | 6169 |
[true] whileTrue:[ |
8874 | 6170 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
6171 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6172 |
||
6173 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
6174 |
^ 0 |
|
6175 |
]. |
|
6176 |
same := true. |
|
6177 |
cmpIndex := 1. |
|
6178 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6179 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
6180 |
same := false |
|
6181 |
] ifTrue:[ |
|
6182 |
cmpIndex := cmpIndex + 1 |
|
6183 |
] |
|
6184 |
]. |
|
6185 |
same ifTrue:[^ checkIndex]. |
|
6186 |
] |
|
6187 |
||
6188 |
" |
|
6189 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
6190 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
6191 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
6192 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
6193 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
6194 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 6195 |
" |
602 | 6196 |
! |
6197 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6198 |
lastIndexOfSubCollection:aCollection |
8874 | 6199 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6200 |
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
|
6201 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6202 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6203 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6204 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6205 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6206 |
! |
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 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6209 |
"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
|
6210 |
if not found, return the result of evaluating exceptionBlock." |
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 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6213 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6214 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6215 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6216 |
! |
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 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6219 |
"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
|
6220 |
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
|
6221 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6222 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6223 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6224 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
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 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6227 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6228 |
"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
|
6229 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6230 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6231 |
|
8874 | 6232 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6233 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6234 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6235 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6236 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6237 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6238 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6239 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6240 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6241 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6242 |
|
8874 | 6243 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6244 |
checkIndex > mySize ifTrue:[ |
8874 | 6245 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6246 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6247 |
[true] whileTrue:[ |
8874 | 6248 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
6249 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6250 |
||
6251 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
6252 |
^ 0 |
|
6253 |
]. |
|
6254 |
same := true. |
|
6255 |
cmpIndex := 1. |
|
6256 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6257 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
6258 |
same := false |
|
6259 |
] ifTrue:[ |
|
6260 |
cmpIndex := cmpIndex + 1 |
|
6261 |
] |
|
6262 |
]. |
|
6263 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
6264 |
] |
|
6265 |
||
6266 |
" |
|
6267 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
6268 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
6269 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
6270 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
6271 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
6272 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
6273 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
6274 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
6275 |
#(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
|
6276 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6277 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6278 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6279 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6280 |
|
7264 | 6281 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6282 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6283 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6284 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6285 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6286 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6287 |
|
10194 | 6288 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6289 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6290 |
" |
8874 | 6291 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6292 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6293 |
returns: true - if found |
10194 | 6294 |
false - if not found |
8874 | 6295 |
" |
6296 |
||
6297 |
" |
|
6298 |
#(10 20 30 40 50 60 70) includes:99 |
|
6299 |
#(10 20 30 40 50 60 70) includes:40 |
|
6300 |
#(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
|
6301 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6302 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6303 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6304 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6305 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6306 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6307 |
"search the collection for anElement; |
602 | 6308 |
if found, return the index otherwise return 0. |
8874 | 6309 |
The comparison is done using = |
602 | 6310 |
(i.e. equality test - not identity test)." |
6311 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6312 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6313 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6314 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6315 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6316 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6317 |
returns: elementIndex - if found |
8874 | 6318 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6319 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6320 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6321 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6322 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6323 |
#(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
|
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 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6327 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6328 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6329 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
"search the collection for anElement; |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6331 |
if found, return the index; otherwise return the value of the exceptionBlock. |
8874 | 6332 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6333 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6335 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6336 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6337 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6338 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6342 |
args: anElement : <object> |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6343 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6344 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6345 |
returns: elementIndex - if found |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6346 |
exceptionBlock value - if not found |
8874 | 6347 |
" |
6348 |
||
6349 |
" |
|
6350 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
6351 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
6352 |
#(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
|
6353 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6354 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6355 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6356 |
! |
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 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 6359 |
"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
|
6360 |
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
|
6361 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6362 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6363 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6364 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6365 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6366 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6367 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 6368 |
self at:idx put:replacement. |
6369 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6370 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6371 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6372 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6373 |
" |
8874 | 6374 |
args: elementToFind : <object> |
6375 |
replacement : <integer> |
|
6376 |
start : <integer> |
|
6377 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6378 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6379 |
returns: elementIndex - if found (and replaced) |
8874 | 6380 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6381 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6384 |
|a| |
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 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6387 |
(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
|
6388 |
a printNL. |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6391 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6392 |
! |
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 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6395 |
"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
|
6396 |
if found, return the index otherwise return 0. |
8874 | 6397 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6398 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6399 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6400 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6401 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6402 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6403 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6404 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6405 |
startIndex to:stop do:[:index | |
8874 | 6406 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6407 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6408 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6409 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6410 |
" |
8874 | 6411 |
args: anElement : <object> |
6412 |
start : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6415 |
0 - if not found |
6416 |
" |
|
6417 |
||
6418 |
" |
|
6419 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
6420 |
#(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
|
6421 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6422 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6423 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6424 |
! |
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 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6427 |
"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
|
6428 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6429 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6430 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6431 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6432 |
|
602 | 6433 |
|startIndex "{ Class: SmallInteger }" |
6434 |
stopIndex "{ Class: SmallInteger }" | |
|
6435 |
||
6436 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6437 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6438 |
startIndex to:stopIndex do:[:index | |
8874 | 6439 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6440 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6441 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6442 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6443 |
" |
8874 | 6444 |
args: anElement : <object> |
6445 |
start : <integer> |
|
6446 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6447 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6448 |
returns: elementIndex - if found |
8874 | 6449 |
exceptionBlock value - if not found |
6450 |
" |
|
6451 |
||
6452 |
" |
|
6453 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
6454 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
6455 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
6456 |
#(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
|
6457 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6458 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6459 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6460 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
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 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6464 |
"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
|
6465 |
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
|
6466 |
exceptionBlock. |
8874 | 6467 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6468 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6469 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6470 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6471 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6472 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6473 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 6477 |
args: anElement : <object> |
6478 |
start : <integer> |
|
6479 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6480 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6481 |
returns: elementIndex - if found |
8874 | 6482 |
exceptionBlock value - if not found |
6483 |
" |
|
6484 |
||
6485 |
" |
|
6486 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
6487 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
6488 |
#(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
|
6489 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6490 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6491 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6492 |
! |
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 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6495 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6496 |
if found, return the index otherwise return 0. |
8874 | 6497 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6498 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6499 |
|
8874 | 6500 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6501 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6502 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6503 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6504 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6505 |
returns: elementIndex - if found |
8874 | 6506 |
0 - if not found |
6507 |
" |
|
6508 |
||
6509 |
" |
|
6510 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
6511 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
6512 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
6513 |
#(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
|
6514 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6515 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6516 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6517 |
! |
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 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6520 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6521 |
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
|
6522 |
exceptionBlock. |
8874 | 6523 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6524 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6525 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6526 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6527 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6528 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6529 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 6533 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
6534 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
6535 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
6536 |
#(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
|
6537 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6538 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6539 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6540 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6541 |
"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
|
6542 |
if found, return the index otherwise return 0. |
8874 | 6543 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6544 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6545 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6546 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6547 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6548 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6549 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6550 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 6551 |
]. |
6552 |
^ 0 |
|
6553 |
||
6554 |
" |
|
8874 | 6555 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
6556 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
6557 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
6558 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
6559 |
#(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
|
6560 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6561 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6562 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6563 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6564 |
"search the collection backwards for anElement starting the search at |
6565 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6566 |
otherwise return the value of the exceptionBlock. |
8874 | 6567 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6568 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6569 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6570 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6571 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6572 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6573 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 6577 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
6578 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6579 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6580 |
#(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
|
6581 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6582 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6583 |
|
4788 | 6584 |
lastIndexOfAny:aCollection |
6585 |
"search the collection backwards for any in aCollection; |
|
6586 |
if found, return the index, otherwise return 0. |
|
8874 | 6587 |
The comparison is done using = |
4788 | 6588 |
(i.e. equality test - not identity test)." |
6589 |
||
8874 | 6590 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 6591 |
|
6592 |
" |
|
6593 |
args: aCollection : <collection> |
|
6594 |
||
6595 |
returns: elementIndex - if found |
|
8874 | 6596 |
0 - if not found |
6597 |
" |
|
6598 |
||
6599 |
" |
|
6600 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 6601 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 6602 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
6603 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 6604 |
" |
6605 |
||
6606 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
6607 |
! |
|
6608 |
||
6609 |
lastIndexOfAny:aCollection startingAt:start |
|
6610 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
6611 |
if found, return the index, otherwise return 0. |
|
8874 | 6612 |
The comparison is done using = |
4788 | 6613 |
(i.e. equality test - not identity test)." |
6614 |
||
6615 |
|startIndex "{ Class: SmallInteger }"| |
|
6616 |
||
6617 |
startIndex := start. |
|
6618 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 6619 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 6620 |
]. |
6621 |
^ 0 |
|
6622 |
||
6623 |
" |
|
8874 | 6624 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
6625 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 6626 |
" |
6627 |
! |
|
6628 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6629 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6630 |
"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
|
6631 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6632 |
if found, return the index otherwise return nil. |
8874 | 6633 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6634 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6635 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6636 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6637 |
although this is somewhat strange (the indexOf-search |
6638 |
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
|
6639 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6640 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6641 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6642 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6643 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6644 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6645 |
startIndex to:stop do:[:index | |
8874 | 6646 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6647 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6648 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6649 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6650 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6651 |
args: anElement : <object> |
8874 | 6652 |
start : <integer> |
6653 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6656 |
nil - if not found |
6657 |
" |
|
6658 |
||
6659 |
" |
|
6660 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
6661 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
6662 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 6663 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6664 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6665 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 6666 |
! |
6667 |
||
6668 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6669 |
"search the collection for anElement, starting the search at index start |
|
6670 |
and stopping at stop; |
|
6671 |
if found, return the index otherwise return the value of the |
|
6672 |
exceptionBlock. |
|
8874 | 6673 |
The comparison is done using = |
602 | 6674 |
(i.e. equality test - not identity test)." |
6675 |
||
6676 |
|index| |
|
6677 |
||
6678 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6679 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 6680 |
^ index |
6681 |
||
6682 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6683 |
args: anElement : <object> |
8874 | 6684 |
start : <integer> |
6685 |
stop : <integer> |
|
6686 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6687 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6688 |
returns: elementIndex - if found |
8874 | 6689 |
valueImplementor value - if not found |
6690 |
" |
|
6691 |
||
6692 |
" |
|
6693 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6694 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6695 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6696 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6697 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6698 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 6699 |
! |
6700 |
||
6701 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6702 |
"search the collection for anElement, starting the search at index start; |
|
6703 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6704 |
If found (within the range), return the index, otherwise return nil. |
8874 | 6705 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6706 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6707 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6708 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6709 |
although this is somewhat strange (the indexOf-search |
6710 |
methods return 0), this is done for VW compatibility." |
|
2275 | 6711 |
|
6712 |
|startIndex "{ Class: SmallInteger }" |
|
6713 |
stopIndex "{ Class: SmallInteger }" | |
|
6714 |
||
6715 |
startIndex := startSearchIndex. |
|
6716 |
stopIndex := endSearchIndex max:1. |
|
6717 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 6718 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6719 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6720 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6721 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6722 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6723 |
args: anElement : <object> |
8874 | 6724 |
start : <integer> |
6725 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6728 |
nil - if not found |
6729 |
" |
|
6730 |
||
6731 |
" |
|
6732 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6733 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6734 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6735 |
" |
6736 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6737 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6738 |
! |
6739 |
||
6740 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6741 |
"search the collection for anElement, starting the search at index start |
|
6742 |
and stopping at stop, doing a reverse search; |
|
6743 |
if found, return the index otherwise return the value of the |
|
6744 |
exceptionBlock. |
|
8874 | 6745 |
The comparison is done using = |
2275 | 6746 |
(i.e. equality test - not identity test)." |
6747 |
||
6748 |
|index| |
|
6749 |
||
6750 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6751 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6752 |
^ index |
|
6753 |
||
6754 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6755 |
args: anElement : <object> |
8874 | 6756 |
start : <integer> |
6757 |
stop : <integer> |
|
6758 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6759 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6760 |
returns: elementIndex - if found |
8874 | 6761 |
valueImplementor value - if not found |
6762 |
" |
|
6763 |
||
6764 |
" |
|
6765 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6766 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6767 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6768 |
" |
6769 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6770 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6771 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6772 |
|
7264 | 6773 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6774 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6775 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6776 |
"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
|
6777 |
if found, return the index otherwise return 0." |
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 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6780 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6781 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6782 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6783 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6784 |
returns: elementIndex - if found |
8874 | 6785 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6786 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6789 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6790 |
#(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
|
6791 |
#(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
|
6792 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6793 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6794 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6795 |
#(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
|
6796 |
#(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
|
6797 |
|
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6800 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
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 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6804 |
"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
|
6805 |
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
|
6806 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6807 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6808 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6809 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6810 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6811 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6815 |
args: anElement : <object> |
8874 | 6816 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 6819 |
exceptionBlock value - if not found |
6820 |
" |
|
6821 |
||
6822 |
" |
|
6823 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6824 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6825 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6826 |
#(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
|
6827 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6828 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6829 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6830 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6831 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6832 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6833 |
"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
|
6834 |
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
|
6835 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6836 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6837 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6838 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6839 |
args: anElement : <object> |
8874 | 6840 |
anotherElement : <object> |
5520
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 |
returns: elementIndex - if found |
8874 | 6843 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6844 |
" |
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 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6847 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6848 |
"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
|
6849 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6850 |
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
|
6851 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6852 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6853 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6854 |
el | |
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 := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6857 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6858 |
startIndex to:stop do:[:index | |
8874 | 6859 |
el := self at:index. |
6860 |
anElement == el ifTrue:[^ index]. |
|
6861 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6862 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6863 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6864 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6865 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6866 |
args: anElement : <object> |
8874 | 6867 |
anotherElement : <object> |
6868 |
start : <integer> |
|
5520
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 |
returns: elementIndex - if found |
8874 | 6871 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6872 |
" |
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 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6875 |
#(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
|
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 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6879 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6880 |
"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
|
6881 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6882 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6883 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6884 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6885 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6886 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6887 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6888 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6889 |
startIndex to:stop do:[:index | |
8874 | 6890 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6891 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6892 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6893 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6894 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6895 |
args: anElement : <object> |
8874 | 6896 |
start : <integer> |
3468
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 |
returns: elementIndex - if found |
8874 | 6899 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6900 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6903 |
#(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
|
6904 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6905 |
#(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
|
6906 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6907 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6908 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6909 |
! |
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 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6912 |
"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
|
6913 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6914 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6915 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6916 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6917 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6918 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6919 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6920 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6921 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6922 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6923 |
startIndex to:stopIndex do:[:index | |
8874 | 6924 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6925 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6926 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6927 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6928 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6929 |
args: anElement : <object> |
8874 | 6930 |
start : <integer> |
6931 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6934 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6935 |
" |
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 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6938 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
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 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6942 |
"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
|
6943 |
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
|
6944 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6945 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6946 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6947 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6948 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6949 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6950 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6954 |
args: anElement : <object> |
8874 | 6955 |
start : <integer> |
6956 |
exceptionBlock : <valueImplementor> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6959 |
exceptionBlock value - if not found |
6960 |
" |
|
6961 |
||
6962 |
" |
|
6963 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6964 |
#(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
|
6965 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6966 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6967 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6968 |
! |
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 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6971 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6972 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6973 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6974 |
|
10194 | 6975 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6976 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6977 |
" |
8874 | 6978 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6979 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6980 |
returns: true - if found |
10194 | 6981 |
false - if not found |
8874 | 6982 |
" |
6983 |
||
6984 |
" |
|
6985 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6986 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6987 |
#(10 20 30 40 50 60 70) includes:40 |
|
6988 |
#(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
|
6989 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6990 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6991 |
|
8874 | 6992 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6993 |
#(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
|
6994 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6995 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6996 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6997 |
! |
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 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7000 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7001 |
if found, return the index otherwise return 0. |
8874 | 7002 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7003 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7004 |
|
8874 | 7005 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7006 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7007 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7008 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7009 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7010 |
returns: elementIndex - if found |
8874 | 7011 |
0 - if not found |
7012 |
" |
|
7013 |
||
7014 |
" |
|
7015 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
7016 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
7017 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
7018 |
#(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
|
7019 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7020 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7021 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7022 |
! |
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 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7025 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7026 |
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
|
7027 |
exceptionBlock. |
8874 | 7028 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7029 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7030 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7031 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7032 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7033 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7034 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7038 |
args: anElement : <object> |
8874 | 7039 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 7042 |
exceptionBlock value - if not found |
7043 |
" |
|
7044 |
||
7045 |
" |
|
7046 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
7047 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
7048 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
7049 |
#(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
|
7050 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7051 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7052 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7053 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
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 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7057 |
"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
|
7058 |
if found, return the index otherwise return 0. |
8874 | 7059 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7060 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7061 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7062 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7063 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7064 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7065 |
startIndex to:1 by:-1 do:[:index | |
8874 | 7066 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7069 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7070 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7071 |
args: anElement : <object> |
8874 | 7072 |
start : <integer> |
3468
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 |
returns: elementIndex - if found |
8874 | 7075 |
0 - if not found |
7076 |
" |
|
7077 |
||
7078 |
" |
|
7079 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
7080 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
7081 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
7082 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
7083 |
#(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
|
7084 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7085 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7086 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7087 |
! |
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 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 7090 |
"search the collection backwards for anElement starting the search at |
7091 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7092 |
otherwise return the value of the exceptionBlock. |
8874 | 7093 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7094 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7095 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7096 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7097 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7098 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7099 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7103 |
args: anElement : <object> |
8874 | 7104 |
start : <integer> |
7105 |
exceptionBlock : <valueImplementor> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 7108 |
exceptionBlock value - if not found |
7109 |
" |
|
7110 |
||
7111 |
" |
|
7112 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
7113 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
7114 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
7115 |
#(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
|
7116 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7117 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7118 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7119 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 7120 |
! ! |
7121 |
||
7122 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
7123 |
||
6958 | 7124 |
detectFirstInOrder:sortBlock |
7125 |
"find the first element of the collection sorted with sortBlock" |
|
7126 |
||
7127 |
|selectedForNow start| |
|
7128 |
||
7129 |
start := true. |
|
7130 |
self do:[:element| |
|
8874 | 7131 |
start ifTrue:[ |
7132 |
start := false. |
|
7133 |
selectedForNow := element. |
|
7134 |
] ifFalse:[ |
|
7135 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
7136 |
selectedForNow := element. |
|
7137 |
]. |
|
7138 |
]. |
|
6958 | 7139 |
]. |
7140 |
||
7141 |
^ selectedForNow |
|
7142 |
||
7143 |
" |
|
8874 | 7144 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
7145 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
7146 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
7147 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 7148 |
" |
7149 |
! |
|
7150 |
||
5358 | 7151 |
mergeSort |
8874 | 7152 |
"sort the collection using a mergeSort algorithm. |
7153 |
The elements are compared using'<' |
|
5358 | 7154 |
i.e. they should offer a magnitude-like protocol. |
7155 |
||
7156 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 7157 |
the best possible for all situations |
5358 | 7158 |
See also #quickSort and #randomizedSort for other sort algorithms |
7159 |
with different worst- and average case behavior)" |
|
7160 |
||
12888 | 7161 |
self mergeSort:[:a :b | a < b] |
5358 | 7162 |
|
7163 |
" |
|
7164 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
12888 | 7165 |
#(1 16 7 98 7 3 19 4 0) mergeSort |
5358 | 7166 |
|
7167 |
|data| |
|
7168 |
data := Random new next:100000. |
|
7169 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
7170 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 7171 |
data reverse. |
5358 | 7172 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
7173 |
||
7174 |
data := Random new next:100000. |
|
7175 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7176 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7177 |
data reverse. |
5358 | 7178 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7179 |
||
7180 |
data := Random new next:100000. |
|
7181 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
7182 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 7183 |
data reverse. |
5358 | 7184 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
7185 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7186 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7187 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7188 |
mergeSort:sortBlock |
8874 | 7189 |
"sort the collection using a mergeSort algorithm. |
7190 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7191 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7192 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7193 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 7194 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7195 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7196 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7197 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7198 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7199 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7200 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7201 |
(stop > 1) ifTrue:[ |
8874 | 7202 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7203 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7204 |
|
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 |
#(1 16 7 98 3 19 4 0) mergeSort |
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 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7209 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7210 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7211 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7212 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7213 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7214 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7215 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7216 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7217 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 7218 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7219 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7220 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7221 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7222 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7223 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 7224 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7225 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7226 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7227 |
! |
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 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 7230 |
"Sort the given range of indices using the mergesort algorithm. |
7231 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 7232 |
as many comparisons as heapsort or quicksort." |
7233 |
||
8874 | 7234 |
"Details: recursively split the range to be sorted into two halves, |
7235 |
mergesort each half, then merge the two halves together. |
|
7236 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
7237 |
and forth between the receiver and this copy. |
|
7238 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 7239 |
resulting in the receiver being completely sorted." |
7240 |
||
7241 |
| temp | |
|
7242 |
||
7243 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
7244 |
startIndex = stopIndex ifTrue: [^ self]. |
|
7245 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 7246 |
ifFalse: [self error: 'bad start index']. |
5358 | 7247 |
stopIndex <= self size |
8874 | 7248 |
ifFalse: [self error: 'bad stop index']. |
5358 | 7249 |
temp := self clone. |
7250 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
7251 |
||
7252 |
" |
|
8874 | 7253 |
#(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
|
7254 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7255 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7256 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7257 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7258 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7259 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7260 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7261 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7262 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
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:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7268 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 7269 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7270 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7274 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7275 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7276 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7277 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7278 |
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
|
7279 |
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
|
7280 |
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
|
7281 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7282 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7283 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7284 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7285 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7286 |
(stop > 1) ifTrue:[ |
8874 | 7287 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7288 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7289 |
|
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 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 7292 |
|
7293 |
|data| |
|
7294 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7295 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7296 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 7297 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7298 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7299 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7300 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7301 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7302 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7303 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7304 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7305 |
"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
|
7306 |
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
|
7307 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7308 |
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
|
7309 |
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
|
7310 |
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
|
7311 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7312 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7313 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7314 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7315 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7316 |
(stop > 1) ifTrue:[ |
8874 | 7317 |
sortBlock numArgs == 3 ifTrue:[ |
7318 |
"/ TODO: pass a collating policy to aBlock |
|
7319 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
7320 |
] ifFalse:[ |
|
7321 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
7322 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7323 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7324 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7325 |
" |
8874 | 7326 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7327 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7328 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7329 |
#(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
|
7330 |
" |
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 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7333 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7334 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7335 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7336 |
"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
|
7337 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7338 |
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
|
7339 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7340 |
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
|
7341 |
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
|
7342 |
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
|
7343 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7344 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7345 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7346 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7347 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7348 |
(stop > 1) ifTrue:[ |
8874 | 7349 |
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
|
7350 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7351 |
|
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 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7354 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7355 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7356 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7357 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7358 |
c2 printNL |
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 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7362 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7363 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7364 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7365 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7366 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7367 |
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
|
7368 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7369 |
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
|
7370 |
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
|
7371 |
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
|
7372 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7373 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7374 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7375 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7376 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7377 |
(stop > 1) ifTrue:[ |
8874 | 7378 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7379 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7380 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7381 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 7382 |
! |
7383 |
||
11272 | 7384 |
randomShuffle |
7385 |
"random shuffle my elements in place. |
|
7386 |
This is a destructive algorithm. |
|
7387 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
7388 |
See 'The Art of Computer Programming'." |
|
7389 |
||
7390 |
self size to:2 by:-1 do:[:endIndex | |
|
7391 |
|rndIndex t| |
|
7392 |
||
7393 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
7394 |
"/ exchange |
|
7395 |
t := self at:rndIndex. |
|
7396 |
self at:rndIndex put:(self at:endIndex). |
|
7397 |
self at:endIndex put:t. |
|
7398 |
]. |
|
7399 |
||
7400 |
" |
|
7401 |
(1 to:10) asOrderedCollection randomShuffle |
|
7402 |
||
7403 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7404 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7405 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7406 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7407 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7408 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7409 |
" |
|
7410 |
||
11273 | 7411 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
7412 |
||
7413 |
|a score| |
|
7414 |
||
7415 |
a := 'abcd'. |
|
7416 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
7417 |
||
7418 |
1000000 times do:[ |
|
7419 |
|sorted| |
|
7420 |
sorted := a copy randomShuffle. |
|
7421 |
score at:sorted put:(score at:sorted)+1. |
|
7422 |
]. |
|
7423 |
||
7424 |
score keys asSortedCollection do:[:k | |
|
7425 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
7426 |
]. |
|
7427 |
" |
|
7428 |
||
7429 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
11272 | 7430 |
! |
7431 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7432 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7433 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7434 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7435 |
|
8874 | 7436 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7437 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7438 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7439 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7440 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7441 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7442 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7443 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7444 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7445 |
"/ 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
|
7446 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7447 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7448 |
"/ 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
|
7449 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7450 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7451 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7452 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7453 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7454 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7455 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7456 |
|
8874 | 7457 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7458 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7459 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7460 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7461 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7462 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7463 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7464 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7465 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7466 |
"/ 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
|
7467 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7468 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7469 |
"/ 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
|
7470 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7471 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7472 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7473 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7474 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7475 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7476 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7477 |
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
|
7478 |
|
8874 | 7479 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7480 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7481 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7482 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7483 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7484 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7485 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7486 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7487 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7488 |
"/ 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
|
7489 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7490 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7491 |
"/ 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
|
7492 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7493 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7494 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7495 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7496 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7497 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7498 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7499 |
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
|
7500 |
|
8874 | 7501 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7502 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7503 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7504 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7505 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7506 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7507 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7508 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7509 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7510 |
"/ 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
|
7511 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7512 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7513 |
"/ 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
|
7514 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7515 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7516 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7517 |
|
602 | 7518 |
reverse |
7335 | 7519 |
"destructively reverse the order of the elements inplace. |
11264 | 7520 |
WARNING: this is a destructive operation, which modifies the receiver. |
7521 |
Please use reversed (with a d) for a functional version." |
|
602 | 7522 |
|
7523 |
|lowIndex "{ Class: SmallInteger }" |
|
7524 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7525 |
t1 t2| |
602 | 7526 |
|
7527 |
hiIndex := self size. |
|
7528 |
lowIndex := 1. |
|
7529 |
[lowIndex < hiIndex] whileTrue:[ |
|
11264 | 7530 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
7531 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
7532 |
||
7533 |
lowIndex := lowIndex + 1. |
|
7534 |
hiIndex := hiIndex - 1 |
|
602 | 7535 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7536 |
|
69 | 7537 |
" |
602 | 7538 |
#(4 5 6 7 7) reverse |
7539 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 7540 |
" |
1 | 7541 |
! |
7542 |
||
5004 | 7543 |
reversed |
7544 |
"return a copy with elements in reverse order" |
|
7545 |
||
7546 |
^ self copy reverse |
|
7547 |
||
7548 |
" |
|
8874 | 7549 |
#(4 5 6 7 7) reversed |
5004 | 7550 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
7551 |
" |
|
7552 |
! |
|
7553 |
||
1 | 7554 |
sort |
7555 |
"sort the collection inplace. The elements are compared using |
|
2069 | 7556 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7557 |
|
159 | 7558 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7559 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7560 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7561 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 7562 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7563 |
self quickSort |
1 | 7564 |
|
95 | 7565 |
" |
7566 |
#(1 16 7 98 3 19 4 0) sort |
|
7567 |
||
7568 |
|data| |
|
7569 |
data := Random new next:100000. |
|
7570 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7571 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7572 |
data reverse. |
95 | 7573 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7574 |
" |
|
2069 | 7575 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7576 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 7577 |
! |
7578 |
||
7579 |
sort:sortBlock |
|
7580 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7581 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7582 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7583 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7584 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7585 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7586 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7587 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7588 |
self quickSort:sortBlock |
1 | 7589 |
|
95 | 7590 |
" |
8874 | 7591 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7592 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7593 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7594 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 7595 |
" |
2069 | 7596 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7597 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 7598 |
! |
7599 |
||
7600 |
sort:sortBlock with:aCollection |
|
7601 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 7602 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7603 |
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
|
7604 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7605 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7606 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7607 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7608 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7609 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7610 |
self quickSort:sortBlock with:aCollection |
1 | 7611 |
|
69 | 7612 |
" |
7613 |
|c1 c2| |
|
1 | 7614 |
c1 := #(1 16 7 9). |
7615 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
7616 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 7617 |
c1 printNL. |
7618 |
c2 printNL |
|
69 | 7619 |
" |
2069 | 7620 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7621 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 7622 |
! |
7623 |
||
12647 | 7624 |
sortByApplying:aBlock |
7625 |
"Sort my contents inplace based on the value of what aBlock returns for each element. |
|
7626 |
Similar to, but even more flexible than sortBySelector." |
|
7627 |
||
7628 |
^ self sort:[:a :b | (aBlock value:a) < (aBlock value:b)] |
|
7629 |
||
7630 |
" |
|
7631 |
replace all uses of sort as in: |
|
7632 |
... sort:[:a :b | a foo < b foo] |
|
7633 |
by: |
|
7634 |
... sortByApplying:[:each | each foo] |
|
7635 |
" |
|
7636 |
! |
|
7637 |
||
11154 | 7638 |
sortBySelector:aSelector |
7639 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
7640 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
7641 |
||
7642 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
7643 |
||
7644 |
" |
|
7645 |
replace all uses of sort as in: |
|
7646 |
... sort:[:a :b | a foo < b foo] |
|
7647 |
by: |
|
7648 |
... sortBySelector:#foo |
|
7649 |
||
7650 |
find these by searching for code matching (code-search in the browsers method list): |
|
7651 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
7652 |
" |
|
7653 |
! |
|
7654 |
||
602 | 7655 |
sortWith:aCollection |
2069 | 7656 |
"sort the receiver collection inplace, using '<' to compare elements. |
7657 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7658 |
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
|
7659 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7660 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7661 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7662 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7663 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 7664 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7665 |
self quickSortWith:aCollection |
424 | 7666 |
! |
7667 |
||
11279 | 7668 |
sortedBy:aBlock |
9072 | 7669 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
7670 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
7671 |
||
7672 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
11279 | 7673 |
|
7674 |
"Modified: / 22-10-2008 / 21:25:07 / cg" |
|
9072 | 7675 |
! |
7676 |
||
602 | 7677 |
topologicalSort:sortBlock |
7678 |
"sort the collection inplace using a sloooow sort algorithm. |
|
7679 |
This algorithm has O-square runtime behavior and should be used only |
|
7680 |
in special situations. |
|
7681 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
7682 |
a>c (for example, to sort classes by inheritance) |
|
7683 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7684 |
In all other situations, use #sort; which implements the quicksort algorithm. |
10812 | 7685 |
|
7686 |
If there are cycles, this algorithm may loop endless!! |
|
7687 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
77 | 7688 |
" |
602 | 7689 |
|
7690 |
|smallestIndex "{ Class: SmallInteger }" |
|
7691 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7692 |
index2 "{ Class: SmallInteger }" |
602 | 7693 |
smallest thisOne| |
7694 |
||
7695 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
7696 |
||
7697 |
end := self size. |
|
862 | 7698 |
1 to:end-1 do:[:index | |
10812 | 7699 |
smallest := self at:index. |
7700 |
smallestIndex := index. |
|
7701 |
||
7702 |
index2 := index + 1. |
|
7703 |
[ |
|
7704 |
thisOne := self at:index2. |
|
7705 |
(index2 ~~ smallestIndex |
|
7706 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
7707 |
smallestIndex := index2. |
|
7708 |
smallest := thisOne. |
|
7709 |
index2 := index + 1. |
|
7710 |
] ifFalse:[ |
|
7711 |
index2 := index2 + 1. |
|
7712 |
]. |
|
7713 |
] doWhile:[index2 <= end]. |
|
862 | 7714 |
|
7715 |
"/ (index + 1) to:end do:[:index2 | |
|
7716 |
"/ thisOne := self at:index2. |
|
7717 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
7718 |
"/ smallestIndex := index2. |
|
7719 |
"/ smallest := thisOne |
|
7720 |
"/ ]. |
|
7721 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7722 |
|
10812 | 7723 |
(smallestIndex ~~ index) ifTrue:[ |
7724 |
thisOne := self at:index. |
|
7725 |
self at:index put:smallest. |
|
7726 |
self at:smallestIndex put:thisOne |
|
7727 |
]. |
|
1 | 7728 |
] |
95 | 7729 |
|
77 | 7730 |
" |
8874 | 7731 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
7732 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
10812 | 7733 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
8874 | 7734 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
7735 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 7736 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7737 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7738 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 7739 |
! ! |
602 | 7740 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7741 |
!SequenceableCollection methodsFor:'testing'! |
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 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7744 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7745 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 7746 |
however, having a common protocol with indexed collections |
7747 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7748 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7749 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7750 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7751 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7752 |
" |
8874 | 7753 |
#(1 2 3) includesKey:4 |
7754 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7755 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7756 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7757 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7758 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7759 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7760 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7761 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7762 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7763 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7764 |
|
2069 | 7765 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7766 |
|
7767 |
version |
|
12888 | 7768 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.295 2010-04-23 13:39:19 mb Exp $' |
12051 | 7769 |
! |
7770 |
||
7771 |
version_CVS |
|
12888 | 7772 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.295 2010-04-23 13:39:19 mb Exp $' |
628 | 7773 |
! ! |
6820 | 7774 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7775 |
SequenceableCollection initialize! |