author | Stefan Vogel <sv@exept.de> |
Thu, 06 Dec 2007 22:46:33 +0100 | |
changeset 10812 | 88568f2c6647 |
parent 10749 | 967a9e8adfc0 |
child 10818 | cbbb7124b553 |
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 |
" |
|
39 |
SequenceableCollections have ordered elements which can be accessed via |
|
8874 | 40 |
a numeric index. |
41 |
SequenceableCollection is an abstract class - there are no instances of |
|
42 |
it in the system. |
|
1385 | 43 |
|
44 |
The methods found here assume that implementations of (at least) |
|
45 |
#at:/#at:put: are implemented (which are always found in Object for |
|
46 |
indexable objects). |
|
47 |
||
48 |
For performance, some subclasses redefine more methods, knowing the details |
|
8874 | 49 |
of how elements are stored. |
50 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
|
1385 | 51 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this |
52 |
kind of tuneup. |
|
53 |
||
54 |
See concrete subclasses (such as OrderedCollection, Array or String). |
|
1289 | 55 |
|
56 |
[author:] |
|
8874 | 57 |
Claus Gittinger |
1385 | 58 |
|
59 |
[see also:] |
|
8874 | 60 |
OrderedCollection Array |
61 |
CharacterArray String |
|
88 | 62 |
" |
63 |
! ! |
|
1 | 64 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
65 |
!SequenceableCollection class methodsFor:'initialization'! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
66 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
67 |
initialize |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
68 |
MissingClassInLiteralArrayErrorSignal isNil ifTrue:[ |
8874 | 69 |
MissingClassInLiteralArrayErrorSignal := Error newSignalMayProceed:true. |
70 |
MissingClassInLiteralArrayErrorSignal nameClass:self message:#missingClassInLiteralArrayErrorSignal. |
|
71 |
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
|
72 |
] |
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 |
"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
|
75 |
! ! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
76 |
|
2069 | 77 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 78 |
|
4807 | 79 |
decodeFromLiteralArray:anArray |
80 |
"create & return a new instance from information encoded in anArray. |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
81 |
Redefined for faster creation." |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
82 |
|
8874 | 83 |
|collection |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
84 |
sz "{ Class: SmallInteger }"| |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
85 |
|
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
86 |
sz := anArray size. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
87 |
collection := self withSize:sz-1. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
88 |
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
|
89 |
^ collection |
4807 | 90 |
|
8797 | 91 |
" |
92 |
Array |
|
8874 | 93 |
decodeFromLiteralArray:#(Array 1 2 3) |
8797 | 94 |
" |
4807 | 95 |
! |
96 |
||
1 | 97 |
new:size withAll:element |
360 | 98 |
"return a new collection of size, where all elements are |
99 |
initialized to element." |
|
1 | 100 |
|
101 |
|newCollection| |
|
102 |
||
103 |
newCollection := self new:size. |
|
104 |
newCollection atAllPut:element. |
|
105 |
^ newCollection |
|
360 | 106 |
! |
107 |
||
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
108 |
newWithConcatenationOfAll:aCollectionOfCollections |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
109 |
"this creates and returns a new collection consisting |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
by avoiding repeated garbage allocations. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
114 |
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
|
115 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
116 |
|totalSize newColl idx| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
117 |
|
8874 | 118 |
totalSize := aCollectionOfCollections |
119 |
inject:0 |
|
120 |
into:[:sumSoFar :el | sumSoFar + el size]. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
121 |
newColl := self new:totalSize. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
122 |
idx := 1. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
123 |
aCollectionOfCollections do:[:el | |
8874 | 124 |
|sz| |
125 |
||
126 |
sz := el size. |
|
127 |
newColl replaceFrom:idx to:(idx+sz-1) with:el startingAt:1. |
|
128 |
idx := idx + sz |
|
4303
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 |
^ newColl |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
131 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
132 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
133 |
this method optimizes the following (common) operation: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
134 |
|
8874 | 135 |
|s| |
136 |
||
137 |
s := ''. |
|
138 |
#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') do:[:e| |
|
139 |
s := s , e. |
|
140 |
]. |
|
141 |
s |
|
142 |
||
143 |
String |
|
144 |
newWithConcatenationOfAll:#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') |
|
145 |
||
146 |
String |
|
147 |
newWithConcatenationOfAll:#() |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
148 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
149 |
Array |
8874 | 150 |
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
|
151 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
152 |
timing: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
153 |
------- |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
154 |
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
|
155 |
for more elements, #newWithConcatenationOfAll: is much faster. |
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 arr2 arr3 t| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
158 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
159 |
arr1 := #('hello' ' ' 'world' ' ' 'this is ' 'ST/X'). |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
160 |
arr2 := Array new:1000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
161 |
arr3 := Array new:10000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
162 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
163 |
(Array with:arr1 with:arr2 with:arr3) with:#(10000 100 10) do:[:arr :cnt | |
8874 | 164 |
t := Time millisecondsToRun:[ |
165 |
cnt timesRepeat:[ |
|
166 |
String |
|
167 |
newWithConcatenationOfAll:arr |
|
168 |
] |
|
169 |
]. |
|
170 |
Transcript showCR:(arr size printString , ' elements - time for #newWithConcatenationOfAll :' , (t/cnt) asFloat printString , 'mS'). |
|
171 |
Transcript endEntry. |
|
172 |
||
173 |
t := Time millisecondsToRun:[ |
|
174 |
cnt timesRepeat:[ |
|
175 |
|s| |
|
176 |
||
177 |
s := ''. |
|
178 |
arr do:[:e| |
|
179 |
s := s , e. |
|
180 |
]. |
|
181 |
s |
|
182 |
] |
|
183 |
]. |
|
184 |
Transcript showCR:(arr size printString , ' elements - time for loop over #, :' , (t/cnt) asFloat printString , 'mS'). |
|
185 |
Transcript endEntry. |
|
4303
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 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
188 |
! |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
189 |
|
360 | 190 |
withSize:size |
191 |
"return a new collection of size. |
|
192 |
For variable size collections, this is different from #new:, |
|
193 |
in that #new: creates an empty collection with preallocated size, |
|
194 |
while #withSize: creates a non empty one." |
|
195 |
||
9162 | 196 |
^ (self new:size) grow:size. |
360 | 197 |
|
198 |
" |
|
199 |
(OrderedCollection new:10) inspect. |
|
200 |
(OrderedCollection withSize:10) inspect. |
|
201 |
(Array new:10) inspect. |
|
202 |
(Array withSize:10) inspect. |
|
203 |
" |
|
1 | 204 |
! ! |
205 |
||
7304 | 206 |
!SequenceableCollection class methodsFor:'Compatibility-Squeak'! |
4370 | 207 |
|
6820 | 208 |
writeStream |
209 |
"create a write-stream on an instance of the receiver-class" |
|
210 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
211 |
^ WriteStream on:(self new:50). |
8220 | 212 |
|
213 |
" |
|
214 |
String writeStream |
|
215 |
" |
|
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
216 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
217 |
"Modified: / 29-03-2007 / 15:20:02 / cg" |
4370 | 218 |
! ! |
219 |
||
5188 | 220 |
!SequenceableCollection class methodsFor:'Signal constants'! |
221 |
||
222 |
missingClassInLiteralArrayErrorSignal |
|
223 |
^ MissingClassInLiteralArrayErrorSignal |
|
224 |
||
225 |
"Created: / 18.5.1999 / 14:50:04 / cg" |
|
226 |
! ! |
|
227 |
||
7304 | 228 |
!SequenceableCollection class methodsFor:'instance creation-multiDimensional'! |
7255 | 229 |
|
230 |
_at:nIndices |
|
231 |
"this is a synthetic selector, generated by the compiler, |
|
232 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 233 |
I.e. |
234 |
Array[n] |
|
7255 | 235 |
generates |
8874 | 236 |
Array _at: n |
7255 | 237 |
" |
238 |
||
239 |
^ self new:nIndices |
|
240 |
! |
|
241 |
||
242 |
_at:dim1 at:dim2 |
|
243 |
"this is a synthetic selector, generated by the compiler, |
|
244 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 245 |
I.e. |
246 |
Array[n,m] |
|
7255 | 247 |
generates |
8874 | 248 |
Array _at:n at:m |
7255 | 249 |
" |
250 |
||
251 |
|data| |
|
252 |
||
253 |
data := self withSize:(dim1 * dim2). |
|
254 |
^ MultiDimensionalArrayAccessor |
|
8874 | 255 |
collection:data |
256 |
dimensions:(Array with:dim1 with:dim2) |
|
7255 | 257 |
! |
258 |
||
259 |
_at:dim1 at:dim2 at:dim3 |
|
260 |
"this is a synthetic selector, generated by the compiler, |
|
261 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 262 |
I.e. |
263 |
Array[n,m,o] |
|
7255 | 264 |
generates |
8874 | 265 |
Array _at:n at:m at:o |
7255 | 266 |
" |
267 |
||
268 |
|data| |
|
269 |
||
270 |
data := self withSize:(dim1 * dim2 * dim3). |
|
8874 | 271 |
^ MultiDimensionalArrayAccessor |
272 |
collection:data |
|
273 |
dimensions:(Array with:dim1 with:dim2 with:dim3) |
|
7255 | 274 |
! ! |
275 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
276 |
!SequenceableCollection class methodsFor:'instance creation-streaming'! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
277 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
278 |
streamContents:blockWithArg |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
279 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
280 |
evaluate blockWithArg, passing that stream, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
281 |
extract and return the streams contents." |
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| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
284 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
285 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
286 |
blockWithArg value:stream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
287 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
288 |
|
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| |
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 |
rslt := String streamContents:[:s | s nextPutAll:'hello'; space; nextPutAll:'world'] |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
293 |
" |
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 |
"Modified: / 29-03-2007 / 15:20:20 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
296 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
297 |
|
10590 | 298 |
streamContents:blockWithArg limitedTo:limit |
299 |
"create a limited write-stream on an instance of the receiver-class, |
|
300 |
evaluate blockWithArg, passing that stream, |
|
301 |
extract and return the streams contents (possibly truncated)." |
|
302 |
||
303 |
|stream| |
|
304 |
||
305 |
stream := self writeStream. |
|
306 |
stream writeLimit:limit. |
|
307 |
blockWithArg value:stream. |
|
308 |
^ stream contents |
|
309 |
||
310 |
"Created: / 04-06-2007 / 17:22:38 / cg" |
|
311 |
! |
|
312 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
313 |
streamContentsEnumerating:aReceiver using:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
314 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
315 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
316 |
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
|
317 |
enumerator somewhere, and you want this as a collection." |
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| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
320 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
321 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
322 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
323 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
324 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
325 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
326 |
String streamContentsEnumerating:'hello' using:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
327 |
Array streamContentsEnumerating:Method using:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
328 |
" |
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 |
"Created: / 29-03-2007 / 15:21:12 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
331 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
332 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
333 |
streamContentsOf:aReceiver enumerator:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
334 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
335 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
336 |
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
|
337 |
enumerator somewhere, and you want this as a collection." |
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| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
340 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
341 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
342 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
343 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
344 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
345 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
346 |
String streamContentsOf:'hello' enumerator:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
347 |
Array streamContentsOf:Method enumerator:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
348 |
" |
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 |
"Created: / 29-03-2007 / 15:05:30 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
351 |
! ! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
352 |
|
8893 | 353 |
!SequenceableCollection class methodsFor:'queries'! |
354 |
||
355 |
isAbstract |
|
356 |
^ self == SequenceableCollection |
|
357 |
! ! |
|
358 |
||
7261 | 359 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 360 |
|
7796 | 361 |
allButFirst |
8874 | 362 |
"Return a copy of the receiver containing all but the first element. |
7796 | 363 |
Raise an error if there are not enough elements." |
364 |
||
365 |
^ self copyFrom:2 |
|
8571 | 366 |
|
367 |
" |
|
368 |
'1234567890' allButFirst |
|
369 |
" |
|
370 |
! |
|
371 |
||
372 |
allButFirst:n |
|
8874 | 373 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 374 |
Raise an error if there are not enough elements." |
375 |
||
376 |
^ self copyFrom:n+1 |
|
377 |
||
378 |
" |
|
379 |
'1234567890' allButFirst:5 |
|
380 |
" |
|
7796 | 381 |
! |
382 |
||
6149 | 383 |
allButLast |
8874 | 384 |
"Return a copy of the receiver containing all but the last element. |
7796 | 385 |
Raise an error if there are not enough elements." |
6183 | 386 |
|
387 |
^ self allButLast:1 |
|
388 |
||
8571 | 389 |
" |
390 |
'1234567890' allButLast |
|
391 |
" |
|
392 |
||
6183 | 393 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
394 |
! |
|
395 |
||
396 |
allButLast:n |
|
8874 | 397 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 398 |
Raise an error if there are not enough elements." |
6183 | 399 |
|
400 |
^ self copyFrom: 1 to: self size - n |
|
401 |
||
8571 | 402 |
" |
403 |
'1234567890' allButLast:5 |
|
404 |
" |
|
405 |
||
6183 | 406 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 407 |
! |
408 |
||
8874 | 409 |
atPin: index |
4414 | 410 |
"Return the index'th element of me if possible. |
411 |
Return the first or last element if index is out of bounds." |
|
412 |
||
413 |
index < 1 ifTrue:[^ self first]. |
|
414 |
index > self size ifTrue:[^ self last]. |
|
415 |
^ self at:index |
|
416 |
||
417 |
" |
|
8874 | 418 |
#(1 2 3) atPin:4 |
419 |
#(1 2 3) atPin:0 |
|
420 |
#(1 2 3) atPin:1 |
|
421 |
#(1 2 3) atPin:3 |
|
422 |
" |
|
423 |
! |
|
424 |
||
425 |
atRandom |
|
4459 | 426 |
"Return any random element from the receiver" |
427 |
||
8573 | 428 |
^ self atRandom:Random |
429 |
||
430 |
" |
|
8874 | 431 |
#(1 2 3) atRandom |
432 |
" |
|
433 |
! |
|
434 |
||
435 |
atRandom:aRandomGenerator |
|
8573 | 436 |
"Return any random element from the receiver" |
437 |
||
4459 | 438 |
|max idx| |
439 |
||
440 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 441 |
self emptyCollectionError |
4459 | 442 |
]. |
8573 | 443 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 444 |
^ self at:idx |
445 |
||
446 |
" |
|
8874 | 447 |
#(1 2 3) atRandom:(Random new) |
448 |
" |
|
449 |
! |
|
450 |
||
451 |
atWrap:index |
|
4975 | 452 |
"Return the index'th element of me if possible. |
453 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
454 |
||
455 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
456 |
||
457 |
" |
|
8874 | 458 |
#(1 2 3) atWrap:1 |
459 |
#(1 2 3) atWrap:2 |
|
460 |
#(1 2 3) atWrap:3 |
|
461 |
#(1 2 3) atWrap:4 |
|
462 |
#(1 2 3) atWrap:5 |
|
463 |
#(1 2 3) atWrap:6 |
|
464 |
#(1 2 3) atWrap:7 |
|
465 |
#(1 2 3) atWrap:0 |
|
466 |
#(1 2 3) atWrap:-1 |
|
4975 | 467 |
" |
5372 | 468 |
! |
469 |
||
470 |
beginsWith:aCollection |
|
6183 | 471 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 472 |
|
473 |
^ self startsWith:aCollection |
|
6183 | 474 |
|
475 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 476 |
! |
477 |
||
8611 | 478 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 479 |
"squeak uses a different naming here - sigh. |
480 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
481 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
482 |
this method first takes a slice (from:to:) and then collects over that. |
|
483 |
I.e. it is equivalent to: |
|
8874 | 484 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 485 |
|
486 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
487 |
! |
|
488 |
||
7012 | 489 |
copyAfter: anElement |
8874 | 490 |
"Answer a copy of the receiver from after the first occurrence |
491 |
of anElement up to the end. If no such element exists, answer |
|
492 |
an empty copy." |
|
7012 | 493 |
|idx| |
494 |
||
495 |
idx := self indexOf:anElement. |
|
496 |
idx == 0 ifTrue:[idx := self size]. |
|
497 |
^ self copyFrom:idx + 1 |
|
498 |
||
499 |
" |
|
8874 | 500 |
'hello world' copyAfter:$l |
501 |
'123456123456' copyAfter:$2 |
|
502 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
503 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
504 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 505 |
" |
506 |
! |
|
507 |
||
6982 | 508 |
copyAfterLast:element |
8874 | 509 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 510 |
of element to the end; uses = for comparison" |
511 |
||
512 |
|idx| |
|
513 |
||
514 |
idx := self lastIndexOf:element. |
|
515 |
idx == 0 ifTrue:[idx := self size]. |
|
516 |
^ self copyFrom:idx + 1 |
|
517 |
||
518 |
" |
|
8874 | 519 |
'hello world' copyAfterLast:$l |
520 |
'123456123456' copyAfterLast:$2 |
|
521 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
522 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
523 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 524 |
" |
525 |
! |
|
526 |
||
6980 | 527 |
copyUpToLast:element |
8874 | 528 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 529 |
of element; uses = for comparison" |
530 |
||
531 |
|idx| |
|
532 |
||
533 |
idx := self lastIndexOf:element. |
|
534 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
535 |
^ self copyTo:idx - 1 |
|
536 |
||
537 |
" |
|
8874 | 538 |
'hello world' copyUpToLast:$l |
539 |
'123456123456' copyUpToLast:$2 |
|
540 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
541 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
542 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 543 |
" |
544 |
! |
|
545 |
||
8276 | 546 |
copyWithoutAll:elementsToSkip |
547 |
"return a new collection consisting of a copy of the receiver, with |
|
548 |
ALL elements equal to any in elementsToSkip are left out. |
|
549 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
550 |
||
551 |
|n "{ Class: SmallInteger }" |
|
552 |
sz "{ Class: SmallInteger }" |
|
553 |
srcIndex "{ Class: SmallInteger }" |
|
554 |
dstIndex "{ Class: SmallInteger }" |
|
555 |
skipIndex "{ Class: SmallInteger }" |
|
556 |
copy l| |
|
557 |
||
8874 | 558 |
"the code below may look like overkill, |
8276 | 559 |
however, for big collections its better to move data |
560 |
around in big chunks" |
|
561 |
||
562 |
n := self occurrencesOfAny:elementsToSkip. |
|
563 |
n == 0 ifTrue:[^ self copy]. |
|
564 |
||
565 |
sz := self size. |
|
566 |
copy := self copyEmptyAndGrow:(sz - n). |
|
567 |
||
568 |
srcIndex := 1. |
|
569 |
dstIndex := 1. |
|
570 |
||
571 |
n timesRepeat:[ |
|
8874 | 572 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
573 |
l := skipIndex - srcIndex. |
|
574 |
l ~~ 0 ifTrue:[ |
|
575 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
576 |
with:self startingAt:srcIndex. |
|
577 |
dstIndex := dstIndex + l |
|
578 |
]. |
|
579 |
srcIndex := skipIndex + 1 |
|
8276 | 580 |
]. |
581 |
l := sz - srcIndex. |
|
582 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 583 |
with:self startingAt:srcIndex. |
8276 | 584 |
^ copy |
585 |
||
586 |
" |
|
587 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 588 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 589 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
590 |
" |
|
591 |
! |
|
592 |
||
7304 | 593 |
copyWithoutFirst |
594 |
^ self copyFrom:2 |
|
595 |
! |
|
596 |
||
6149 | 597 |
permutationsDo: aBlock |
6183 | 598 |
"Repeatly value aBlock with a single copy of the receiver. Reorder the copy |
599 |
so that aBlock is presented all (self size factorial) possible permutations." |
|
600 |
||
601 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
602 |
||
603 |
" |
|
604 |
(1 to: 4) permutationsDo:[:each | Transcript cr; show: each printString] |
|
605 |
" |
|
606 |
||
607 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
6149 | 608 |
! |
609 |
||
610 |
permutationsStartingAt: anInteger do: aBlock |
|
10749 | 611 |
"a helper for permitationsDo:" |
6183 | 612 |
|
613 |
|mySize| |
|
614 |
||
615 |
mySize := self size. |
|
616 |
anInteger > mySize ifTrue: [^self]. |
|
617 |
anInteger = mySize ifTrue: [^aBlock value: self]. |
|
8874 | 618 |
anInteger to:mySize do:[:i | |
10749 | 619 |
self swap: anInteger with: i. |
620 |
self permutationsStartingAt: anInteger + 1 do: aBlock. |
|
621 |
self swap: anInteger with: i |
|
6183 | 622 |
] |
623 |
||
10749 | 624 |
" |
625 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
626 |
" |
|
627 |
||
628 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
4414 | 629 |
! ! |
630 |
||
7261 | 631 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 632 |
|
633 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
634 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
635 |
! ! |
|
636 |
||
1 | 637 |
!SequenceableCollection methodsFor:'accessing'! |
638 |
||
1366 | 639 |
after:anObject |
640 |
"return the element, after anObject. |
|
5231 | 641 |
If anObject is not in the receiver, report an error; |
642 |
if anObject is the last in the receiver, return nil. |
|
1366 | 643 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
644 |
||
645 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
646 |
||
647 |
" |
|
8874 | 648 |
#(4 3 2 1) asOrderedCollection after:3. |
649 |
#(4 3 2 1) asOrderedCollection after:5 |
|
650 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 651 |
" |
652 |
||
653 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
654 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
655 |
! |
|
656 |
||
657 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 658 |
"return the element, after anObject. |
5231 | 659 |
If anObject is the last in the receiver, return nil. |
660 |
If there is no such element (anObject is not found), |
|
1366 | 661 |
return the value from exceptionBlock." |
662 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
663 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
664 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
665 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
666 |
idx ~~ 0 ifTrue:[ |
8874 | 667 |
idx == self size ifTrue:[^ nil]. |
668 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
669 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
670 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
671 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
672 |
" |
8874 | 673 |
#(4 3 2 1) after:3 ifAbsent:nil. |
674 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
675 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
676 |
(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
|
677 |
" |
1366 | 678 |
! |
679 |
||
6967 | 680 |
anElement |
8874 | 681 |
"return any element from the collection, |
6967 | 682 |
report an error if there is none" |
683 |
||
684 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
685 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
686 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
687 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
688 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
689 |
" |
6967 | 690 |
! |
691 |
||
32 | 692 |
at:index ifAbsent:exceptionBlock |
8874 | 693 |
"return the element at index if valid. |
694 |
If the index is invalid, return the result of evaluating |
|
302 | 695 |
the exceptionblock. |
8874 | 696 |
NOTICE: |
697 |
in ST-80, this message is only defined for Dictionaries, |
|
698 |
however, having a common protocol with indexed collections |
|
699 |
often simplifies things." |
|
32 | 700 |
|
8579 | 701 |
(index between:1 and:self size) ifFalse:[ |
8874 | 702 |
^ exceptionBlock value |
32 | 703 |
]. |
704 |
^ self at:index |
|
705 |
||
95 | 706 |
" |
707 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 708 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
709 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 710 |
|
95 | 711 |
(Dictionary with:(#foo -> #bar) |
8874 | 712 |
with:(#frob -> #baz)) |
713 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 714 |
" |
362 | 715 |
! |
716 |
||
1366 | 717 |
before:anObject |
718 |
"return the element before the argument, anObject. |
|
5231 | 719 |
If anObject is not in the receiver, report an error; |
720 |
if anObject is the first in the receiver, return nil. |
|
1366 | 721 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
722 |
||
723 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
724 |
||
725 |
" |
|
8874 | 726 |
#(4 3 2 1) asOrderedCollection before:3. |
727 |
#(4 3 2 1) asOrderedCollection before:4 |
|
728 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 729 |
" |
730 |
||
731 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
732 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
733 |
! |
|
734 |
||
735 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 736 |
"return the element, before anObject. |
5231 | 737 |
If anObject is the first in the receiver, return nil. |
738 |
If there is no such element (anObject is not found), |
|
1366 | 739 |
return the value from exceptionBlock." |
740 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
741 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
742 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
743 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
744 |
idx ~~ 0 ifTrue:[ |
8874 | 745 |
idx == 1 ifTrue:[^ nil]. |
746 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
747 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
748 |
^ exceptionBlock value |
1366 | 749 |
|
5231 | 750 |
" |
8874 | 751 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
752 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
753 |
#(4 3 2 1) asOrderedCollection before:5. |
|
754 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
755 |
#(4 3 2 1) asOrderedCollection before:4. |
|
756 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
757 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 758 |
" |
759 |
||
1366 | 760 |
! |
761 |
||
602 | 762 |
first |
8874 | 763 |
"return the first element; |
5231 | 764 |
report an error, if the collection is empty." |
602 | 765 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
766 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 767 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
768 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
769 |
" |
8874 | 770 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
771 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
772 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
773 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
774 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 775 |
! |
776 |
||
5887 | 777 |
first:n |
778 |
"return the n first elements of the collection. |
|
779 |
Raises an error if there are not enough elements in the receiver." |
|
780 |
||
781 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
782 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
783 |
||
784 |
^ self copyFirst:n |
|
785 |
||
786 |
" |
|
8874 | 787 |
#(1 2 3 4 5) first:3 |
788 |
#(1 2 3 4 5) first:6 |
|
789 |
#(1 2 3 4 5) asSet first:3 |
|
790 |
'hello world' first:5 |
|
5887 | 791 |
" |
792 |
! |
|
793 |
||
362 | 794 |
keyAtEqualValue:value |
795 |
"return the index of a value. |
|
796 |
This is normally not used (use indexOf:), but makes the |
|
797 |
protocol more compatible with dictionaries." |
|
798 |
||
799 |
^ self indexOf:value |
|
800 |
! |
|
801 |
||
802 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
803 |
"return the index of a value. |
|
804 |
This is normally not used (use indexOf:), but makes the |
|
805 |
protocol more compatible with dictionaries." |
|
806 |
||
807 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 808 |
! |
809 |
||
810 |
keyAtValue:value |
|
811 |
"return the index of a value. |
|
812 |
This is normally not used (use indexOf:), but makes the |
|
813 |
protocol more compatible with dictionaries." |
|
814 |
||
815 |
^ self identityIndexOf:value |
|
816 |
! |
|
817 |
||
818 |
keyAtValue:value ifAbsent:exceptionBlock |
|
819 |
"return the index of a value. |
|
820 |
This is normally not used (use indexOf:), but makes the |
|
821 |
protocol more compatible with dictionaries." |
|
822 |
||
823 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
824 |
! |
|
825 |
||
826 |
last |
|
5231 | 827 |
"return the last element; |
828 |
report an error, if the collection is empty." |
|
602 | 829 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
830 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
831 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
832 |
(sz := self size) == 0 ifFalse:[ |
8874 | 833 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
834 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
835 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
836 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
837 |
|
5231 | 838 |
" |
839 |
#(1 2 3 4 5) last |
|
840 |
#() last |
|
841 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
842 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
843 |
|
5887 | 844 |
last:n |
845 |
"return the n last elements of the collection. |
|
846 |
Raises an error if there are not enough elements in the receiver." |
|
847 |
||
848 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
849 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
850 |
||
851 |
^ self copyLast:n |
|
852 |
||
853 |
" |
|
8874 | 854 |
#(1 2 3 4 5) last:3 |
855 |
#(1 2 3 4 5) last:6 |
|
856 |
#(1 2 3 4 5) asSet last:3 |
|
857 |
'hello world' last:5 |
|
5887 | 858 |
" |
859 |
! |
|
860 |
||
4986 | 861 |
nth:n |
5231 | 862 |
"return the nth element; |
863 |
report an error, if the collections size is smaller than n." |
|
4986 | 864 |
|
865 |
^ self at:n |
|
866 |
||
867 |
" |
|
8874 | 868 |
args: |
4986 | 869 |
returns: nth element <object> |
870 |
" |
|
871 |
||
5231 | 872 |
" |
8874 | 873 |
#(10 20 30 40 50) nth:4 |
5231 | 874 |
#(10 20 30 40 50) nth:6 |
875 |
#() nth:1 |
|
876 |
" |
|
877 |
||
4986 | 878 |
! |
879 |
||
4855 | 880 |
second |
5231 | 881 |
"return the second element; |
882 |
report an error, if the collections size is smaller than 2." |
|
4855 | 883 |
|
884 |
^ self at:2 |
|
885 |
||
886 |
" |
|
8874 | 887 |
args: |
4855 | 888 |
returns: secondElement <object> |
889 |
" |
|
890 |
||
891 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
892 |
! |
|
893 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
894 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
895 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
896 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
897 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
898 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
899 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
900 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
901 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
902 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
903 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 904 |
! ! |
905 |
||
602 | 906 |
!SequenceableCollection methodsFor:'adding & removing'! |
907 |
||
908 |
add:anObject |
|
909 |
"append the argument, anObject to the collection. |
|
910 |
Return the argument, anObject. |
|
911 |
||
912 |
Notice, that this is modifies the receiver NOT a copy. |
|
913 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 914 |
due to the grow:-message, which is inefficient for fixed size |
602 | 915 |
collections (i.e. for Strings and Arrays it is not recommened)." |
916 |
||
917 |
|newSize| |
|
918 |
||
919 |
newSize := self size + 1. |
|
920 |
self grow:newSize. |
|
921 |
self at:newSize put:anObject. |
|
922 |
^ anObject |
|
923 |
||
924 |
" |
|
925 |
|a| |
|
926 |
a := #(1 2 3 4 5 6 7 8). |
|
927 |
a add:'hello'. |
|
928 |
a |
|
929 |
" |
|
930 |
" |
|
931 |
|c| |
|
932 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
933 |
c add:'hello'. |
|
934 |
c |
|
935 |
" |
|
936 |
! |
|
937 |
||
938 |
add:anElement beforeIndex:index |
|
939 |
"insert the first argument, anObject into the collection before slot index. |
|
940 |
Return the receiver (sigh - ST-80 compatibility). |
|
941 |
||
942 |
Notice, that this is modifies the receiver NOT a copy. |
|
943 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 944 |
due to the grow:-message, which is inefficient for fixed size |
602 | 945 |
collections (i.e. for Strings and Arrays it is not recommened)." |
946 |
||
947 |
|newSize| |
|
948 |
||
949 |
newSize := self size + 1. |
|
950 |
self grow:newSize. |
|
951 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
952 |
self at:index put:anElement |
|
953 |
||
954 |
" |
|
955 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
956 |
" |
|
202 | 957 |
! |
958 |
||
602 | 959 |
addFirst:anObject |
960 |
"prepend the argument, anObject to the collection. |
|
961 |
Return the argument, anObject. |
|
962 |
||
963 |
Notice, that this is modifies the receiver NOT a copy. |
|
964 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 965 |
due to the grow:-message, which is inefficient for fixed size |
602 | 966 |
collections (i.e. for Strings and Arrays it is not recommened)." |
967 |
||
968 |
|newSize| |
|
969 |
||
970 |
newSize := self size + 1. |
|
971 |
self grow:newSize. |
|
972 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
973 |
self at:1 put:anObject. |
|
974 |
^ anObject |
|
975 |
||
976 |
" |
|
8874 | 977 |
|a| |
978 |
a:= #(1 2 3 4 5 6 7 8). |
|
979 |
a addFirst:'hello'. |
|
980 |
a |
|
602 | 981 |
" |
982 |
" |
|
983 |
|c| |
|
984 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
985 |
c addFirst:'hello'. |
|
986 |
c |
|
987 |
" |
|
69 | 988 |
! |
989 |
||
602 | 990 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
991 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
992 |
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
|
993 |
Use equality compare (=) to search for an occurrence. |
602 | 994 |
|
995 |
Notice, that this is modifies the receiver NOT a copy. |
|
996 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 997 |
due to the grow:-message, which is inefficient for fixed size |
602 | 998 |
collections (i.e. for Strings and Arrays it is not recommened)." |
999 |
||
8874 | 1000 |
|any |
602 | 1001 |
dstIndex "{ Class: SmallInteger }" |
1002 |
sz "{ Class: SmallInteger }"| |
|
1003 |
||
1004 |
dstIndex := 1. |
|
1005 |
any := false. |
|
1006 |
sz := self size. |
|
1007 |
1 to:sz do:[:srcIndex | |
|
8874 | 1008 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1009 |
any := true |
|
1010 |
] ifFalse:[ |
|
1011 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1012 |
self at:dstIndex put:(self at:srcIndex) |
|
1013 |
]. |
|
1014 |
dstIndex := dstIndex + 1 |
|
1015 |
] |
|
602 | 1016 |
]. |
1017 |
any ifTrue:[ |
|
8874 | 1018 |
self grow:dstIndex - 1. |
1019 |
^ anElement |
|
602 | 1020 |
]. |
1021 |
^ aBlock value |
|
1022 |
||
1023 |
" |
|
1422 | 1024 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1025 |
#(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
|
1026 |
#(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
|
1027 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1028 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1029 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1030 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1031 |
! |
1032 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1033 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1034 |
"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
|
1035 |
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
|
1036 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1037 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1038 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1039 |
"/ 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
|
1040 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1041 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1042 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1043 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1044 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1045 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1046 |
[runIndex <= self size] whileTrue:[ |
8874 | 1047 |
element := self at:runIndex. |
1048 |
(aBlock value:element) ifTrue:[ |
|
1049 |
removed add:element. |
|
1050 |
self removeAtIndex:runIndex |
|
1051 |
] ifFalse:[ |
|
1052 |
runIndex := runIndex + 1 |
|
1053 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1054 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1055 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1056 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1057 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1058 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1059 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1060 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1061 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1062 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1063 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1064 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1065 |
"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
|
1066 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1067 |
|
602 | 1068 |
removeAtIndex:anIndex |
1069 |
"remove the element stored at anIndex. Return the removed object. |
|
1070 |
||
1071 |
Notice, that this is modifies the receiver NOT a copy. |
|
1072 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1073 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1074 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1075 |
||
1076 |
|element| |
|
1077 |
||
1078 |
element := self at:anIndex. |
|
1079 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1080 |
^ element |
|
1081 |
||
1082 |
" |
|
1083 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1084 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1085 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1086 |
" |
|
13 | 1087 |
! |
1088 |
||
602 | 1089 |
removeFirst |
1090 |
"remove the first element of the receiver and return it. |
|
1091 |
||
1092 |
Notice, that this is modifies the receiver NOT a copy. |
|
1093 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1094 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1095 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1096 |
||
1097 |
^ self removeAtIndex:1 |
|
1098 |
||
1099 |
" |
|
1100 |
|a| |
|
1101 |
a := #(1 2 3 4 5 6). |
|
1102 |
a removeFirst. |
|
8874 | 1103 |
a |
602 | 1104 |
" |
13 | 1105 |
! |
1106 |
||
2294 | 1107 |
removeFromIndex:startIndex |
1108 |
"remove the elements stored at indexes from startIndex to the end. |
|
1109 |
Return the receiver. |
|
1110 |
Returning the receiver is a historic leftover - it may at one |
|
1111 |
time return a collection of the removed elements. |
|
1112 |
||
8874 | 1113 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1114 |
therefore any other users of the receiver will also see this change. |
1115 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1116 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1117 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1118 |
||
1119 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1120 |
||
1121 |
" |
|
8874 | 1122 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1123 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1124 |
" |
|
1125 |
||
1126 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1127 |
! |
|
1128 |
||
602 | 1129 |
removeFromIndex:startIndex toIndex:endIndex |
1130 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1131 |
Return the receiver. |
|
1132 |
Returning the receiver is a historic leftover - it may at one |
|
1133 |
time return a collection of the removed elements. |
|
1134 |
||
8874 | 1135 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1136 |
therefore any other users of the receiver will also see this change. |
1137 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1138 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1139 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1140 |
||
1141 |
|size newSize| |
|
1142 |
||
1143 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1144 |
self grow:(startIndex - 1) |
602 | 1145 |
] ifFalse:[ |
8874 | 1146 |
newSize := size - endIndex + startIndex - 1. |
1147 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1148 |
self grow:newSize |
|
602 | 1149 |
] |
1150 |
||
1151 |
"/ |newSize| |
|
1152 |
"/ |
|
1153 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1154 |
"/ newSize <= 0 ifTrue:[ |
|
1155 |
"/ self grow:0 |
|
1156 |
"/ ] ifFalse:[ |
|
1157 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1158 |
"/ self grow:newSize |
|
1159 |
"/ ] |
|
1160 |
||
1161 |
" |
|
8874 | 1162 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1163 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1164 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1165 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1166 |
" |
1167 |
! |
|
1168 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1169 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1170 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1171 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1172 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1173 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1174 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1175 |
Also note, that it may be a slow operation for some collections, |
8874 | 1176 |
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
|
1177 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1178 |
|
2363 | 1179 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1180 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1181 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1182 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1183 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1184 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1185 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1186 |
1 to:sz do:[:srcIndex | |
8874 | 1187 |
el := self at:srcIndex. |
1188 |
(anElement == el) ifTrue:[ |
|
1189 |
any := true |
|
1190 |
] ifFalse:[ |
|
1191 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1192 |
self at:dstIndex put:el |
|
1193 |
]. |
|
1194 |
dstIndex := dstIndex + 1 |
|
1195 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1196 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1197 |
any ifTrue:[ |
8874 | 1198 |
self grow:dstIndex - 1. |
1199 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1200 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1201 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1202 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1203 |
" |
8874 | 1204 |
#(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
|
1205 |
#(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
|
1206 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1207 |
|
2363 | 1208 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1209 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1210 |
|
602 | 1211 |
removeIndex:index |
1212 |
"remove the argument stored at index. Return the receiver. |
|
1213 |
||
1214 |
Notice, that this is modifies the receiver NOT a copy. |
|
1215 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1216 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1217 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1218 |
||
1219 |
self removeFromIndex:index toIndex:index |
|
1220 |
||
1221 |
" |
|
1222 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1223 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1224 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1225 |
" |
|
77 | 1226 |
! |
1227 |
||
602 | 1228 |
removeLast |
1229 |
"remove the last element of the receiver and return it. |
|
1230 |
||
1231 |
Notice, that this is modifies the receiver NOT a copy. |
|
1232 |
Also note, that it may be a slow operation for some collections, |
|
1233 |
due to the grow:-message, which is inefficient for fixed size |
|
1234 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1235 |
||
8874 | 1236 |
^ self removeAtIndex:(self size) |
602 | 1237 |
|
1238 |
" |
|
1239 |
|a| |
|
1240 |
a := #(1 2 3 4 5 6). |
|
1241 |
a removeLast. |
|
8874 | 1242 |
a |
602 | 1243 |
" |
1244 |
! ! |
|
1245 |
||
1246 |
!SequenceableCollection methodsFor:'comparing'! |
|
1247 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1248 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1249 |
"compare two sequenceable collections" |
8874 | 1250 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1251 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1252 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1253 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1254 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1255 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1256 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1257 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1258 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1259 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1260 |
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
|
1261 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1262 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1263 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1264 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1265 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1266 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1267 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1268 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1269 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1270 |
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
|
1271 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1272 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1273 |
(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
|
1274 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1275 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1276 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1277 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1278 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1279 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1280 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1281 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1282 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1283 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1284 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1285 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1286 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1287 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1288 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1289 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1290 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1291 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1292 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1293 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1294 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1295 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1296 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1297 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1298 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1299 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1300 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1301 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1302 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1303 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1304 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1305 |
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
|
1306 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1307 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1308 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1309 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1310 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1311 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1312 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1313 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1314 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1315 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1316 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1317 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1318 |
"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
|
1319 |
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
|
1320 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1321 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1322 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1323 |
|
602 | 1324 |
= aCollection |
1325 |
"return true if the receiver and aCollection represent collections |
|
1326 |
with equal contents." |
|
1327 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1328 |
| stop "{ Class: SmallInteger }" | |
602 | 1329 |
|
1330 |
(aCollection == self) ifTrue:[^true]. |
|
1331 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1332 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1333 |
|
1334 |
stop := self size. |
|
1335 |
stop == (aCollection size) ifFalse:[^false]. |
|
1336 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1337 |
1 to:stop do:[:index| |
9366 | 1338 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1339 |
]. |
1340 |
^ true |
|
1341 |
||
1342 |
" |
|
8874 | 1343 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1344 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1345 |
#($1 $2 $3 $4 $5) = '12345' |
|
1346 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1347 |
" |
1348 |
! |
|
1349 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1350 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1351 |
"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
|
1352 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1353 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1354 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1355 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1356 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1357 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1358 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1359 |
"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
|
1360 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1361 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1362 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1363 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1364 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1365 |
|
8874 | 1366 |
commonPrefixWith:aCollection |
6507 | 1367 |
"return the common prefix of myself and the argument, aCollection. |
1368 |
If there is none, an empty collection is returned." |
|
1369 |
||
1370 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1371 |
||
1372 |
" |
|
8874 | 1373 |
'hello' commonPrefixWith:'hello' |
1374 |
'hello' commonPrefixWith:'hElLo' |
|
1375 |
'hello' commonPrefixWith:'hello world' |
|
1376 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1377 |
||
1378 |
'hello world' commonPrefixWith:'hello' |
|
1379 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1380 |
||
1381 |
'abcd' commonPrefixWith:'bcde' |
|
1382 |
||
1383 |
'abcd' commonPrefixWith:'abab' |
|
1384 |
'abcd' commonPrefixWith:'ab' |
|
1385 |
'abcd' commonPrefixWith:'ababab' |
|
1386 |
'abcd' commonPrefixWith:'abcdef' |
|
1387 |
||
1388 |
'abab' commonPrefixWith:'abcd' |
|
1389 |
'ab' commonPrefixWith:'abcd' |
|
1390 |
'ababab' commonPrefixWith:'abcd' |
|
1391 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1392 |
" |
1393 |
! |
|
1394 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1395 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1396 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1397 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1398 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1399 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1400 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1401 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1402 |
1 to:matchLen do:[:idx | |
8874 | 1403 |
|elHere elThere same| |
1404 |
||
1405 |
elHere := self at:idx. |
|
1406 |
elThere := aCollection at:idx. |
|
1407 |
||
1408 |
ignoreCase ifTrue:[ |
|
1409 |
same := elHere sameAs:elThere |
|
1410 |
] ifFalse:[ |
|
1411 |
same := elHere = elThere |
|
1412 |
]. |
|
1413 |
same ifFalse:[ |
|
1414 |
^ self copyTo:(idx - 1). |
|
1415 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1416 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1417 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1418 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1419 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1420 |
" |
8874 | 1421 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1422 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1423 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1424 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1425 |
||
1426 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1427 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1428 |
||
1429 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1430 |
||
1431 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1432 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1433 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1434 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1435 |
||
1436 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1437 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1438 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1439 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1440 |
" |
|
1441 |
! |
|
1442 |
||
1443 |
commonSuffixWith:aCollection |
|
6507 | 1444 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1445 |
If there is none, an empty collection is returned." |
|
1446 |
||
1447 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1448 |
||
1449 |
" |
|
8874 | 1450 |
'hello' commonSuffixWith:'hello' |
1451 |
'hello' commonSuffixWith:'hElLo' |
|
1452 |
'hello' commonSuffixWith:'hello world' |
|
1453 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1454 |
'hello2 world' commonSuffixWith:'hello world' |
|
1455 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1456 |
||
1457 |
'hello world' commonSuffixWith:'world' |
|
1458 |
'hello WoRlD' commonSuffixWith:'world' |
|
1459 |
||
1460 |
'dcba' commonSuffixWith:'edcb' |
|
1461 |
||
1462 |
'dcba' commonSuffixWith:'baba' |
|
1463 |
'dcba' commonSuffixWith:'ba' |
|
1464 |
'dcba' commonSuffixWith:'bababa' |
|
1465 |
'dcba' commonSuffixWith:'fdcba' |
|
1466 |
||
1467 |
'baba' commonSuffixWith:'dcba' |
|
1468 |
'ba' commonSuffixWith:'dcba' |
|
1469 |
'bababa' commonSuffixWith:'dcba' |
|
1470 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1471 |
" |
1472 |
! |
|
1473 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1474 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1475 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1476 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1477 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1478 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1479 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1480 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1481 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1482 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1483 |
1 to:matchLen do:[:idx | |
8874 | 1484 |
|elHere elThere same| |
1485 |
||
1486 |
elHere := self at:(l1 - idx + 1). |
|
1487 |
elThere := aCollection at:(l2 - idx + 1). |
|
1488 |
||
1489 |
ignoreCase ifTrue:[ |
|
1490 |
same := elHere sameAs:elThere |
|
1491 |
] ifFalse:[ |
|
1492 |
same := elHere = elThere |
|
1493 |
]. |
|
1494 |
same ifFalse:[ |
|
1495 |
^ self copyFrom:(l1 - idx + 2). |
|
1496 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1497 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1498 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1499 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1500 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1501 |
" |
8874 | 1502 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1503 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1504 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1505 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1506 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1507 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1508 |
||
1509 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1510 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1511 |
||
1512 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1513 |
||
1514 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1515 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1516 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1517 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1518 |
||
1519 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1520 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1521 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1522 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1523 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1524 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1525 |
|
5813 | 1526 |
deepSameContentsAs:aCollection |
1527 |
"return true, if the receiver and the arg have the same contents |
|
1528 |
in both the named instance vars and any indexed instVars. |
|
1529 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1530 |
||
1531 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1532 |
unused space" |
|
1533 |
||
1534 |
|index "{ Class: SmallInteger }" |
|
1535 |
stop "{ Class: SmallInteger }" | |
|
1536 |
||
1537 |
(aCollection == self) ifTrue:[^true]. |
|
1538 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1539 |
||
1540 |
stop := self size. |
|
1541 |
stop == (aCollection size) ifFalse:[^false]. |
|
1542 |
||
1543 |
index := 1. |
|
1544 |
[index <= stop] whileTrue:[ |
|
8874 | 1545 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1546 |
index := index + 1 |
|
5813 | 1547 |
]. |
1548 |
^ true |
|
1549 |
||
1550 |
" |
|
8874 | 1551 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1552 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1553 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1554 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1555 |
" |
1556 |
! |
|
1557 |
||
602 | 1558 |
endsWith:aCollection |
1559 |
"return true, if the receivers last elements match those |
|
1560 |
of aCollection" |
|
1561 |
||
1562 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1563 |
index2 "{ Class: SmallInteger }" |
1564 |
stop "{ Class: SmallInteger }" |
|
602 | 1565 |
sz "{ Class: SmallInteger }"| |
1566 |
||
1567 |
(aCollection == self) ifTrue:[^ true]. |
|
1568 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1569 |
||
1570 |
stop := aCollection size. |
|
77 | 1571 |
sz := self size. |
602 | 1572 |
stop > sz ifTrue:[^false]. |
1573 |
||
1574 |
index1 := sz. |
|
1575 |
index2 := stop. |
|
1576 |
[index2 > 0] whileTrue:[ |
|
8874 | 1577 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1578 |
index1 := index1 - 1. |
|
1579 |
index2 := index2 - 1 |
|
77 | 1580 |
]. |
602 | 1581 |
^ true |
1582 |
||
1583 |
" |
|
1584 |
'abcde' endsWith:#($d $e) |
|
8874 | 1585 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1586 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1587 |
" |
|
1588 |
! |
|
1589 |
||
7278 | 1590 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1591 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1592 |
|
1593 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1594 |
||
1595 |
" |
|
1596 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1597 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1598 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1599 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1600 |
" |
1601 |
! |
|
1602 |
||
602 | 1603 |
hash |
1604 |
"return a hash key for the receiver" |
|
1605 |
||
1606 |
"this hash is stupid - but for larger collections, the hashing |
|
1607 |
time can become much bigger than the time lost in added probing. |
|
1608 |
Time will show ..." |
|
1609 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1610 |
|mySize h| |
3653 | 1611 |
|
1612 |
mySize := self size. |
|
1613 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1614 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1615 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1616 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1617 |
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
|
1618 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1619 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1620 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1621 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1622 |
"/ 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
|
1623 |
"/ ^ ((((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
|
1624 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1625 |
" |
8874 | 1626 |
#(1 2 3 4 5 6) hash |
1627 |
(1 to:6) hash |
|
1628 |
||
1629 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1630 |
#(1 2 3 4 5) hash |
|
602 | 1631 |
" |
3653 | 1632 |
|
1633 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1634 |
! |
1635 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1636 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1637 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1638 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1639 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1640 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1641 |
^ self |
8874 | 1642 |
sameContentsAs:aCollection |
1643 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1644 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1645 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1646 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1647 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1648 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1649 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1650 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1651 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1652 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1653 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1654 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1655 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1656 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1657 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1658 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1659 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1660 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1661 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1662 |
" |
8874 | 1663 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1664 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1665 |
#($1 $2 $3 $4 $5) = '12345' |
|
1666 |
||
1667 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1668 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1669 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1670 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1671 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1672 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1673 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1674 |
|
6118 | 1675 |
isSameSequenceAs: otherCollection |
8874 | 1676 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1677 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1678 |
" |
|
1679 |
||
1680 |
| size | |
|
1681 |
||
1682 |
(size := self size) = otherCollection size ifFalse: [^false]. |
|
1683 |
||
1684 |
1 to: size do:[:index | |
|
8874 | 1685 |
(self at: index) = (otherCollection at: index) ifFalse: [^false] |
6118 | 1686 |
]. |
1687 |
^ true |
|
1688 |
! |
|
1689 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1690 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1691 |
"return true if the receiver and aCollection represent collections |
8874 | 1692 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1693 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1694 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1695 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1696 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1697 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1698 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1699 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1700 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1701 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1702 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1703 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1704 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1705 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1706 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1707 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1708 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1709 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1710 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1711 |
[index <= stop] whileTrue:[ |
8874 | 1712 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1713 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1714 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1715 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1716 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1717 |
" |
8874 | 1718 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
1719 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1720 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1721 |
#(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
|
1722 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1723 |
#('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
|
1724 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1725 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1726 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1727 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1728 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1729 |
|
602 | 1730 |
startsWith:aCollection |
1731 |
"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
|
1732 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1733 |
If the argument is empty, true is returned." |
602 | 1734 |
|
1735 |
|index "{ Class: SmallInteger }" |
|
1736 |
stop "{ Class: SmallInteger }" | |
|
1737 |
||
1738 |
(aCollection == self) ifTrue:[^true]. |
|
1739 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1740 |
||
1741 |
stop := aCollection size. |
|
1742 |
stop > self size ifTrue:[^false]. |
|
1743 |
||
1744 |
index := 1. |
|
1745 |
[index <= stop] whileTrue:[ |
|
8874 | 1746 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1747 |
index := index + 1 |
|
602 | 1748 |
]. |
1749 |
^ true |
|
1750 |
||
1751 |
" |
|
1752 |
'abcde' startsWith:#($a $b $c) |
|
1753 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1754 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1755 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 1756 |
" |
7279 | 1757 |
! |
1758 |
||
1759 |
startsWithAnyOf:aCollectionOfCollections |
|
1760 |
"return true, if the receiver starts with any in aCollection" |
|
1761 |
||
1762 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
1763 |
||
1764 |
" |
|
1765 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
1766 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 1767 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
1768 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 1769 |
" |
13 | 1770 |
! ! |
1771 |
||
260 | 1772 |
!SequenceableCollection methodsFor:'converting'! |
1773 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1774 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 1775 |
"return a collection containing the subcollections (separated by anElement) |
1776 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1777 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1778 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1779 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1780 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1781 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1782 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1783 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1784 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1785 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1786 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1787 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1788 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1789 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1790 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1791 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1792 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1793 |
1 to:numberOfCols do:[:lineNr | |
8874 | 1794 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
1795 |
stopIndex == 0 ifTrue:[ |
|
1796 |
stopIndex := self size |
|
1797 |
] ifFalse: [ |
|
1798 |
stopIndex := stopIndex - 1. |
|
1799 |
]. |
|
1800 |
||
1801 |
(stopIndex < startIndex) ifTrue: [ |
|
1802 |
cols add:(myClass new:0) |
|
1803 |
] ifFalse: [ |
|
1804 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
1805 |
]. |
|
1806 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1807 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1808 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1809 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1810 |
" |
8874 | 1811 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1812 |
'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
|
1813 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1814 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1815 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1816 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1817 |
|
9234 | 1818 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
1819 |
"evaluate aBlock for each subcollection generated by separating elements |
|
1820 |
of the receiver by anElement. |
|
1821 |
If anElement occurs multiple times in a row, |
|
1822 |
the block will be invoked with empty collections as argument. |
|
1823 |
This algorithm uses equality-compare to detect the element." |
|
1824 |
||
1825 |
|subCollection |
|
1826 |
endIndex "{ Class:SmallInteger }" |
|
1827 |
startIndex "{ Class:SmallInteger }" |
|
1828 |
stopIndex "{ Class:SmallInteger }" | |
|
1829 |
||
1830 |
startIndex := 0. |
|
1831 |
endIndex := self size. |
|
1832 |
||
1833 |
[startIndex <= endIndex] whileTrue:[ |
|
1834 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
1835 |
stopIndex == 0 ifTrue:[ |
|
1836 |
stopIndex := self size |
|
1837 |
] ifFalse: [ |
|
1838 |
stopIndex := stopIndex - 1. |
|
1839 |
]. |
|
1840 |
||
1841 |
(stopIndex < startIndex) ifTrue: [ |
|
1842 |
subCollection := self species new:0 |
|
1843 |
] ifFalse: [ |
|
1844 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
1845 |
]. |
|
1846 |
aBlock value:subCollection. |
|
1847 |
startIndex := stopIndex + 1 |
|
1848 |
]. |
|
1849 |
||
1850 |
" |
|
1851 |
'' |
|
1852 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1853 |
||
1854 |
'1 one' |
|
1855 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1856 |
||
1857 |
'1 one:2 two:3 three:4 four:5 five' |
|
1858 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1859 |
||
1860 |
'a::b' |
|
1861 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1862 |
||
1863 |
':' |
|
1864 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1865 |
||
1866 |
':a' |
|
1867 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1868 |
||
1869 |
'a:' |
|
1870 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1871 |
" |
|
1872 |
! |
|
1873 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1874 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 1875 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
1876 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1877 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1878 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1879 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1880 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1881 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1882 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1883 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1884 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1885 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1886 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1887 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1888 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1889 |
[done] whileFalse:[ |
8874 | 1890 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
1891 |
stopIndex == 0 ifTrue:[ |
|
1892 |
stopIndex := self size. |
|
1893 |
done := true. |
|
1894 |
] ifFalse: [ |
|
1895 |
stopIndex := stopIndex - 1. |
|
1896 |
]. |
|
1897 |
||
1898 |
(stopIndex < startIndex) ifTrue: [ |
|
1899 |
items add:(myClass new:0) |
|
1900 |
] ifFalse: [ |
|
1901 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
1902 |
]. |
|
1903 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1904 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1905 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1906 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1907 |
" |
8874 | 1908 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
1909 |
#(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
|
1910 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1911 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1912 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1913 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 1914 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1915 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1916 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1917 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1918 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1919 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1920 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1921 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1922 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 1923 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
1924 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
1925 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
1926 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
1927 |
#(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
|
1928 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1929 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1930 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1931 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 1932 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1933 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1934 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1935 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1936 |
|words |
8874 | 1937 |
start "{ Class:SmallInteger }" |
1938 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1939 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1940 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1941 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1942 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1943 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1944 |
[start <= mySize] whileTrue:[ |
8874 | 1945 |
"skip multiple separators" |
1946 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
1947 |
start := start + 1 . |
|
1948 |
start > mySize ifTrue:[ |
|
1949 |
^ words |
|
1950 |
]. |
|
1951 |
]. |
|
1952 |
||
1953 |
stop := self findFirst:aBlock startingAt:start. |
|
1954 |
stop == 0 ifTrue:[ |
|
1955 |
words add:(self copyFrom:start to:mySize). |
|
1956 |
^ words |
|
1957 |
]. |
|
1958 |
words add:(self copyFrom:start to:(stop - 1)). |
|
1959 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1960 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1961 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1962 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1963 |
" |
8874 | 1964 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
1965 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1966 |
#(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
|
1967 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1968 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1969 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1970 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1971 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1972 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1973 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1974 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1975 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1976 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1977 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1978 |
|
1413 | 1979 |
asStringCollection |
1980 |
"return a new string collection containing the elements; |
|
1981 |
these ought to be strings. (i.e. String or Text instances)" |
|
1982 |
||
1983 |
|newColl sz| |
|
1984 |
||
1985 |
sz := self size. |
|
9162 | 1986 |
newColl := StringCollection withSize:sz. |
1413 | 1987 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
1988 |
^ newColl |
|
1989 |
||
1990 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
1991 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
1992 |
! |
|
1993 |
||
1188 | 1994 |
asStringWith:sepChar |
8874 | 1995 |
"return a string generated by concatenating my elements |
1188 | 1996 |
(which must be strings or nil) and embedding sepChar characters in between. |
1997 |
Nil entries and empty strings are counted as empty lines." |
|
1998 |
||
8874 | 1999 |
^ self |
2000 |
from:1 to:(self size) |
|
2001 |
asStringWith:sepChar |
|
2002 |
compressTabs:false |
|
2003 |
final:nil |
|
1413 | 2004 |
|
2005 |
" |
|
2006 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
2007 |
" |
|
2008 |
||
2009 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 2010 |
! |
2011 |
||
2012 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2013 |
"return part of myself as a string with embedded sepCharacters. |
|
2014 |
My elements must be strings or nil; nil entries and empty strings are |
|
2015 |
taken as empty lines." |
|
2016 |
||
8874 | 2017 |
^ self |
2018 |
from:firstLine to:lastLine |
|
2019 |
asStringWith:sepCharacter |
|
2020 |
compressTabs:false |
|
2021 |
final:nil |
|
1188 | 2022 |
" |
2023 |
creating entries for searchpath: |
|
2024 |
||
8874 | 2025 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2026 |
||
2027 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2028 |
" |
2029 |
||
2030 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2031 |
! |
|
2032 |
||
260 | 2033 |
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
|
2034 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2035 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2036 |
taken as empty lines. |
2037 |
If the argument compressTabs is true, leading spaces are converted |
|
2038 |
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
|
2039 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2040 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2041 |
^ self |
8874 | 2042 |
from:firstLine to:lastLine |
2043 |
asStringWith:sepCharacter |
|
2044 |
compressTabs:compressTabs |
|
2045 |
final:endCharacter |
|
2046 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2047 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2048 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2049 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2050 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2051 |
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
|
2052 |
"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
|
2053 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2054 |
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
|
2055 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2056 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2057 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2058 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2059 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2060 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2061 |
any emphasis. If false, a plain string is returned. |
260 | 2062 |
This method is tuned for big collections, in not creating many |
263 | 2063 |
intermediate strings (has linear runtime). For very small collections |
2064 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2065 |
|
5575 | 2066 |
^ self |
8874 | 2067 |
from:firstLine to:lastLine |
2068 |
asStringWith:sepCharacter |
|
2069 |
compressTabs:compressTabs |
|
2070 |
final:endCharacter |
|
2071 |
withEmphasis:withEmphasis |
|
5575 | 2072 |
! |
2073 |
||
2074 |
asStringWithCRs |
|
8874 | 2075 |
"return a string generated by concatenating my elements |
5575 | 2076 |
(which must be strings or nil) and embedding cr characters in between. |
2077 |
Nil entries and empty strings are counted as empty lines." |
|
2078 |
||
2079 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2080 |
||
2081 |
" |
|
8874 | 2082 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2083 |
|
2084 |
(OrderedCollection new |
|
8874 | 2085 |
add:'hello'; |
2086 |
add:'world'; |
|
2087 |
add:'foo'; |
|
2088 |
add:('bar' asText allBold); |
|
2089 |
yourself) asStringWithCRs |
|
5575 | 2090 |
|
2091 |
Transcript showCR: |
|
8874 | 2092 |
(OrderedCollection new |
2093 |
add:'hello'; |
|
2094 |
add:'world'; |
|
2095 |
add:'foo'; |
|
2096 |
add:('bar' asText allBold); |
|
2097 |
yourself) asStringWithCRs |
|
5575 | 2098 |
" |
2099 |
||
2100 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2101 |
! |
|
2102 |
||
2103 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2104 |
"return a string generated by concatenating some of my elements |
5575 | 2105 |
(which must be strings or nil) and embedding cr characters in between. |
2106 |
Nil entries and empty strings are counted as empty lines." |
|
2107 |
||
2108 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2109 |
||
2110 |
" |
|
8874 | 2111 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2112 |
|
2113 |
" |
|
2114 |
||
2115 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2116 |
! |
|
2117 |
||
2118 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2119 |
"return part of myself as a string with embedded cr's. |
|
2120 |
My elements must be strings or nil. |
|
2121 |
If the argument compressTabs is true, leading spaces are converted |
|
2122 |
to tab-characters (8col tabs). |
|
2123 |
Nil entries and empty strings are taken as empty lines." |
|
2124 |
||
2125 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2126 |
! |
|
2127 |
||
2128 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2129 |
"return part of myself as a string with embedded cr's. |
|
2130 |
My elements must be strings or nil; nil entries and empty strings are |
|
2131 |
taken as empty lines. |
|
2132 |
If the argument compressTabs is true, leading spaces are converted |
|
2133 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2134 |
should be followed by a cr or not." |
|
2135 |
||
2136 |
^ self asStringWith:(Character cr) |
|
8874 | 2137 |
from:firstLine |
2138 |
to:lastLine |
|
2139 |
compressTabs:compressTabs |
|
2140 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2141 |
! |
2142 |
||
2143 |
decodeAsLiteralArray |
|
2144 |
"given a literalEncoding in the receiver, |
|
2145 |
create & return the corresponding object. |
|
2146 |
The inverse operation to #literalArrayEncoding." |
|
2147 |
||
9414 | 2148 |
|clsName cls| |
5575 | 2149 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2150 |
self size == 0 ifTrue:[ |
9358 | 2151 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2152 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2153 |
|
9414 | 2154 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2155 |
clsName isSymbol ifFalse:[ |
9358 | 2156 |
^ MissingClassInLiteralArrayErrorSignal |
2157 |
raiseRequestWith:clsName |
|
9414 | 2158 |
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
|
2159 |
]. |
9414 | 2160 |
cls := Smalltalk classNamed:clsName. |
5575 | 2161 |
cls isNil ifTrue:[ |
9414 | 2162 |
^ MissingClassInLiteralArrayErrorSignal |
2163 |
raiseRequestWith:clsName |
|
2164 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2165 |
]. |
9414 | 2166 |
|
5575 | 2167 |
^ cls decodeFromLiteralArray:self. |
2168 |
||
2169 |
" |
|
10476 | 2170 |
#(Array 10 20) decodeAsLiteralArray |
2171 |
#(10 20) literalArrayEncoding |
|
2172 |
" |
|
2173 |
||
2174 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2175 |
! |
2176 |
||
8874 | 2177 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2178 |
"return part of myself as a string with embedded sepCharacters. |
2179 |
My elements must be strings or nil; nil entries and empty strings are |
|
2180 |
taken as empty lines." |
|
2181 |
||
8874 | 2182 |
^ self |
2183 |
from:firstLine |
|
2184 |
to:lastLine |
|
2185 |
asStringWith:sepCharacter |
|
2186 |
compressTabs:false |
|
2187 |
final:nil |
|
5575 | 2188 |
" |
2189 |
creating entries for searchpath: |
|
2190 |
||
8874 | 2191 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2192 |
||
2193 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2194 |
||
2195 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2196 |
" |
2197 |
||
2198 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2199 |
! |
|
2200 |
||
2201 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2202 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2203 |
My elements must be strings or nil; nil entries and empty strings are |
|
2204 |
taken as empty lines. |
|
2205 |
If the argument compressTabs is true, leading spaces are converted |
|
2206 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2207 |
character (if non-nil)." |
|
2208 |
||
2209 |
^ self |
|
8874 | 2210 |
from:firstLine |
2211 |
to:lastLine |
|
2212 |
asStringWith:sepCharacter |
|
2213 |
compressTabs:compressTabs |
|
2214 |
final:endCharacter |
|
2215 |
withEmphasis:true |
|
5575 | 2216 |
|
2217 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2218 |
! |
|
2219 |
||
2220 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2221 |
"return part of myself as a string or text with embedded sepCharacters |
|
2222 |
and followup endCharacter. |
|
2223 |
My elements must be strings or nil; nil entries and empty strings are |
|
2224 |
taken as empty lines. |
|
2225 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2226 |
If the argument compressTabs is true, leading spaces are converted |
|
2227 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2228 |
character (if non-nil). |
|
2229 |
The withEmphais argument controls if the returned string should preserve |
|
2230 |
any emphasis. If false, a plain string is returned. |
|
2231 |
This method is tuned for big collections, in not creating many |
|
2232 |
intermediate strings (has linear runtime). For very small collections |
|
2233 |
and small strings, it may be faster to use the comma , operation." |
|
2234 |
||
293 | 2235 |
|idx1 "{ Class:SmallInteger }" |
2236 |
idx2 "{ Class:SmallInteger }" |
|
2237 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2238 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2239 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2240 |
newString lineString spaces idx nTabs |
8099 | 2241 |
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
|
2242 |
thisLen anyTab| |
260 | 2243 |
|
2244 |
" |
|
2245 |
first accumulate the size of the string, to avoid |
|
2246 |
countless reallocations. If tabs are compressed, |
|
2247 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2248 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2249 |
" |
8099 | 2250 |
needEmphasis := false. |
2251 |
maxBitsPerCharacter := 8. |
|
1015 | 2252 |
|
260 | 2253 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2254 |
sepCharacter isNil ifTrue:[ |
8874 | 2255 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2256 |
] ifFalse:[ |
8874 | 2257 |
sepCharacter isCharacter ifTrue:[ |
2258 |
sepCnt := 1 |
|
2259 |
] ifFalse:[ |
|
2260 |
sepCnt := sepCharacter size |
|
2261 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2262 |
]. |
260 | 2263 |
|
293 | 2264 |
idx1 := firstLine. |
2265 |
idx2 := lastLine. |
|
2266 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2267 |
lineString := self at:lineIndex. |
2268 |
||
2269 |
lineString notNil ifTrue:[ |
|
2270 |
withEmphasis ifTrue:[ |
|
2271 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2272 |
needEmphasis := true |
|
2273 |
]. |
|
2274 |
]. |
|
2275 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2276 |
totalLength := totalLength + lineString size |
|
2277 |
]. |
|
2278 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2279 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2280 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2281 |
|
8099 | 2282 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2283 |
maxBitsPerCharacter > 16 ifTrue:[ |
2284 |
stringClass := Unicode32String. |
|
2285 |
] ifFalse:[ |
|
2286 |
stringClass := Unicode16String. |
|
2287 |
] |
|
8099 | 2288 |
] ifFalse:[ |
8874 | 2289 |
stringClass := String. |
8099 | 2290 |
]. |
2291 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2292 |
endCharacter notNil ifTrue:[ |
8874 | 2293 |
endCharacter isCharacter ifTrue:[ |
2294 |
totalLength := totalLength + 1 |
|
2295 |
] ifFalse:[ |
|
2296 |
totalLength := totalLength + endCharacter size |
|
2297 |
]. |
|
260 | 2298 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2299 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2300 |
|
260 | 2301 |
spaces := ' '. |
1016 | 2302 |
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
|
2303 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2304 |
needEmphasis ifTrue:[ |
8874 | 2305 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2306 |
]. |
260 | 2307 |
|
2308 |
" |
|
2309 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2310 |
Be careful with runArrays: |
8874 | 2311 |
replacing individual elements is VERY expensive. |
2312 |
Therefore, create a new runArray from scratch. |
|
260 | 2313 |
" |
2314 |
pos := 1. |
|
293 | 2315 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2316 |
lineString := self at:lineIndex. |
2317 |
thisLen := lineString size. |
|
2318 |
thisLen ~~ 0 ifTrue:[ |
|
2319 |
withEmphasis ifFalse:[ |
|
2320 |
lineString := lineString string. |
|
2321 |
]. |
|
2322 |
||
2323 |
(anyTab := compressTabs) ifTrue:[ |
|
2324 |
" |
|
2325 |
mhmh: could use withTabs from String-class here, |
|
2326 |
but we should avoid creating too many temporary strings |
|
2327 |
(especially, since this method is typically used when converting |
|
2328 |
big texts such as when saving in the filebrowser ...). |
|
2329 |
Therefore, we convert tabs inline here doing a direct replace |
|
2330 |
in newString." |
|
2331 |
||
2332 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2333 |
nTabs := (idx-1) // 8. |
|
2334 |
anyTab := (nTabs > 0) |
|
2335 |
]. |
|
2336 |
anyTab ifTrue:[ |
|
2337 |
"any tabs" |
|
2338 |
||
2339 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2340 |
||
2341 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2342 |
newRuns notNil ifTrue:[ |
|
2343 |
newRuns add:nil withOccurrences:nTabs |
|
2344 |
]. |
|
2345 |
pos := pos + nTabs. |
|
2346 |
||
2347 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2348 |
newRuns notNil ifTrue:[ |
|
2349 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2350 |
idx to:lineString size do:[:pos | |
|
2351 |
newRuns add:(lineString emphasisAt:pos) |
|
2352 |
] |
|
2353 |
] ifFalse:[ |
|
2354 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2355 |
] |
|
2356 |
]. |
|
2357 |
pos := pos + thisLen - (nTabs * 8). |
|
2358 |
] ifFalse:[ |
|
2359 |
newString replaceFrom:pos with:lineString. |
|
2360 |
||
2361 |
newRuns notNil ifTrue:[ |
|
2362 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2363 |
newRuns addAll:(lineString emphasis) |
|
2364 |
] ifFalse:[ |
|
2365 |
newRuns add:nil withOccurrences:lineString size |
|
2366 |
] |
|
2367 |
]. |
|
2368 |
pos := pos + thisLen. |
|
2369 |
]. |
|
2370 |
]. |
|
2371 |
||
2372 |
lineIndex ~~ lastLine ifTrue:[ |
|
2373 |
c := sepCharacter |
|
2374 |
] ifFalse:[ |
|
2375 |
c := endCharacter |
|
2376 |
]. |
|
2377 |
||
2378 |
c notNil ifTrue:[ |
|
2379 |
c isCharacter ifTrue:[ |
|
2380 |
newString at:pos put:c. |
|
2381 |
newRuns notNil ifTrue:[ |
|
2382 |
newRuns add:nil. |
|
2383 |
]. |
|
2384 |
pos := pos + 1 |
|
2385 |
] ifFalse:[ |
|
2386 |
newString replaceFrom:pos with:c. |
|
2387 |
newRuns notNil ifTrue:[ |
|
2388 |
newRuns add:nil withOccurrences:c size |
|
2389 |
]. |
|
2390 |
pos := pos + c size |
|
2391 |
] |
|
2392 |
]. |
|
260 | 2393 |
]. |
2394 |
||
2395 |
" |
|
2396 |
in case of tab compression, the result has to be |
|
2397 |
cut to size ... sorry |
|
2398 |
" |
|
2399 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2400 |
newString := newString copyTo:(pos - 1) |
260 | 2401 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2402 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2403 |
newRuns notNil ifTrue:[ |
8874 | 2404 |
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
|
2405 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2406 |
|
260 | 2407 |
^ newString |
2408 |
||
2409 |
" |
|
2410 |
creating entries for searchpath: |
|
2411 |
||
8874 | 2412 |
#('foo' 'bar' 'baz' '/foo/bar') |
2413 |
asStringWith:$: |
|
2414 |
from:1 to:4 |
|
2415 |
compressTabs:false |
|
2416 |
final:nil |
|
260 | 2417 |
|
2418 |
with trailing colon: |
|
2419 |
||
8874 | 2420 |
#('foo' 'bar' 'baz' '/foo/bar') |
2421 |
asStringWith:$: |
|
2422 |
from:1 to:4 |
|
2423 |
compressTabs:false |
|
2424 |
final:$: |
|
260 | 2425 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2426 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2427 |
|
8874 | 2428 |
#('foo' 'bar' 'baz') |
2429 |
asStringWith:nil |
|
2430 |
from:1 to:3 |
|
2431 |
compressTabs:false |
|
2432 |
final:nil |
|
260 | 2433 |
|
2434 |
creating a string from a collection of lines: |
|
2435 |
||
8874 | 2436 |
#('foo' 'bar' 'baz') |
2437 |
asStringWith:(Character cr) |
|
2438 |
from:1 to:3 |
|
2439 |
compressTabs:false |
|
2440 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2441 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2442 |
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
|
2443 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2444 |
(Array |
8874 | 2445 |
with:'foo' |
2446 |
with:('bar' asText allBold) |
|
2447 |
with:'baz' |
|
2448 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2449 |
) |
|
2450 |
asStringWith:(Character cr) |
|
2451 |
from:1 to:4 |
|
2452 |
compressTabs:false |
|
2453 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2454 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2455 |
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
|
2456 |
|
8874 | 2457 |
#('foo' 'bar' 'baz') |
2458 |
asStringWith:'__' |
|
2459 |
from:1 to:3 |
|
2460 |
compressTabs:false |
|
2461 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2462 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2463 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2464 |
|
8874 | 2465 |
#('foo' 'bar' 'baz') |
2466 |
asStringWith:'__' |
|
2467 |
from:1 to:3 |
|
2468 |
compressTabs:false |
|
2469 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2470 |
|
260 | 2471 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2472 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2473 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2474 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
260 | 2475 |
! ! |
2476 |
||
7253 | 2477 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2478 |
||
8874 | 2479 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2480 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2481 |
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
|
2482 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2483 |
however, physically, no copy is made). |
9291 | 2484 |
Warning: |
2485 |
The slice SHARES the memory for the element-data with the original, |
|
2486 |
this means that any modifications in the original are visible in the slice |
|
2487 |
and vice versa." |
|
7253 | 2488 |
|
2489 |
^ self |
|
9291 | 2490 |
from:startIndex |
2491 |
to:self size |
|
2492 |
by:1 |
|
8874 | 2493 |
|
2494 |
" |
|
2495 |
#(1 2 3 4 5 6 7) from:3 |
|
2496 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2497 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2498 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2499 |
" |
|
2500 |
! |
|
2501 |
||
2502 |
from:startIndex by:step |
|
9291 | 2503 |
"Create a ReindexedCollection from the receiver. |
2504 |
The new collection represents the elements starting at startIndex to the end. |
|
2505 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2506 |
however, physically, no copy is made). |
|
2507 |
Warning: |
|
2508 |
The slice SHARES the memory for the element-data with the original, |
|
2509 |
this means that any modifications in the original are visible in the slice |
|
2510 |
and vice versa." |
|
7253 | 2511 |
|
2512 |
^ self |
|
9291 | 2513 |
from:startIndex |
2514 |
to:(step > 1 |
|
2515 |
ifTrue: [self size] |
|
2516 |
ifFalse: [1]) |
|
2517 |
by:step |
|
8874 | 2518 |
|
2519 |
" |
|
2520 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2521 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2522 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2523 |
( #(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
|
2524 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2525 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2526 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2527 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2528 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2529 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2530 |
Warning: |
2531 |
The slice SHARES the memory for the element-data with the original, |
|
2532 |
this means that any modifications in the original are visible in the slice |
|
2533 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2534 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2535 |
^ self |
9291 | 2536 |
from:startIndex |
2537 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2538 |
|
2539 |
" |
|
2540 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2541 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2542 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2543 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2544 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2545 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2546 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2547 |
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
|
2548 |
slice := coll from:3 to:7. |
8874 | 2549 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2550 |
coll at:4 put:40. |
8874 | 2551 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2552 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2553 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2554 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2555 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2556 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2557 |
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
|
2558 |
slice := coll from:3 to:7. |
8874 | 2559 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2560 |
slice at:1 put:40. |
8874 | 2561 |
slice. |
2562 |
" |
|
2563 |
! |
|
2564 |
||
2565 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2566 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2567 |
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
|
2568 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2569 |
however, physically, no copy is made). |
9291 | 2570 |
Warning: |
2571 |
The slice SHARES the memory for the element-data with the original, |
|
2572 |
this means that any modifications in the original are visible in the slice |
|
2573 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2574 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2575 |
^ self |
9291 | 2576 |
from:startIndex |
2577 |
to:endIndex |
|
2578 |
by: (startIndex <= endIndex |
|
2579 |
ifTrue: [1] |
|
2580 |
ifFalse: [-1]) |
|
8874 | 2581 |
|
2582 |
" |
|
2583 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2584 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2585 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2586 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2587 |
" |
|
2588 |
! |
|
2589 |
||
2590 |
from:startIndex to:endIndex by:step |
|
9291 | 2591 |
"Create a ReindexedCollection from the receiver. |
2592 |
The new collection represents the receivers elements up to endIndex. |
|
2593 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2594 |
however, physically, no copy is made). |
|
2595 |
Warning: |
|
2596 |
The slice SHARES the memory for the element-data with the original, |
|
2597 |
this means that any modifications in the original are visible in the slice |
|
2598 |
and vice versa." |
|
7253 | 2599 |
|
2600 |
^ ReindexedCollection |
|
9291 | 2601 |
on:self |
2602 |
from:startIndex |
|
2603 |
to:endIndex |
|
2604 |
by:step |
|
8874 | 2605 |
|
2606 |
" |
|
2607 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2608 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2609 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2610 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2611 |
" |
|
2612 |
! |
|
2613 |
||
2614 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2615 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2616 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2617 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2618 |
however, physically, no copy is made). |
9291 | 2619 |
Warning: |
2620 |
The slice SHARES the memory for the element-data with the original, |
|
2621 |
this means that any modifications in the original are visible in the slice |
|
2622 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2623 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2624 |
^ self |
9291 | 2625 |
from:1 |
2626 |
to:endIndex |
|
2627 |
by:1 |
|
8874 | 2628 |
|
2629 |
" |
|
2630 |
#(1 2 3 4 5 6 7) to:4 |
|
2631 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2632 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2633 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2634 |
" |
|
2635 |
! |
|
2636 |
||
2637 |
to:endIndex by:step |
|
9291 | 2638 |
"Create a ReindexedCollection from the receiver. |
2639 |
The new collection represents the receivers elements up to endIndex. |
|
2640 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2641 |
however, physically, no copy is made). |
|
2642 |
Warning: |
|
2643 |
The slice SHARES the memory for the element-data with the original, |
|
2644 |
this means that any modifications in the original are visible in the slice |
|
2645 |
and vice versa." |
|
7253 | 2646 |
|
2647 |
^ self |
|
9291 | 2648 |
from:(step > 0 |
2649 |
ifTrue: [1] |
|
2650 |
ifFalse: [self size]) |
|
2651 |
to:endIndex |
|
2652 |
by:step |
|
8874 | 2653 |
|
2654 |
" |
|
2655 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2656 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2657 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2658 |
( #(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
|
2659 |
" |
7253 | 2660 |
! ! |
2661 |
||
1 | 2662 |
!SequenceableCollection methodsFor:'copying'! |
2663 |
||
9106 | 2664 |
++ aCollection |
2665 |
"return a new collection formed from concatenating the receiver with |
|
2666 |
the argument. The class of the new collection is determined by the |
|
2667 |
receivers class, so mixing classes is possible, if the second collections |
|
2668 |
elements can be stored into instances of the receivers class." |
|
2669 |
||
2670 |
^ self , aCollection |
|
2671 |
||
2672 |
" |
|
2673 |
#($a $b $c) ++ #(1 2 3) |
|
2674 |
#($a $b $c) ++ '123' |
|
2675 |
'abc' ++ '123' |
|
2676 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2677 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2678 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2679 |
" |
|
2680 |
! |
|
2681 |
||
1 | 2682 |
, aCollection |
2683 |
"return a new collection formed from concatenating the receiver with |
|
44 | 2684 |
the argument. The class of the new collection is determined by the |
2685 |
receivers class, so mixing classes is possible, if the second collections |
|
2686 |
elements can be stored into instances of the receivers class." |
|
1 | 2687 |
|
8874 | 2688 |
|newCollection |
1 | 2689 |
mySize "{ Class: SmallInteger }" |
2690 |
newSize "{ Class: SmallInteger }" |
|
2691 |
otherSize "{ Class: SmallInteger }" |
|
2692 |
dstIndex "{ Class: SmallInteger }"| |
|
2693 |
||
2694 |
mySize := self size. |
|
2695 |
otherSize := aCollection size. |
|
2696 |
newSize := mySize + otherSize. |
|
202 | 2697 |
|
252 | 2698 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 2699 |
|
2700 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
2701 |
dstIndex := mySize + 1. |
|
202 | 2702 |
aCollection isSequenceable ifTrue:[ |
8874 | 2703 |
" |
2704 |
yes, aCollection has indexed elements |
|
2705 |
" |
|
2706 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 2707 |
] ifFalse:[ |
8874 | 2708 |
" |
2709 |
no, enumerate aCollection |
|
2710 |
" |
|
2711 |
aCollection do:[:element | |
|
2712 |
newCollection at:dstIndex put:element. |
|
2713 |
dstIndex := dstIndex + 1 |
|
2714 |
] |
|
1 | 2715 |
]. |
2716 |
^ newCollection |
|
44 | 2717 |
|
95 | 2718 |
" |
2719 |
#($a $b $c) , #(1 2 3) |
|
2720 |
#($a $b $c) , '123' |
|
2721 |
'abc' , '123' |
|
2722 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
2723 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2724 |
'abc' asArray , #(1 2 3 4 5) |
|
2725 |
" |
|
1 | 2726 |
! |
2727 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2728 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2729 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2730 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2731 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2732 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2733 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2734 |
" |
8874 | 2735 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2736 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2737 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2738 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2739 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2740 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2741 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2742 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2743 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2744 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2745 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2746 |
" |
8874 | 2747 |
#($a $b $c $d $e $f $g) copyButLast:2 |
2748 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2749 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2750 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2751 |
|
602 | 2752 |
copyFirst:count |
2753 |
"return a new collection consisting of the receivers first count |
|
2754 |
elements - this is just a rename of copyTo: - for compatibility." |
|
2755 |
||
2756 |
^ self copyFrom:1 to:count |
|
2757 |
||
2758 |
" |
|
2759 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
2760 |
'1234567890' copyFirst:4 |
|
2761 |
" |
|
2762 |
! |
|
2763 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2764 |
copyFrom:startIndex |
9234 | 2765 |
"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
|
2766 |
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
|
2767 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2768 |
^ self copyFrom:startIndex to:(self size) |
602 | 2769 |
|
2770 |
" |
|
2771 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
2772 |
'1234567890' copyFrom:2 |
|
8874 | 2773 |
(10 to:19) copyFrom:5 |
602 | 2774 |
" |
2775 |
! |
|
2776 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2777 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2778 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2779 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2780 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2781 |
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
|
2782 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2783 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2784 |
^ self |
8874 | 2785 |
copyFrom:startIndex |
2786 |
to:(startIndex + numberOfElements - 1) |
|
2787 |
||
2788 |
" |
|
2789 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
2790 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2791 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2792 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2793 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2794 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2795 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2796 |
copyFrom:startIndex to:stopIndex |
602 | 2797 |
"return a new collection consisting of receivers elements |
4403 | 2798 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2799 |
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
|
2800 |
Raise an error, if stopIndex is out of range." |
602 | 2801 |
|
2802 |
|newCollection newSize| |
|
2803 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2804 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2805 |
newSize < 0 ifTrue:[ |
8874 | 2806 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2807 |
]. |
602 | 2808 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2809 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 2810 |
^ newCollection |
2811 |
||
2812 |
" |
|
2813 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
2814 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 2815 |
(1 to:10) copyFrom:2 to:5 |
2816 |
||
2817 |
'1234567890' copyFrom:2 to:15 |
|
2818 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 2819 |
" |
2820 |
! |
|
2821 |
||
2822 |
copyLast:count |
|
2823 |
"return a new collection consisting of the receivers last count |
|
2824 |
elements." |
|
2825 |
||
2826 |
|sz| |
|
2827 |
||
2828 |
sz := self size. |
|
2829 |
^ self copyFrom:(sz - count + 1) to:sz |
|
2830 |
||
2831 |
" |
|
2832 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
2833 |
'1234567890' copyLast:4 |
|
2834 |
" |
|
2835 |
! |
|
2836 |
||
3653 | 2837 |
copyReplaceAll:oldElement with:newElement |
2838 |
"return a copy of the receiver, where all elements equal to oldElement |
|
2839 |
have been replaced by newElement." |
|
2840 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2841 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 2842 |
^ self copy replaceAll:oldElement with:newElement |
2843 |
||
2844 |
" |
|
8874 | 2845 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
2846 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 2847 |
" |
2848 |
||
2849 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
2850 |
! |
|
2851 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2852 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2853 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2854 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2855 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2856 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2857 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2858 |
|
5489 | 2859 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2860 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2861 |
"/ '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
|
2862 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2863 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2864 |
self do:[:el | |
8874 | 2865 |
el = oldObject ifTrue:[ |
2866 |
aCollection do:[:el2 | |
|
2867 |
s nextPut:el2 |
|
2868 |
] |
|
2869 |
] ifFalse:[ |
|
2870 |
s nextPut:el |
|
2871 |
] |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2872 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2873 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2874 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2875 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2876 |
args: oldObject : <object> |
8874 | 2877 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2878 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2879 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2880 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2881 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2882 |
" |
8874 | 2883 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
2884 |
#(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
|
2885 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2886 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2887 |
|
602 | 2888 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
2889 |
"return a copy of the receiver, where the elements from startIndex to |
|
2890 |
endIndex have been replaced by the elements of aCollection" |
|
2891 |
||
2892 |
|newColl sz replSize| |
|
2893 |
||
2894 |
replSize := aCollection size. |
|
2895 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
2896 |
newColl := self copyEmptyAndGrow:sz. |
|
2897 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
2898 |
newColl replaceFrom:startIndex with:aCollection. |
|
2899 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
2900 |
^ newColl |
|
2901 |
||
2902 |
" |
|
2903 |
#(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 | 2904 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 2905 |
" |
2906 |
! |
|
2907 |
||
2908 |
copyReplaceFrom:startIndex with:aCollection |
|
2909 |
"return a copy of the receiver, where the elements from startIndex to |
|
2910 |
the end have been replaced by the elements of aCollection" |
|
2911 |
||
2912 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
2913 |
||
2914 |
" |
|
8874 | 2915 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
2916 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 2917 |
" |
2918 |
! |
|
2919 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2920 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2921 |
"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
|
2922 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2923 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2924 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2925 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2926 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2927 |
" |
8874 | 2928 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
2929 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2930 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2931 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2932 |
|
602 | 2933 |
copyThrough:element |
2934 |
"return a new collection consisting of the receiver elements |
|
8874 | 2935 |
up-to (AND including) the first occurrence of element." |
602 | 2936 |
|
2937 |
|idx| |
|
2938 |
||
2939 |
idx := self indexOf:element. |
|
2940 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
2941 |
^ self copyFrom:1 to:idx |
|
2942 |
||
2943 |
" |
|
2944 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
2945 |
'1234567890' copyThrough:$5 |
|
2946 |
'1234567890' copyThrough:$a |
|
2947 |
'1234567890' copyThrough:$1 |
|
2948 |
" |
|
2949 |
! |
|
2950 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2951 |
copyTo:stopIndex |
9234 | 2952 |
"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
|
2953 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2954 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2955 |
^ self copyFrom:1 to:stopIndex |
602 | 2956 |
|
2957 |
" |
|
2958 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
2959 |
'1234567890' copyTo:4 |
|
2960 |
" |
|
2961 |
! |
|
2962 |
||
5526 | 2963 |
copyToMax:stop |
2964 |
"return a new collection consisting of receivers elements |
|
8874 | 2965 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 2966 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
2967 |
||
2968 |
^ self copyFrom:1 to:(self size min:stop) |
|
2969 |
||
2970 |
" |
|
2971 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
2972 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
2973 |
" |
|
2974 |
! |
|
2975 |
||
602 | 2976 |
copyUpTo:element |
2977 |
"return a new collection consisting of the receiver elements |
|
8874 | 2978 |
up-to (but excluding) the first occurrence of element; |
602 | 2979 |
or to the end, if element is not included" |
2980 |
||
2981 |
|idx| |
|
2982 |
||
2983 |
idx := self indexOf:element. |
|
2984 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
2985 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
2986 |
^ self copyFrom:1 to:(idx-1) |
|
2987 |
||
2988 |
" |
|
2989 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
2990 |
'1234567890' copyUpTo:$5 |
|
2991 |
'1234567890' copyUpTo:$a |
|
2992 |
'1234567890' copyUpTo:$1 |
|
2993 |
'123456789' copyUpTo:$0 |
|
2994 |
" |
|
2995 |
! |
|
2996 |
||
1 | 2997 |
copyWith:newElement |
233 | 2998 |
"return a new collection containing the receivers elements |
8874 | 2999 |
and the single new element, newElement. |
233 | 3000 |
This is different from concatentation, which expects another collection |
3001 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3002 |
|
3003 |
|newCollection mySize newSize| |
|
3004 |
||
3005 |
mySize := self size. |
|
3006 |
newSize := mySize + 1. |
|
252 | 3007 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3008 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3009 |
newCollection at:newSize put:newElement. |
|
3010 |
^newCollection |
|
44 | 3011 |
|
95 | 3012 |
" |
3013 |
#(1 2 3 4 5) copyWith:$a |
|
3014 |
'abcdefg' copyWith:$h |
|
3015 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3016 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3017 |
" |
|
1 | 3018 |
! |
3019 |
||
61 | 3020 |
copyWithout:elementToSkip |
3021 |
"return a new collection consisting of a copy of the receiver, with |
|
3022 |
ALL elements equal to elementToSkip are left out. |
|
3023 |
No error is reported, if elementToSkip is not in the collection." |
|
3024 |
||
293 | 3025 |
|n "{ Class: SmallInteger }" |
3026 |
sz "{ Class: SmallInteger }" |
|
3027 |
srcIndex "{ Class: SmallInteger }" |
|
3028 |
dstIndex "{ Class: SmallInteger }" |
|
3029 |
skipIndex "{ Class: SmallInteger }" |
|
3030 |
copy l| |
|
61 | 3031 |
|
8874 | 3032 |
"the code below may look like overkill, |
61 | 3033 |
however, for big collections its better to move data |
3034 |
around in big chunks" |
|
3035 |
||
3036 |
n := self occurrencesOf:elementToSkip. |
|
3037 |
n == 0 ifTrue:[^ self copy]. |
|
3038 |
||
3039 |
sz := self size. |
|
252 | 3040 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3041 |
|
3042 |
srcIndex := 1. |
|
3043 |
dstIndex := 1. |
|
3044 |
||
3045 |
n timesRepeat:[ |
|
8874 | 3046 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3047 |
l := skipIndex - srcIndex. |
|
3048 |
l ~~ 0 ifTrue:[ |
|
3049 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3050 |
with:self startingAt:srcIndex. |
|
3051 |
dstIndex := dstIndex + l |
|
3052 |
]. |
|
3053 |
srcIndex := skipIndex + 1 |
|
61 | 3054 |
]. |
3055 |
l := sz - srcIndex. |
|
3056 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3057 |
with:self startingAt:srcIndex. |
61 | 3058 |
^ copy |
3059 |
||
95 | 3060 |
" |
3061 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3062 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3063 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3064 |
'abcdefghi' copyWithout:$h |
3065 |
'abcdefg' copyWithout:$h |
|
95 | 3066 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3067 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3068 |
#(90 80 70 60 50) copyWithout:70 |
|
3069 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3070 |
" |
|
61 | 3071 |
! |
3072 |
||
602 | 3073 |
copyWithoutFirst:elementToSkip |
3074 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3075 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3076 |
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
|
3077 |
Do not confuse this with copyButFirst:" |
602 | 3078 |
|
3079 |
|copy skipIndex sz| |
|
3080 |
||
3081 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3082 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3083 |
||
3084 |
sz := self size - 1. |
|
3085 |
copy := self copyEmptyAndGrow:sz. |
|
3086 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3087 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3088 |
^ copy |
|
3089 |
||
3090 |
" |
|
3091 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3092 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3093 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3094 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3095 |
" |
|
3096 |
! |
|
3097 |
||
8673 | 3098 |
copyWithoutIdentical:elementToSkip |
3099 |
"return a new collection consisting of a copy of the receiver, with |
|
3100 |
ALL elements identical to elementToSkip are left out. |
|
3101 |
No error is reported, if elementToSkip is not in the collection." |
|
3102 |
||
3103 |
|n "{ Class: SmallInteger }" |
|
3104 |
sz "{ Class: SmallInteger }" |
|
3105 |
srcIndex "{ Class: SmallInteger }" |
|
3106 |
dstIndex "{ Class: SmallInteger }" |
|
3107 |
skipIndex "{ Class: SmallInteger }" |
|
3108 |
copy l| |
|
3109 |
||
8874 | 3110 |
"the code below may look like overkill, |
8673 | 3111 |
however, for big collections its better to move data |
3112 |
around in big chunks" |
|
3113 |
||
3114 |
n := self count:[:el | el == elementToSkip]. |
|
3115 |
n == 0 ifTrue:[^ self copy]. |
|
3116 |
||
3117 |
sz := self size. |
|
3118 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3119 |
||
3120 |
srcIndex := 1. |
|
3121 |
dstIndex := 1. |
|
3122 |
||
3123 |
n timesRepeat:[ |
|
8874 | 3124 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3125 |
l := skipIndex - srcIndex. |
|
3126 |
l ~~ 0 ifTrue:[ |
|
3127 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3128 |
with:self startingAt:srcIndex. |
|
3129 |
dstIndex := dstIndex + l |
|
3130 |
]. |
|
3131 |
srcIndex := skipIndex + 1 |
|
8673 | 3132 |
]. |
3133 |
l := sz - srcIndex. |
|
3134 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3135 |
with:self startingAt:srcIndex. |
8673 | 3136 |
^ copy |
3137 |
||
3138 |
" |
|
8874 | 3139 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3140 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3141 |
" |
3142 |
! |
|
3143 |
||
61 | 3144 |
copyWithoutIndex:omitIndex |
3145 |
"return a new collection consisting of receivers elements |
|
3146 |
without the argument stored at omitIndex" |
|
3147 |
||
3148 |
|copy sz| |
|
3149 |
||
202 | 3150 |
sz := self size - 1. |
252 | 3151 |
copy := self copyEmptyAndGrow:sz. |
293 | 3152 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3153 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3154 |
^ copy |
3155 |
||
95 | 3156 |
" |
3157 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3158 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3159 |
" |
|
1 | 3160 |
! |
3161 |
||
202 | 3162 |
copyWithoutLast:count |
3163 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3164 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3165 |
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
|
3166 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3167 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3168 |
|
9250 | 3169 |
<resource:#obsolete> |
3170 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3171 |
^ self copyTo:(self size - count) |
202 | 3172 |
|
3173 |
" |
|
8874 | 3174 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3175 |
'1234567890' copyWithoutLast:4 |
|
202 | 3176 |
" |
5231 | 3177 |
! |
3178 |
||
3179 |
restAfter:anElement |
|
8366 | 3180 |
"return a new collection consisting of the receivers elements after |
5231 | 3181 |
(but excluding) anElement. |
8366 | 3182 |
If anElement is not in the receiver, the returned collection |
3183 |
will be empty. |
|
3184 |
See also #upTo:." |
|
5231 | 3185 |
|
3186 |
|pos| |
|
3187 |
||
3188 |
pos := self indexOf:anElement. |
|
8366 | 3189 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3190 |
||
5231 | 3191 |
^ self copyFrom:(pos + 1) |
3192 |
||
3193 |
" |
|
8874 | 3194 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3195 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3196 |
'hello world' upTo:Character space |
|
3197 |
'hello world' restAfter:Character space |
|
3198 |
'1234.5678' upTo:$. |
|
3199 |
'1234.5678' restAfter:$. |
|
8366 | 3200 |
" |
5231 | 3201 |
! |
3202 |
||
3203 |
upTo:anElement |
|
3204 |
"return a new collection consisting of the receivers elements upTo |
|
3205 |
(but excluding) anElement. |
|
3206 |
If anElement is not in the receiver, the returned collection |
|
3207 |
will consist of all elements of the receiver. |
|
9307 | 3208 |
See also #restAfter: , #copyFrom:index." |
5231 | 3209 |
|
3210 |
|pos| |
|
3211 |
||
3212 |
pos := self indexOf:anElement. |
|
3213 |
pos == 0 ifTrue:[^ self copy]. |
|
3214 |
||
3215 |
^ self copyFrom:1 to:(pos - 1) |
|
3216 |
||
3217 |
" |
|
8874 | 3218 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3219 |
'hello world' upTo:Character space |
|
3220 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3221 |
'1234.5678' upTo:$. |
|
3222 |
'1234' upTo:$. |
|
5231 | 3223 |
|
3224 |
raises an error: |
|
3225 |
||
8874 | 3226 |
(Dictionary new |
9307 | 3227 |
at:#foo put:'foo'; |
3228 |
at:#bar put:'bar'; |
|
3229 |
at:#baz put:'baz'; |
|
3230 |
yourself) upTo:#bar |
|
8874 | 3231 |
|
5231 | 3232 |
" |
3233 |
||
3234 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3235 |
! |
|
3236 |
||
3237 |
upTo:anElement count:count |
|
3238 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3239 |
(but excluding) count found anElements, i.e. the first found count-1 |
3240 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3241 |
procedure is done from the end of the collection backwards. |
3242 |
If anElement is not in the receiver, the returned collection |
|
3243 |
will consist of all elements of the receiver" |
|
3244 |
||
3245 |
|startPos endPos pos found| |
|
3246 |
||
3247 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3248 |
^ self upTo:anElement |
5231 | 3249 |
]. |
3250 |
||
3251 |
startPos := 1. |
|
3252 |
endPos := self size. |
|
3253 |
found := 0. |
|
3254 |
||
3255 |
count > 1 ifTrue:[ |
|
8874 | 3256 |
pos := 0. |
3257 |
[pos < self size and: [found < count]] |
|
3258 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3259 |
found == count ifTrue:[ |
|
3260 |
endPos := pos - 1 |
|
3261 |
] |
|
3262 |
]. |
|
5231 | 3263 |
|
3264 |
count < 0 ifTrue:[ |
|
8874 | 3265 |
pos := self size + 1. |
3266 |
[pos > 1 and: [found < count abs]] |
|
3267 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3268 |
found == count abs ifTrue:[ |
|
3269 |
startPos := pos + 1 |
|
3270 |
] |
|
3271 |
]. |
|
5231 | 3272 |
|
3273 |
^ self copyFrom: startPos to: endPos |
|
3274 |
||
3275 |
" |
|
8874 | 3276 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3277 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3278 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3279 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3280 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3281 |
" |
|
5563 | 3282 |
! |
3283 |
||
3284 |
upToAny:aCollectionOfObjects |
|
3285 |
"return a new collection consisting of the receivers elements upTo |
|
3286 |
(but excluding) any in aCollectionOfObjects. |
|
3287 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3288 |
will consist of all elements of the receiver. |
|
3289 |
See also #upTo:" |
|
3290 |
||
3291 |
|pos| |
|
3292 |
||
3293 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3294 |
pos == 0 ifTrue:[^ self copy]. |
|
3295 |
||
3296 |
^ self copyFrom:1 to:(pos - 1) |
|
3297 |
||
3298 |
" |
|
8874 | 3299 |
'hello world' upToAny:#($/ $:) |
3300 |
'hello:world' upToAny:#($/ $:) |
|
3301 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3302 |
" |
602 | 3303 |
! ! |
3304 |
||
3305 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3306 |
||
3307 |
collect:aBlock |
|
3308 |
"evaluate the argument, aBlock for every element in the collection |
|
3309 |
and return a collection of the results" |
|
3310 |
||
3311 |
|newCollection |
|
3312 |
sz "{ Class:SmallInteger }"| |
|
3313 |
||
3314 |
sz := self size. |
|
3315 |
newCollection := self copyEmptyAndGrow:sz. |
|
3316 |
1 to:sz do:[:index | |
|
8874 | 3317 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3318 |
]. |
3319 |
^ newCollection |
|
3320 |
||
3321 |
" |
|
8874 | 3322 |
#(one two three four five six) collect:[:element | element asUppercase] |
3323 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3324 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3325 |
" |
202 | 3326 |
! |
3327 |
||
602 | 3328 |
do:aBlock |
3329 |
"evaluate the argument, aBlock for every element in the collection." |
|
3330 |
||
3331 |
|stop "{ Class:SmallInteger }"| |
|
3332 |
||
3333 |
stop := self size. |
|
3334 |
1 to:stop do:[:index | |
|
8874 | 3335 |
aBlock value:(self at:index). |
602 | 3336 |
] |
2927 | 3337 |
|
602 | 3338 |
" |
1422 | 3339 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 3340 |
" |
3341 |
! |
|
3342 |
||
2927 | 3343 |
do:aBlock separatedBy:sepBlock |
3344 |
"evaluate the argument, aBlock for every element in the collection. |
|
3345 |
Between each element (i.e. not before the first element and not after the |
|
3346 |
last element), evaluate sepBlock. |
|
3347 |
This supports printing of collections with elements separated by some |
|
3348 |
string." |
|
3349 |
||
3350 |
|stop "{ Class:SmallInteger }"| |
|
3351 |
||
3352 |
stop := self size. |
|
3353 |
stop < 1 ifTrue:[^ self]. |
|
3354 |
aBlock value:(self at:1). |
|
3355 |
2 to:stop do:[:index | |
|
8874 | 3356 |
sepBlock value. |
3357 |
aBlock value:(self at:index) |
|
2927 | 3358 |
]. |
3359 |
||
3360 |
" |
|
3361 |
#(one two three four five six) |
|
8874 | 3362 |
do:[:each | Transcript show:each] |
3363 |
separatedBy:[Transcript show:' , '] |
|
2927 | 3364 |
" |
3365 |
! |
|
3003 | 3366 |
|
3044 | 3367 |
doWithIndex:aBlock |
3368 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
3369 |
the index as second argument." |
|
3370 |
||
3371 |
self keysAndValuesDo:[:index :value | |
|
8874 | 3372 |
aBlock value:value value:index |
3044 | 3373 |
]. |
3374 |
||
3375 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
3376 |
! |
|
3377 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3378 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3379 |
"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
|
3380 |
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
|
3381 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3382 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3383 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3384 |
" |
8874 | 3385 |
#(one two three four five six) |
3386 |
from:2 |
|
3387 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3388 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3389 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3390 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3391 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3392 |
|
602 | 3393 |
from:startIndex do:aBlock |
3394 |
"evaluate the argument, aBlock for the elements starting with the |
|
3395 |
element at startIndex to the end." |
|
3396 |
||
3397 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 3398 |
|
95 | 3399 |
" |
8874 | 3400 |
#(one two three four five six) |
3401 |
from:3 |
|
3402 |
do:[:element | Transcript showCR:element] |
|
602 | 3403 |
" |
3404 |
! |
|
3405 |
||
7490 | 3406 |
from:startIndex keysAndValuesDo:aBlock |
3407 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
3408 |
element at startIndex to the end." |
|
3409 |
||
3410 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
3411 |
||
3412 |
" |
|
8874 | 3413 |
#(one two three four five six) |
3414 |
from:3 |
|
3415 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 3416 |
" |
3417 |
! |
|
3418 |
||
7337 | 3419 |
from:index1 to:index2 by:stepArg do:aBlock |
3420 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
3421 |
||
3422 |
|start "{ Class:SmallInteger }" |
|
3423 |
stop "{ Class:SmallInteger }" |
|
3424 |
step "{ Class:SmallInteger }" | |
|
3425 |
||
3426 |
start := index1. "/ these assignments force type checking... |
|
3427 |
stop := index2. "/ and guarantee inline loop code below. |
|
3428 |
step := stepArg. |
|
3429 |
||
3430 |
start to:stop by:step do:[:index | |
|
8874 | 3431 |
aBlock value:(self at:index). |
7337 | 3432 |
] |
3433 |
||
3434 |
" |
|
8874 | 3435 |
#(one two three four five six seven eight nine ten) |
3436 |
from:2 |
|
3437 |
to:10 |
|
3438 |
by:2 |
|
3439 |
do:[:element | Transcript showCR:element] |
|
7337 | 3440 |
" |
3441 |
! |
|
3442 |
||
602 | 3443 |
from:start to:stop collect:aBlock |
3444 |
"evaluate the argument, aBlock for the elements indexed by start |
|
3445 |
to stop in the collection and return a collection of the results" |
|
3446 |
||
3447 |
|newCollection sz |
|
3448 |
idx "{ Class:SmallInteger }"| |
|
3449 |
||
3450 |
sz := stop - start + 1. |
|
3451 |
newCollection := self copyEmptyAndGrow:sz. |
|
3452 |
idx := 1. |
|
3453 |
start to:stop do:[:index | |
|
8874 | 3454 |
newCollection at:idx put:(aBlock value:(self at:index)). |
3455 |
idx := idx + 1 |
|
602 | 3456 |
]. |
3457 |
^ newCollection |
|
3458 |
||
3459 |
" |
|
8874 | 3460 |
#(one two three four five six) |
3461 |
from:2 |
|
3462 |
to:4 |
|
3463 |
collect:[:element | element asUppercase] |
|
602 | 3464 |
" |
3465 |
! |
|
3466 |
||
3467 |
from:index1 to:index2 do:aBlock |
|
3468 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3469 |
index2 in the collection" |
|
3470 |
||
3471 |
|start "{ Class:SmallInteger }" |
|
3472 |
stop "{ Class:SmallInteger }" | |
|
3473 |
||
7337 | 3474 |
start := index1. "/ these assignments force type checking... |
3475 |
stop := index2. "/ and guarantee inline loop code below. |
|
3476 |
||
602 | 3477 |
start to:stop do:[:index | |
8874 | 3478 |
aBlock value:(self at:index). |
602 | 3479 |
] |
3480 |
||
3481 |
" |
|
8874 | 3482 |
#(one two three four five six) |
3483 |
from:3 |
|
3484 |
to:5 |
|
3485 |
do:[:element | Transcript showCR:element] |
|
602 | 3486 |
" |
3487 |
! |
|
3488 |
||
6868 | 3489 |
from:index1 to:index2 keysAndValuesDo:aBlock |
3490 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3491 |
index2 in the collection; pass both index and value." |
|
3492 |
||
3493 |
|start "{ Class:SmallInteger }" |
|
3494 |
stop "{ Class:SmallInteger }" | |
|
3495 |
||
7337 | 3496 |
start := index1. "/ these assignments force type checking... |
3497 |
stop := index2. "/ and guarantee inline loop code below. |
|
3498 |
||
6868 | 3499 |
start to:stop do:[:index | |
8874 | 3500 |
aBlock value:index value:(self at:index). |
6868 | 3501 |
] |
3502 |
||
3503 |
" |
|
8874 | 3504 |
#(one two three four five six) |
3505 |
from:3 |
|
3506 |
to:5 |
|
3507 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 3508 |
" |
3509 |
! |
|
3510 |
||
602 | 3511 |
from:index1 to:index2 reverseDo:aBlock |
3512 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3513 |
index2 in the collection. Step in reverse order" |
|
3514 |
||
3515 |
|start "{ Class:SmallInteger }" |
|
3516 |
stop "{ Class:SmallInteger }" | |
|
3517 |
||
7337 | 3518 |
start := index1. "/ these assignments force type checking... |
3519 |
stop := index2. "/ and guarantee inline loop code below. |
|
3520 |
||
602 | 3521 |
stop to:start by:-1 do:[:index | |
8874 | 3522 |
aBlock value:(self at:index). |
602 | 3523 |
] |
3524 |
||
3525 |
" |
|
8874 | 3526 |
#(one two three four five six) |
3527 |
from:3 |
|
3528 |
to:5 |
|
3529 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 3530 |
" |
61 | 3531 |
! |
3532 |
||
6027 | 3533 |
from:startIndex to:stopIndex select:aBlock |
3534 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
3535 |
and return a collection of those elements for which the block return true." |
|
3536 |
||
3537 |
|element newColl species needCopy |
|
3538 |
n "{ Class:SmallInteger }"| |
|
3539 |
||
3540 |
n := stopIndex - startIndex + 1. |
|
3541 |
species := self species. |
|
3542 |
species growIsCheap ifFalse:[ |
|
8874 | 3543 |
newColl := OrderedCollection new:n. |
3544 |
needCopy := true |
|
6027 | 3545 |
] ifTrue:[ |
8874 | 3546 |
newColl := self copyEmpty:n. |
3547 |
needCopy := false |
|
6027 | 3548 |
]. |
3549 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3550 |
element := self at:index. |
3551 |
(aBlock value:element) ifTrue:[ |
|
3552 |
newColl add:element |
|
3553 |
]. |
|
6027 | 3554 |
]. |
3555 |
needCopy ifTrue:[ |
|
8874 | 3556 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 3557 |
]. |
3558 |
^ newColl |
|
3559 |
||
3560 |
" |
|
8874 | 3561 |
#(faba one two three four five six) |
3562 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 3563 |
" |
3564 |
! |
|
3565 |
||
10150 | 3566 |
inGroupsOf:n collect:anNArgBlock |
3567 |
"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
|
3568 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3569 |
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
|
3570 |
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
|
3571 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3572 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3573 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3574 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3575 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3576 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3577 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3578 |
|
10150 | 3579 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
3580 |
"/ 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
|
3581 |
n == 2 ifTrue:[ |
10150 | 3582 |
1 to:stop by:n do:[:index | |
3583 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
3584 |
newCollection at:dstIdx put:rslt. |
|
3585 |
dstIdx := dstIdx + 1. |
|
3586 |
]. |
|
3587 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3588 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3589 |
n == 3 ifTrue:[ |
10150 | 3590 |
1 to:stop by:n do:[:index | |
3591 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3592 |
newCollection at:dstIdx put:rslt. |
|
3593 |
dstIdx := dstIdx + 1. |
|
3594 |
]. |
|
3595 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3596 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3597 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3598 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3599 |
1 to:stop by:n do:[:index | |
10150 | 3600 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3601 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
3602 |
newCollection at:dstIdx put:rslt. |
|
3603 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3604 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3605 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3606 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3607 |
"for groups of 2, this is the same as: |
8874 | 3608 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3609 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 3610 |
|
3611 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 3612 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3613 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3614 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 3615 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
3616 |
" |
|
3617 |
||
3618 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
3619 |
! |
|
3620 |
||
3621 |
inGroupsOf:n do:anNArgBlock |
|
3622 |
"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
|
3623 |
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
|
3624 |
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
|
3625 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3626 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3627 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3628 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3629 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3630 |
|
10150 | 3631 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
3632 |
"/ 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
|
3633 |
n == 2 ifTrue:[ |
10150 | 3634 |
1 to:stop by:n do:[:index | |
3635 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
3636 |
]. |
|
3637 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3638 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3639 |
n == 3 ifTrue:[ |
10150 | 3640 |
1 to:stop by:n do:[:index | |
3641 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3642 |
]. |
|
3643 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3644 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3645 |
n == 4 ifTrue:[ |
10150 | 3646 |
1 to:stop by:n do:[:index | |
3647 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
3648 |
]. |
|
3649 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3650 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3651 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3652 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3653 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3654 |
1 to:stop by:n do:[:index | |
10150 | 3655 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3656 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3657 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3658 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3659 |
"for groups of 2, this is the same as: |
8874 | 3660 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3661 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 3662 |
|
3663 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 3664 |
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
|
3665 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3666 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 3667 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
3668 |
" |
|
3669 |
||
3670 |
"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
|
3671 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3672 |
|
8853 | 3673 |
keysAndValuesCollect:aTwoArgBlock |
3674 |
"evaluate the argument, aBlock for every element in the collection, |
|
3675 |
passing both index and element as arguments. |
|
3676 |
Collect the returned values and return them." |
|
3677 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3678 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3679 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3680 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3681 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3682 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3683 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 3684 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3685 |
|
8853 | 3686 |
^ newCollection |
3687 |
||
3688 |
" |
|
8874 | 3689 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3690 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3691 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 3692 |
|
3693 |
||
3694 |
|d| |
|
3695 |
||
3696 |
d := Dictionary new. |
|
3697 |
d at:'a' put:'A'. |
|
3698 |
d at:'b' put:'B'. |
|
3699 |
d at:'c' put:'C'. |
|
3700 |
d at:'d' put:'D'. |
|
3701 |
d at:'e' put:'E'. |
|
8874 | 3702 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3703 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3704 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3705 |
ifFalse:[element asLowercase]] |
8853 | 3706 |
" |
3707 |
! |
|
3708 |
||
602 | 3709 |
keysAndValuesDo:aTwoArgBlock |
3710 |
"evaluate the argument, aBlock for every element in the collection, |
|
3711 |
passing both index and element as arguments." |
|
3712 |
||
3713 |
|stop "{ Class:SmallInteger }"| |
|
3714 |
||
3715 |
stop := self size. |
|
3716 |
1 to:stop do:[:index | |
|
8874 | 3717 |
aTwoArgBlock value:index value:(self at:index). |
602 | 3718 |
] |
3719 |
" |
|
8874 | 3720 |
#(one two three four five six) |
3721 |
keysAndValuesDo:[:key :element | |
|
3722 |
Transcript show:key; space; showCR:element |
|
3723 |
] |
|
602 | 3724 |
" |
3725 |
! |
|
3726 |
||
1359 | 3727 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 3728 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 3729 |
in the collection, passing both index and element as arguments." |
3730 |
||
3731 |
|stop "{ Class:SmallInteger }"| |
|
3732 |
||
3733 |
stop := self size. |
|
3734 |
stop to:1 by:-1 do:[:index | |
|
8874 | 3735 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 3736 |
] |
3737 |
||
3738 |
" |
|
8874 | 3739 |
#(one two three four five six) |
3740 |
keysAndValuesReverseDo:[:key :element | |
|
3741 |
Transcript show:key; space; showCR:element |
|
3742 |
] |
|
1359 | 3743 |
" |
3744 |
||
3745 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
3746 |
! |
|
3747 |
||
602 | 3748 |
nonNilElementsDo:aBlock |
3749 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
3750 |
||
3751 |
|stop "{ Class:SmallInteger }" |
|
3752 |
element| |
|
3753 |
||
3754 |
stop := self size. |
|
3755 |
1 to:stop do:[:index | |
|
8874 | 3756 |
(element := self at:index) notNil ifTrue:[ |
3757 |
aBlock value:element. |
|
3758 |
] |
|
602 | 3759 |
] |
3760 |
" |
|
1422 | 3761 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 3762 |
" |
3763 |
! |
|
3764 |
||
10150 | 3765 |
pairWiseCollect:aTwoArgBlock |
3766 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 3767 |
The block is called with 2 arguments for each 2 elements in the receiver. |
3768 |
An error will be reported, if the number of elements in the receiver |
|
3769 |
is not a multiple of 2. |
|
3770 |
Collect the results and return a new collection containing those." |
|
3771 |
||
10150 | 3772 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 3773 |
|
95 | 3774 |
" |
8874 | 3775 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3776 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 3777 |
|
3778 |
||
3779 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 3780 |
pairWiseCollect:[:x :y | x@y] |
3781 |
" |
|
3782 |
||
3783 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
3784 |
! |
|
3785 |
||
3786 |
pairWiseDo:aTwoArgBlock |
|
10385 | 3787 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
3788 |
An error will be reported, if the number of elements in the receiver |
|
602 | 3789 |
is not a multiple of 2." |
3790 |
||
10150 | 3791 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3792 |
|
602 | 3793 |
" |
8874 | 3794 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3795 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 3796 |
|
3797 |
||
3798 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 3799 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
3800 |
" |
|
3801 |
||
10385 | 3802 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 3803 |
! |
3804 |
||
602 | 3805 |
reverseDo:aBlock |
3806 |
"evaluate the argument, aBlock for every element in the collection |
|
3807 |
in reverse order" |
|
3808 |
||
3809 |
|sz "{ Class:SmallInteger }"| |
|
3810 |
||
3811 |
sz := self size. |
|
3812 |
sz to:1 by:-1 do:[:index | |
|
8874 | 3813 |
aBlock value:(self at:index). |
602 | 3814 |
] |
3815 |
||
3816 |
" |
|
1422 | 3817 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 3818 |
" |
3819 |
! |
|
3820 |
||
3821 |
select:aBlock |
|
3822 |
"evaluate the argument, aBlock for every element in the collection |
|
3823 |
and return a collection of all elements for which the block return |
|
3824 |
true" |
|
3825 |
||
3826 |
|element newColl species needCopy |
|
3827 |
sz "{ Class:SmallInteger }"| |
|
3828 |
||
3829 |
sz := self size. |
|
3830 |
species := self species. |
|
3831 |
species growIsCheap ifFalse:[ |
|
8874 | 3832 |
newColl := OrderedCollection new:sz. |
3833 |
needCopy := true |
|
602 | 3834 |
] ifTrue:[ |
8874 | 3835 |
newColl := self copyEmpty:sz. |
3836 |
needCopy := false |
|
602 | 3837 |
]. |
3838 |
1 to:sz do:[:index | |
|
8874 | 3839 |
element := self at:index. |
3840 |
(aBlock value:element) ifTrue:[ |
|
3841 |
newColl add:element |
|
3842 |
]. |
|
602 | 3843 |
]. |
3844 |
needCopy ifTrue:[ |
|
8874 | 3845 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 3846 |
]. |
61 | 3847 |
^ newColl |
3848 |
||
95 | 3849 |
" |
8874 | 3850 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
3851 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
3852 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
3853 |
(1 to:9) select:[:element | element odd] |
|
3854 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 3855 |
" |
186 | 3856 |
! |
3857 |
||
602 | 3858 |
with:aSequenceableCollection do:aTwoArgBlock |
3859 |
"evaluate the argument, aBlock for successive elements from |
|
3860 |
each the receiver and the argument, aSequenceableCollection. |
|
3861 |
The second argument, aBlock must be a two-argument block. |
|
3862 |
The collection argument must implement access via a numeric key." |
|
3863 |
||
3864 |
|stop "{ Class: SmallInteger }" | |
|
3865 |
||
3866 |
stop := self size. |
|
3867 |
1 to:stop do:[:index | |
|
8874 | 3868 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 3869 |
] |
186 | 3870 |
" |
8874 | 3871 |
#(one two three four five six) |
3872 |
with:(1 to:10) |
|
3873 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 3874 |
" |
9454 | 3875 |
! |
3876 |
||
3877 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
3878 |
"evaluate the argument, aBlock for successive elements from |
|
3879 |
each the receiver and the argument, aSequenceableCollection. |
|
3880 |
aBlock must be a three-argument block, which get elements from either collection and |
|
3881 |
the index as arguments. |
|
3882 |
The collection argument must implement access via a numeric key." |
|
3883 |
||
3884 |
|stop "{ Class: SmallInteger }" | |
|
3885 |
||
3886 |
stop := self size. |
|
3887 |
1 to:stop do:[:index | |
|
3888 |
aThreeArgBlock |
|
3889 |
value:(self at:index) |
|
3890 |
value:(aSequenceableCollection at:index) |
|
3891 |
value:index. |
|
3892 |
] |
|
3893 |
" |
|
3894 |
#(one two three four five six) |
|
3895 |
with:(1 to:10) |
|
3896 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
3897 |
" |
|
3898 |
||
3899 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 3900 |
! ! |
3901 |
||
4154 | 3902 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 3903 |
|
602 | 3904 |
atAllPut:anObject |
3905 |
"replace all elements of the collection by the argument, anObject. |
|
3906 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3907 |
therefore the change may affect all others referencing the receiver." |
|
3908 |
||
3909 |
self from:1 to:(self size) put:anObject |
|
3910 |
||
3911 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3912 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3913 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3914 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3915 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3916 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3917 |
" |
602 | 3918 |
(Array new:10) atAllPut:1 |
3919 |
(String new:10) atAllPut:$a |
|
3920 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3921 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3922 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 3923 |
! |
3924 |
||
1 | 3925 |
from:index1 to:index2 put:anObject |
3926 |
"replace the elements from index1 to index2 of the collection |
|
44 | 3927 |
by the argument, anObject. |
3928 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3929 |
therefore the change may affect all others referencing the receiver." |
|
1 | 3930 |
|
3931 |
|index "{ Class: SmallInteger }" |
|
3932 |
end "{ Class: SmallInteger }"| |
|
3933 |
||
3934 |
index := index1. |
|
3935 |
end := index2. |
|
3936 |
[index <= end] whileTrue:[ |
|
8874 | 3937 |
self at:index put:anObject. |
3938 |
index := index + 1 |
|
1 | 3939 |
] |
44 | 3940 |
|
95 | 3941 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3942 |
args: index1 : <integer> |
8874 | 3943 |
index2 : <integer> |
3944 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3945 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3946 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3947 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3948 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3949 |
" |
95 | 3950 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
3951 |
'abcdefghijkl' from:3 to:6 put:$X |
|
3952 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3953 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3954 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 3955 |
! |
3956 |
||
3003 | 3957 |
replaceAll:oldObject by:newObject |
3958 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
3959 |
Pleace do no longer use this one; |
|
3960 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
3961 |
||
5872 | 3962 |
<resource:#obsolete> |
3963 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3964 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 3965 |
^ self replaceAll:oldObject with:newObject |
3966 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3967 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3968 |
args: oldObject : <object> |
8874 | 3969 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3970 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3971 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3972 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3973 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3974 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 3975 |
! |
3976 |
||
3977 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
3978 |
"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
|
3979 |
Please do no longer use this one; |
3003 | 3980 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
3981 |
||
5872 | 3982 |
<resource:#obsolete> |
3983 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3984 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 3985 |
^ 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
|
3986 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3987 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3988 |
args: oldObject : <object> |
8874 | 3989 |
newObject : <object> |
3990 |
startIndex : <integer> |
|
3991 |
stopIndex : <integer> |
|
3992 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3993 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3994 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3995 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3996 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 3997 |
! |
3998 |
||
2927 | 3999 |
replaceAll:oldObject with:newObject |
44 | 4000 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4001 |
|
44 | 4002 |
Notice: This operation modifies the receiver, NOT a copy; |
4003 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4004 |
|
8874 | 4005 |
^ self |
4006 |
replaceAll:oldObject |
|
4007 |
with:newObject |
|
4008 |
from:1 |
|
4009 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4010 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4011 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4012 |
args: oldObject : <object> |
8874 | 4013 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4014 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4015 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4016 |
" |
362 | 4017 |
|
4018 |
" |
|
8874 | 4019 |
'123123abc123' replaceAll:$1 with:$* |
4020 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4021 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4022 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4023 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4024 |
! |
293 | 4025 |
|
2927 | 4026 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4027 |
"replace all oldObjects found between startIndex and endIndex, |
4028 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4029 |
|
362 | 4030 |
Notice: This operation modifies the receiver, NOT a copy; |
4031 |
therefore the change may affect all others referencing the receiver." |
|
4032 |
||
4033 |
|start "{ Class: SmallInteger }" |
|
4034 |
stop "{ Class: SmallInteger }"| |
|
4035 |
||
4036 |
start := startIndex. |
|
4037 |
stop := stopIndex. |
|
4038 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4039 |
(self at:index) = oldObject ifTrue:[ |
4040 |
self at:index put:newObject |
|
4041 |
] |
|
1 | 4042 |
] |
44 | 4043 |
|
95 | 4044 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4045 |
args: oldObject : <object> |
8874 | 4046 |
newObject : <object> |
4047 |
startIndex : <integer> |
|
4048 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4049 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4050 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4051 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4052 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4053 |
" |
2980 | 4054 |
'123123abc123' replaceAll:$1 with:$* |
4055 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4056 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4057 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4058 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4059 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4060 |
! |
4061 |
||
7615 | 4062 |
replaceAllForWhich:aConditionBlock with:newObject |
4063 |
"replace all elements for which aConditionBlock returns true |
|
4064 |
between startIndex and endIndex, by newObject in the receiver. |
|
4065 |
||
4066 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4067 |
therefore the change may affect all others referencing the receiver." |
|
4068 |
||
8874 | 4069 |
self |
4070 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4071 |
from:1 to:(self size) |
|
7615 | 4072 |
|
4073 |
" |
|
4074 |
args: aConditionBlock : <block> |
|
8874 | 4075 |
newObject : <object> |
7615 | 4076 |
|
4077 |
returns: self |
|
4078 |
" |
|
4079 |
! |
|
4080 |
||
4081 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4082 |
"replace all elements for which aConditionBlock returns true |
|
4083 |
between startIndex and endIndex, by newObject in the receiver. |
|
4084 |
||
4085 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4086 |
therefore the change may affect all others referencing the receiver." |
|
4087 |
||
4088 |
|start "{ Class: SmallInteger }" |
|
4089 |
stop "{ Class: SmallInteger }"| |
|
4090 |
||
4091 |
start := startIndex. |
|
4092 |
stop := stopIndex. |
|
4093 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4094 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4095 |
self at:index put:newObject |
|
4096 |
] |
|
7615 | 4097 |
] |
4098 |
||
4099 |
" |
|
4100 |
args: aConditionBlock : <block> |
|
8874 | 4101 |
newObject : <object> |
4102 |
startIndex : <integer> |
|
4103 |
stopIndex : <integer> |
|
7615 | 4104 |
|
4105 |
returns: self |
|
4106 |
" |
|
4107 |
! |
|
4108 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4109 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4110 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4111 |
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
|
4112 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4113 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4114 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4115 |
|
8874 | 4116 |
^ self |
4117 |
replaceAllIdentical:oldObject |
|
4118 |
with:newObject |
|
4119 |
from:1 |
|
4120 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4121 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4122 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4123 |
args: oldObject : <object> |
8874 | 4124 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4125 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4126 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4127 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4128 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4129 |
" |
8874 | 4130 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4131 |
#(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
|
4132 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4133 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4134 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4135 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4136 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4137 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4138 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4139 |
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
|
4140 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4141 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4142 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4143 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4144 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4145 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4146 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4147 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4148 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4149 |
startIndex to:stopIndex do:[:index | |
8874 | 4150 |
(self at:index) == oldObject ifTrue:[ |
4151 |
self at:index put:newObject |
|
4152 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4153 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4154 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4155 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4156 |
args: oldObject : <object> |
8874 | 4157 |
newObject : <object> |
4158 |
startIndex : <integer> |
|
4159 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4160 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4161 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4162 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4163 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4164 |
" |
8874 | 4165 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4166 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4167 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4168 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4169 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4170 |
|
3003 | 4171 |
replaceAny:aCollection by:newObject |
4172 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4173 |
Pleace do no longer use this one; |
3003 | 4174 |
instead use #replaceAny:with: for naming consistence." |
4175 |
||
5872 | 4176 |
<resource:#obsolete> |
4177 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4178 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4179 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4180 |
|
4181 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4182 |
args: aCollection : <colleciton of <object> > |
8874 | 4183 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4184 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4185 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4186 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4187 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4188 |
" |
3003 | 4189 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4190 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4191 |
" |
|
4192 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4193 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4194 |
! |
4195 |
||
3003 | 4196 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4197 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4198 |
Pleace do no longer use this one; |
3003 | 4199 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4200 |
|
5872 | 4201 |
<resource:#obsolete> |
4202 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4203 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4204 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4205 |
|
4206 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4207 |
args: aCollection : <colleciton of <object> > |
8874 | 4208 |
newObject : <object> |
4209 |
startIndex : <integer> |
|
4210 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4211 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4212 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4213 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4214 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4215 |
" |
8874 | 4216 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4217 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4218 |
" |
4219 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4220 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4221 |
! |
4222 |
||
4223 |
replaceAny:aCollection with:newObject |
|
4224 |
"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
|
4225 |
|
2927 | 4226 |
Notice: This operation modifies the receiver, NOT a copy; |
4227 |
therefore the change may affect all others referencing the receiver." |
|
4228 |
||
8874 | 4229 |
^ self |
10439 | 4230 |
replaceAny:aCollection |
4231 |
with:newObject |
|
4232 |
from:1 |
|
4233 |
to:self size |
|
4234 |
||
4235 |
" |
|
4236 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
4237 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
4238 |
" |
|
4239 |
||
4240 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 4241 |
! |
4242 |
||
2927 | 4243 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4244 |
"replace all elements within a range, |
4245 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4246 |
|
362 | 4247 |
Notice: This operation modifies the receiver, NOT a copy; |
4248 |
therefore the change may affect all others referencing the receiver." |
|
4249 |
||
4250 |
|start "{ Class: SmallInteger }" |
|
4251 |
stop "{ Class: SmallInteger }"| |
|
4252 |
||
4253 |
start := startIndex. |
|
4254 |
stop := stopIndex. |
|
4255 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4256 |
(aCollection includes:(self at:index)) ifTrue:[ |
4257 |
self at:index put:newObject |
|
4258 |
] |
|
95 | 4259 |
] |
4260 |
||
4261 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4262 |
args: aCollection : <colleciton of <object> > |
8874 | 4263 |
newObject : <object> |
4264 |
startIndex : <integer> |
|
4265 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4266 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4267 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4268 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4269 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4270 |
" |
8874 | 4271 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4272 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4273 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4274 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4275 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4276 |
! |
4277 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4278 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4279 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4280 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4281 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4282 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4283 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4284 |
|
8874 | 4285 |
^ self |
4286 |
replaceFrom:startIndex |
|
4287 |
to:(startIndex + numberOfElements - 1) |
|
4288 |
with:replacementCollection |
|
4289 |
startingAt:1 |
|
4290 |
||
4291 |
" |
|
4292 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4293 |
#($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
|
4294 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4295 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4296 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4297 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4298 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4299 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4300 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4301 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4302 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4303 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4304 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4305 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4306 |
^ self |
8874 | 4307 |
replaceFrom:startIndex |
4308 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4309 |
with:replacementCollection |
|
4310 |
startingAt:repStartIndex |
|
4311 |
||
4312 |
" |
|
4313 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4314 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4315 |
#($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
|
4316 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4317 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4318 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4319 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4320 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4321 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4322 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 4323 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4324 |
|
44 | 4325 |
Notice: This operation modifies the receiver, NOT a copy; |
4326 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4327 |
|
8874 | 4328 |
^ self |
4329 |
replaceFrom:startIndex |
|
4330 |
to:stopIndex |
|
4331 |
with:replacementCollection |
|
4332 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4333 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4334 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4335 |
args: startIndex : <integer> |
8874 | 4336 |
stopIndex : <integer> |
4337 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4338 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4339 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4340 |
" |
61 | 4341 |
|
95 | 4342 |
" |
4343 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
4344 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
4345 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4346 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4347 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 4348 |
! |
4349 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4350 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4351 |
"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
|
4352 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 4353 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4354 |
|
44 | 4355 |
Notice: This operation modifies the receiver, NOT a copy; |
4356 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4357 |
|
4358 |
|srcIndex "{ Class: SmallInteger }" |
|
4359 |
dstIndex "{ Class: SmallInteger }" |
|
4360 |
end "{ Class: SmallInteger }" | |
|
4361 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4362 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 4363 |
" must do reverse copy " |
4364 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
4365 |
dstIndex := stopIndex. |
|
4366 |
end := startIndex. |
|
4367 |
[dstIndex >= end] whileTrue:[ |
|
4368 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
4369 |
srcIndex := srcIndex - 1. |
|
4370 |
dstIndex := dstIndex - 1 |
|
4371 |
]. |
|
4372 |
^ self |
|
1 | 4373 |
]. |
4374 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4375 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4376 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4377 |
end := stopIndex. |
1 | 4378 |
[dstIndex <= end] whileTrue:[ |
8874 | 4379 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
4380 |
srcIndex := srcIndex + 1. |
|
4381 |
dstIndex := dstIndex + 1 |
|
1 | 4382 |
] |
61 | 4383 |
|
95 | 4384 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4385 |
args: startIndex : <integer> |
8874 | 4386 |
stopIndex : <integer> |
4387 |
replacementCollection : <colleciton of <object> > |
|
4388 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4389 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4390 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4391 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4392 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4393 |
" |
8874 | 4394 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
4395 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
4396 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 4397 |
|
4398 |
|c| |
|
4399 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4400 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 4401 |
|
4402 |
|c| |
|
4403 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4404 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 4405 |
" |
3234 | 4406 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4407 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 4408 |
! |
4409 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4410 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4411 |
"replace elements in the receiver starting at startIndex, |
602 | 4412 |
with elements taken from replacementCollection starting at 1 |
4413 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4414 |
|
602 | 4415 |
Notice: This operation modifies the receiver, NOT a copy; |
4416 |
therefore the change may affect all others referencing the receiver." |
|
4417 |
||
8874 | 4418 |
^ self |
4419 |
replaceFrom:startIndex |
|
4420 |
to:(startIndex + replacementCollection size - 1) |
|
4421 |
with:replacementCollection |
|
4422 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4423 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4424 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4425 |
args: startIndex : <integer> |
8874 | 4426 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4427 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4428 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4429 |
" |
602 | 4430 |
|
4431 |
" |
|
4432 |
'1234567890' replaceFrom:5 with:'abc' |
|
4433 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
4434 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4435 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4436 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 4437 |
! |
4438 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4439 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4440 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4441 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 4442 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4443 |
|
602 | 4444 |
Notice: This operation modifies the receiver, NOT a copy; |
4445 |
therefore the change may affect all others referencing the receiver." |
|
4446 |
||
8874 | 4447 |
^ self |
4448 |
replaceFrom:startIndex |
|
4449 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
4450 |
with:replacementCollection |
|
4451 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4452 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4453 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4454 |
args: startIndex : <integer> |
8874 | 4455 |
replacementCollection : <colleciton of <object> > |
4456 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4457 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4458 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4459 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4460 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4461 |
" |
8874 | 4462 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 4463 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
4464 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4465 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4466 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 4467 |
! |
4468 |
||
61 | 4469 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
4470 |
"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
|
4471 |
|
61 | 4472 |
Notice: This operation modifies the destination collection, NOT a copy; |
4473 |
therefore the change may affect all others referencing this object." |
|
4474 |
||
8874 | 4475 |
destColl |
4476 |
replaceFrom:destStartIndex |
|
4477 |
to:destEndIndex |
|
4478 |
with:self |
|
4479 |
startingAt:sourceStart |
|
61 | 4480 |
|
4481 |
" |
|
4482 |
|s| |
|
4483 |
s := 'abcdefghijklmnop'. |
|
4484 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
4485 |
s'123defghijklmnop' |
|
4486 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4487 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4488 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 4489 |
! ! |
4490 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4491 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4492 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4493 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4494 |
"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
|
4495 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4496 |
Elements on the left are filled with padElement. |
8874 | 4497 |
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
|
4498 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4499 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4500 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4501 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4502 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4503 |
(len < size) ifTrue:[ |
8874 | 4504 |
s := self species new:size withAll:padElement. |
4505 |
s replaceFrom:(size - len + 1) with:self. |
|
4506 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4507 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4508 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4509 |
" |
8874 | 4510 |
'foo' leftPaddedTo:10 with:$. |
4511 |
'fooBar' leftPaddedTo:5 with:$. |
|
4512 |
123 printString leftPaddedTo:10 with:$. |
|
4513 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
4514 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
4515 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
4516 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
4517 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
4518 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
4519 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4520 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4521 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4522 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4523 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4524 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4525 |
plus pad elements up to length. |
8874 | 4526 |
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
|
4527 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4528 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4529 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4530 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4531 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4532 |
len < newSize ifTrue:[ |
8874 | 4533 |
s := self species new:newSize withAll:padElement. |
4534 |
s replaceFrom:1 to:len with:self. |
|
4535 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4536 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4537 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4538 |
" |
8874 | 4539 |
'foo' paddedTo:10 with:$. |
4540 |
123 printString paddedTo:10 with:$* |
|
4541 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
4542 |
(Float pi printString) paddedTo:15 with:$- |
|
4543 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
4544 |
#[1 2 3 4] paddedTo:6 with:0 |
|
4545 |
#[1 2 3 4] paddedTo:10 with:99 |
|
4546 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4547 |
" |
7116 | 4548 |
! |
4549 |
||
4550 |
paddedToMultipleOf:sizeModulu with:padElement |
|
4551 |
|mySize mod numPad| |
|
4552 |
||
4553 |
mySize := self size. |
|
4554 |
mod := (mySize \\ sizeModulu). |
|
4555 |
mod == 0 ifTrue:[^ self]. |
|
4556 |
||
4557 |
numPad := sizeModulu - mod. |
|
4558 |
^ self , (self species new:numPad withAll:padElement). |
|
4559 |
||
4560 |
" |
|
8874 | 4561 |
#[] paddedToMultipleOf:3 with:0 |
4562 |
#[1] paddedToMultipleOf:3 with:0 |
|
4563 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
4564 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 4565 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
4566 |
||
4567 |
#() paddedToMultipleOf:3 with:nil |
|
4568 |
#(1) paddedToMultipleOf:3 with:nil |
|
4569 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
4570 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
4571 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
4572 |
||
4573 |
'' paddedToMultipleOf:3 with:Character space |
|
4574 |
'a' paddedToMultipleOf:3 with:Character space |
|
4575 |
'ab' paddedToMultipleOf:3 with:Character space |
|
4576 |
'abc' paddedToMultipleOf:3 with:Character space |
|
4577 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
4578 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4579 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4580 |
|
7260 | 4581 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 4582 |
|
5358 | 4583 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 4584 |
"Private!! |
5358 | 4585 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
4586 |
||
4587 |
| i1 i2 val1 val2 out | |
|
4588 |
||
4589 |
i1 := first. |
|
4590 |
i2 := middle + 1. |
|
4591 |
val1 := self at: i1. |
|
4592 |
val2 := self at: i2. |
|
4593 |
out := first - 1. "will be pre-incremented" |
|
4594 |
||
4595 |
"select 'lower' half of the elements based on comparator" |
|
4596 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 4597 |
(aBlock value: val1 value: val2) |
4598 |
ifTrue: [ |
|
4599 |
dst at: (out := out + 1) put: val1. |
|
4600 |
val1 := self at: (i1 := i1 + 1)] |
|
4601 |
ifFalse: [ |
|
4602 |
dst at: (out := out + 1) put: val2. |
|
4603 |
i2 := i2 + 1. |
|
4604 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 4605 |
|
4606 |
"copy the remaining elements" |
|
4607 |
i1 <= middle |
|
8874 | 4608 |
ifTrue: [ |
4609 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
4610 |
ifFalse: [ |
|
4611 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 4612 |
|
4613 |
! |
|
4614 |
||
4615 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
4616 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
4617 |
||
4618 |
| middle | |
|
4619 |
||
4620 |
first = last ifTrue: [^ self]. |
|
4621 |
middle := (first + last) // 2. |
|
4622 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
4623 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
4624 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
4625 |
||
4626 |
||
4627 |
! |
|
4628 |
||
2069 | 4629 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4630 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4631 |
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
|
4632 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4633 |
|
8874 | 4634 |
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
|
4635 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4636 |
|
4637 |
|begin "{ Class: SmallInteger }" |
|
4638 |
end "{ Class: SmallInteger }" |
|
4639 |
b "{ Class: SmallInteger }" |
|
4640 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4641 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4642 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4643 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4644 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4645 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4646 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4647 |
[stack notEmpty] whileTrue:[ |
8874 | 4648 |
end := stack removeLast. |
4649 |
begin := stack removeLast. |
|
4650 |
||
4651 |
b := begin. |
|
4652 |
e := end. |
|
4653 |
middleElement := self at:((b + e) // 2). |
|
4654 |
||
4655 |
[b < e] whileTrue:[ |
|
4656 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4657 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4658 |
||
4659 |
(b <= e) ifTrue:[ |
|
4660 |
(b == e) ifFalse:[ |
|
4661 |
temp1 := self at:b. temp2 := self at:e. |
|
4662 |
self at:b put:temp2. self at:e put:temp1 |
|
4663 |
]. |
|
4664 |
b := b + 1. |
|
4665 |
e := e - 1 |
|
4666 |
] |
|
4667 |
]. |
|
4668 |
(begin < e) ifTrue:[ |
|
4669 |
stack add:begin. |
|
4670 |
stack add:e. |
|
4671 |
]. |
|
4672 |
(b < end) ifTrue:[ |
|
4673 |
stack add:b. |
|
4674 |
stack add:end. |
|
4675 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4676 |
] |
2069 | 4677 |
! |
4678 |
||
4679 |
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
|
4680 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4681 |
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
|
4682 |
with degenerated collections." |
2069 | 4683 |
|
4684 |
|begin "{ Class: SmallInteger }" |
|
4685 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4686 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4687 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4688 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4689 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4690 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4691 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4692 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4693 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4694 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4695 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4696 |
[stack notEmpty] whileTrue:[ |
8874 | 4697 |
end := stack removeLast. |
4698 |
begin := stack removeLast. |
|
4699 |
||
4700 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4701 |
elB := self at:begin. |
|
4702 |
elE := self at:end. |
|
4703 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4704 |
temp := elB. elB := elE. elE := temp. |
|
4705 |
self at:begin put:elB. |
|
4706 |
self at:end put:elE. |
|
4707 |
]. |
|
4708 |
||
4709 |
nToSort > 2 ifTrue:[ |
|
4710 |
m := (begin + end) // 2. |
|
4711 |
elM := self at:m. |
|
4712 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4713 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4714 |
temp := self at:end. |
|
4715 |
self at:end put:(self at:m). |
|
4716 |
self at:m put:temp. |
|
4717 |
elM := elE |
|
4718 |
] |
|
4719 |
] ifFalse:[ |
|
4720 |
temp := self at:begin. |
|
4721 |
self at:begin put:(self at:m). |
|
4722 |
self at:m put:temp. |
|
4723 |
elM := elB |
|
4724 |
]. |
|
4725 |
nToSort > 3 ifTrue:[ |
|
4726 |
bRun := begin. |
|
4727 |
eRun := end. |
|
4728 |
[ |
|
4729 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4730 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4731 |
bRun <= eRun |
|
4732 |
] whileTrue:[ |
|
4733 |
temp := self at:bRun. |
|
4734 |
self at:bRun put:(self at:eRun). |
|
4735 |
self at:eRun put:temp. |
|
4736 |
]. |
|
4737 |
stack add:begin. |
|
4738 |
stack add:eRun. |
|
4739 |
||
4740 |
stack add:bRun. |
|
4741 |
stack add:end. |
|
4742 |
] |
|
4743 |
] |
|
4744 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4745 |
] |
2069 | 4746 |
! |
4747 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4748 |
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
|
4749 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4750 |
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
|
4751 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4752 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4753 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4754 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4755 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4756 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4757 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4758 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4759 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4760 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4761 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4762 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4763 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4764 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4765 |
[stack notEmpty] whileTrue:[ |
8874 | 4766 |
end := stack removeLast. |
4767 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4768 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4769 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 4770 |
elB := self at:begin. |
4771 |
elE := self at:end. |
|
4772 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
4773 |
temp := elB. elB := elE. elE := temp. |
|
4774 |
self at:begin put:elB. |
|
4775 |
self at:end put:elE. |
|
4776 |
]. |
|
4777 |
||
4778 |
nToSort > 2 ifTrue:[ |
|
4779 |
m := (begin + end) // 2. |
|
4780 |
elM := self at:m. |
|
4781 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
4782 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
4783 |
temp := self at:end. |
|
4784 |
self at:end put:(self at:m). |
|
4785 |
self at:m put:temp. |
|
4786 |
elM := elE |
|
4787 |
] |
|
4788 |
] ifFalse:[ |
|
4789 |
temp := self at:begin. |
|
4790 |
self at:begin put:(self at:m). |
|
4791 |
self at:m put:temp. |
|
4792 |
elM := elB |
|
4793 |
]. |
|
4794 |
nToSort > 3 ifTrue:[ |
|
4795 |
bRun := begin. |
|
4796 |
eRun := end. |
|
4797 |
[ |
|
4798 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
4799 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
4800 |
bRun <= eRun |
|
4801 |
] whileTrue:[ |
|
4802 |
temp := self at:bRun. |
|
4803 |
self at:bRun put:(self at:eRun). |
|
4804 |
self at:eRun put:temp. |
|
4805 |
]. |
|
4806 |
stack add:begin. |
|
4807 |
stack add:eRun. |
|
4808 |
||
4809 |
stack add:bRun. |
|
4810 |
stack add:end. |
|
4811 |
] |
|
4812 |
] |
|
4813 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4814 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4815 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4816 |
|
2069 | 4817 |
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
|
4818 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4819 |
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
|
4820 |
with degenerated collections." |
2069 | 4821 |
|
4822 |
|begin "{ Class: SmallInteger }" |
|
4823 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4824 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4825 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4826 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4827 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4828 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4829 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4830 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4831 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4832 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4833 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4834 |
[stack notEmpty] whileTrue:[ |
8874 | 4835 |
end := stack removeLast. |
4836 |
begin := stack removeLast. |
|
4837 |
||
4838 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4839 |
||
4840 |
elB := self at:begin. |
|
4841 |
elE := self at:end. |
|
4842 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4843 |
temp := elB. elB := elE. elE := temp. |
|
4844 |
self at:begin put:elB. |
|
4845 |
self at:end put:elE. |
|
4846 |
||
4847 |
temp := aCollection at:end. |
|
4848 |
aCollection at:end put:(aCollection at:begin). |
|
4849 |
aCollection at:begin put:temp. |
|
4850 |
]. |
|
4851 |
||
4852 |
nToSort > 2 ifTrue:[ |
|
4853 |
m := (begin + end) // 2. |
|
4854 |
elM := self at:m. |
|
4855 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4856 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4857 |
temp := self at:end. |
|
4858 |
self at:end put:(self at:m). |
|
4859 |
self at:m put:temp. |
|
4860 |
||
4861 |
temp := aCollection at:end. |
|
4862 |
aCollection at:end put:(aCollection at:m). |
|
4863 |
aCollection at:m put:temp. |
|
4864 |
||
4865 |
elM := elE |
|
4866 |
] |
|
4867 |
] ifFalse:[ |
|
4868 |
temp := self at:begin. |
|
4869 |
self at:begin put:(self at:m). |
|
4870 |
self at:m put:temp. |
|
4871 |
||
4872 |
temp := aCollection at:begin. |
|
4873 |
aCollection at:begin put:(aCollection at:m). |
|
4874 |
aCollection at:m put:temp. |
|
4875 |
elM := elB |
|
4876 |
]. |
|
4877 |
nToSort > 3 ifTrue:[ |
|
4878 |
bRun := begin. |
|
4879 |
eRun := end. |
|
4880 |
[ |
|
4881 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4882 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4883 |
bRun <= eRun |
|
4884 |
] whileTrue:[ |
|
4885 |
temp := self at:bRun. |
|
4886 |
self at:bRun put:(self at:eRun). |
|
4887 |
self at:eRun put:temp. |
|
4888 |
||
4889 |
temp := aCollection at:bRun. |
|
4890 |
aCollection at:bRun put:(aCollection at:eRun). |
|
4891 |
aCollection at:eRun put:temp. |
|
4892 |
]. |
|
4893 |
stack add:begin. |
|
4894 |
stack add:eRun. |
|
4895 |
||
4896 |
stack add:bRun. |
|
4897 |
stack add:end. |
|
4898 |
] |
|
4899 |
] |
|
4900 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4901 |
] |
2069 | 4902 |
! |
4903 |
||
4904 |
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
|
4905 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4906 |
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
|
4907 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4908 |
|
8874 | 4909 |
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
|
4910 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4911 |
|
4912 |
|begin "{ Class: SmallInteger }" |
|
4913 |
end "{ Class: SmallInteger }" |
|
4914 |
b "{ Class: SmallInteger }" |
|
4915 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4916 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4917 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4918 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4919 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4920 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4921 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4922 |
[stack notEmpty] whileTrue:[ |
8874 | 4923 |
end := stack removeLast. |
4924 |
begin := stack removeLast. |
|
4925 |
||
4926 |
b := begin. |
|
4927 |
e := end. |
|
4928 |
middleElement := self at:((b + e) // 2). |
|
4929 |
||
4930 |
[b < e] whileTrue:[ |
|
4931 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4932 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4933 |
||
4934 |
(b <= e) ifTrue:[ |
|
4935 |
(b == e) ifFalse:[ |
|
4936 |
temp1 := self at:b. temp2 := self at:e. |
|
4937 |
self at:b put:temp2. self at:e put:temp1. |
|
4938 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
4939 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
4940 |
]. |
|
4941 |
b := b + 1. |
|
4942 |
e := e - 1 |
|
4943 |
] |
|
4944 |
]. |
|
4945 |
(begin < e) ifTrue:[ |
|
4946 |
stack add:begin. |
|
4947 |
stack add:e. |
|
4948 |
]. |
|
4949 |
(b < end) ifTrue:[ |
|
4950 |
stack add:b. |
|
4951 |
stack add:end. |
|
4952 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4953 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4954 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4955 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4956 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4957 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4958 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4959 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4960 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4961 |
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
|
4962 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4963 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4964 |
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
|
4965 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4966 |
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
|
4967 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4968 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4969 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4970 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4971 |
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
|
4972 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4973 |
"/ 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
|
4974 |
^ 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
|
4975 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4976 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4977 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4978 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4979 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4980 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4981 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4982 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4983 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4984 |
"/ 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
|
4985 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4986 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4987 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4988 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4989 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4990 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4991 |
"/ "/ 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
|
4992 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4993 |
"/ 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
|
4994 |
"/ 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
|
4995 |
"/ 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
|
4996 |
"/ aCollection notNil ifTrue:[ |
8874 | 4997 |
"/ 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
|
4998 |
"/ 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
|
4999 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5000 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5001 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5002 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5003 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5004 |
"/ 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
|
5005 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5006 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5007 |
"/ [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
|
5008 |
"/ [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
|
5009 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5010 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5011 |
"/ (b == e) ifFalse:[ |
8874 | 5012 |
"/ 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
|
5013 |
"/ 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
|
5014 |
"/ aCollection notNil ifTrue:[ |
8874 | 5015 |
"/ 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
|
5016 |
"/ 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
|
5017 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5018 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5019 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5020 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5021 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5022 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5023 |
"/ (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
|
5024 |
"/ (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
|
5025 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5026 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5027 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5028 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5029 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5030 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5031 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5032 |
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
|
5033 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5034 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5035 |
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
|
5036 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5037 |
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
|
5038 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5039 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5040 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5041 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5042 |
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
|
5043 |
"/ 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
|
5044 |
^ 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
|
5045 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5046 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5047 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5048 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5049 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5050 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5051 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5052 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5053 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5054 |
"/ 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
|
5055 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5056 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5057 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5058 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5059 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5060 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5061 |
"/ "/ 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
|
5062 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5063 |
"/ 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
|
5064 |
"/ 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
|
5065 |
"/ 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
|
5066 |
"/ aCollection notNil ifTrue:[ |
8874 | 5067 |
"/ 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
|
5068 |
"/ 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
|
5069 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5070 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5071 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5072 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5073 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5074 |
"/ 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
|
5075 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5076 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5077 |
"/ [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
|
5078 |
"/ [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
|
5079 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5080 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5081 |
"/ (b == e) ifFalse:[ |
8874 | 5082 |
"/ 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
|
5083 |
"/ 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
|
5084 |
"/ aCollection notNil ifTrue:[ |
8874 | 5085 |
"/ 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
|
5086 |
"/ 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
|
5087 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5088 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5089 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5090 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5091 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5092 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5093 |
"/ (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
|
5094 |
"/ (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
|
5095 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5096 |
"/ "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
|
5097 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5098 |
! ! |
5099 |
||
602 | 5100 |
!SequenceableCollection methodsFor:'queries'! |
5101 |
||
5102 |
firstIndex |
|
5103 |
"return the first elements index" |
|
5104 |
||
5105 |
^ 1 |
|
5106 |
! |
|
5107 |
||
5108 |
isSequenceable |
|
5109 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5110 |
||
5111 |
^ true |
|
1 | 5112 |
! |
5113 |
||
602 | 5114 |
isSequenceableCollection |
5115 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5116 |
This method is a historic leftover and will be removed." |
|
5117 |
||
5872 | 5118 |
<resource:#obsolete> |
5119 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5120 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5121 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5122 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5123 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5124 |
! |
5125 |
||
5358 | 5126 |
isSorted |
5127 |
"return true. if my elements are sorted (already)" |
|
5128 |
||
5129 |
|lastEl el| |
|
5130 |
||
5131 |
lastEl := self first. |
|
5132 |
2 to:self size do:[:i | |
|
8874 | 5133 |
el := self at:i. |
5134 |
el >= lastEl ifFalse: [^ false]. |
|
5135 |
lastEl := el |
|
5358 | 5136 |
]. |
5137 |
^ true |
|
5138 |
||
5139 |
" |
|
8874 | 5140 |
#(1 2 3 5 10 100) isSorted |
5141 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5142 |
" |
5143 |
! |
|
5144 |
||
5145 |
isSortedBy:aBlock |
|
5146 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5147 |
||
5148 |
|lastEl el| |
|
5149 |
||
5150 |
lastEl := self first. |
|
5151 |
2 to:self size do:[:i | |
|
8874 | 5152 |
el := self at:i. |
5153 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5154 |
lastEl := el |
|
5358 | 5155 |
]. |
5156 |
^ true |
|
5157 |
||
5158 |
" |
|
8874 | 5159 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5160 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5161 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5162 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5163 |
" |
5164 |
||
5165 |
! |
|
5166 |
||
602 | 5167 |
keys |
5168 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5169 |
||
5170 |
|sz| |
|
5171 |
||
1 | 5172 |
sz := self size. |
602 | 5173 |
sz == 0 ifTrue:[ |
8874 | 5174 |
^ #() |
61 | 5175 |
]. |
602 | 5176 |
^ 1 to:sz |
1 | 5177 |
! |
5178 |
||
602 | 5179 |
lastIndex |
5180 |
"return the last elements index" |
|
5181 |
||
5182 |
^ self size |
|
61 | 5183 |
! |
5184 |
||
602 | 5185 |
size |
5186 |
"return the number of elements in the collection. |
|
5187 |
concrete implementations must define this" |
|
5188 |
||
5189 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5190 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5191 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5192 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5193 |
"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
|
5194 |
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
|
5195 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5196 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5197 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5198 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5199 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5200 |
! ! |
5201 |
||
5202 |
!SequenceableCollection methodsFor:'searching'! |
|
5203 |
||
3887 | 5204 |
detect:aBlock startingAt:startIndex |
5205 |
"find the first element, for which evaluation of the argument, aBlock |
|
5206 |
return true, starting the search with startIndex. |
|
5207 |
If none does so, report an error" |
|
5208 |
||
5209 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5210 |
||
5211 |
" |
|
8874 | 5212 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5213 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5214 |
" |
5215 |
||
5216 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5217 |
! |
|
5218 |
||
5219 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5220 |
"find the first element, for which evaluation of the argument, aBlock |
|
5221 |
return true, starting the search with startIndex. |
|
5717 | 5222 |
If none does so, return the evaluation of exceptionBlock" |
5223 |
||
5224 |
|stop| |
|
3887 | 5225 |
|
5226 |
stop := self size. |
|
5717 | 5227 |
self from:startIndex to:stop do:[:el | |
8874 | 5228 |
(aBlock value:el) ifTrue:[ |
5229 |
^ el |
|
5230 |
]. |
|
3887 | 5231 |
]. |
5232 |
^ exceptionBlock value |
|
5233 |
||
5234 |
" |
|
8874 | 5235 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5236 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5237 |
" |
5238 |
||
5239 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5240 |
! |
|
5241 |
||
602 | 5242 |
findFirst:aBlock |
10453 | 5243 |
"find the index of the first element, for which evaluation of the argument, aBlock |
3887 | 5244 |
returns true; return its index or 0 if none detected. |
5245 |
This is much like #detect, however, here an INDEX is returned, |
|
5246 |
while #detect returns the element." |
|
602 | 5247 |
|
5248 |
|stop "{ Class: SmallInteger }" | |
|
5249 |
||
32 | 5250 |
stop := self size. |
602 | 5251 |
1 to:stop do:[:index | |
10453 | 5252 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5253 |
]. |
5254 |
^ 0 |
|
159 | 5255 |
|
95 | 5256 |
" |
602 | 5257 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
5258 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
8874 | 5259 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
602 | 5260 |
'one.two.three' findFirst:[:c | (c == $.)] |
8874 | 5261 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
5262 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
95 | 5263 |
" |
3887 | 5264 |
|
5265 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
1 | 5266 |
! |
5267 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5268 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5269 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5270 |
returns true; return its index or 0 if none detected. |
5271 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5272 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5273 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5274 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5275 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5276 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5277 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5278 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5279 |
start to:stop do:[:index | |
8874 | 5280 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5281 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5282 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5283 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5284 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5285 |
#(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
|
5286 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5287 |
" |
3887 | 5288 |
|
5289 |
"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
|
5290 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5291 |
|
602 | 5292 |
findLast:aBlock |
5293 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5294 |
returns true. |
5295 |
Return its index or 0 if none detected." |
|
602 | 5296 |
|
5297 |
|start "{ Class: SmallInteger }"| |
|
5298 |
||
5299 |
start := self size. |
|
5300 |
start to:1 by:-1 do:[:index | |
|
8874 | 5301 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5302 |
]. |
5303 |
^ 0 |
|
5304 |
||
5305 |
" |
|
602 | 5306 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5307 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5308 |
" |
5309 |
! |
|
5310 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5311 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5312 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5313 |
returns true. Start the search at startIndex. |
5314 |
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
|
5315 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5316 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5317 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5318 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5319 |
start to:1 by:-1 do:[:index | |
8874 | 5320 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5321 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5322 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5323 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5324 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5325 |
#(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
|
5326 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5327 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5328 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5329 |
|
8193 | 5330 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
5331 |
"find the last element, for which evaluation of the argument, aBlock |
|
5332 |
returns true. Start the search at startIndex. |
|
5333 |
End the search at endIndex or when an element is found. |
|
5334 |
Return its index or 0 if none detected." |
|
5335 |
||
5336 |
|start "{ Class: SmallInteger }"| |
|
5337 |
||
5338 |
start := startIndex. |
|
5339 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 5340 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 5341 |
]. |
5342 |
^ 0 |
|
5343 |
||
5344 |
" |
|
5345 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
5346 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
5347 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
5348 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
5349 |
" |
|
5350 |
! |
|
5351 |
||
159 | 5352 |
indexOfAny:aCollection |
5353 |
"search the collection for an element in aCollection. |
|
5354 |
if found, return the index otherwise return 0. |
|
8874 | 5355 |
The comparison is done using = |
159 | 5356 |
(i.e. equality test - not identity test). |
5357 |
||
5358 |
Notice, that for big collections, the runtime of this search |
|
5359 |
grows proportional to size(receiver) * size(aCollection). |
|
5360 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5361 |
||
5362 |
^ self indexOfAny:aCollection startingAt:1 |
|
5363 |
||
5364 |
" |
|
5365 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
5366 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
5367 |
" |
|
5368 |
! |
|
5369 |
||
5370 |
indexOfAny:aCollection startingAt:start |
|
5371 |
"search the collection for an element in aCollection, |
|
5372 |
starting the search at index start; |
|
5373 |
if found, return the index otherwise return 0. |
|
8874 | 5374 |
The comparison is done using = |
159 | 5375 |
(i.e. equality test - not identity test). |
5376 |
||
5377 |
Notice, that for big collections, the runtime of this search |
|
5378 |
grows proportional to size(receiver) * size(aCollection). |
|
5379 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5380 |
||
5381 |
||
5382 |
|startIndex "{ Class: SmallInteger }" |
|
5383 |
stop "{ Class: SmallInteger }" | |
|
5384 |
||
5385 |
startIndex := start. |
|
5386 |
stop := self size. |
|
5387 |
startIndex to:stop do:[:index | |
|
8874 | 5388 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 5389 |
]. |
5390 |
^ 0 |
|
5391 |
" |
|
8874 | 5392 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
5393 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 5394 |
" |
1 | 5395 |
! |
5396 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5397 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5398 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5399 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5400 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 5401 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5402 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5403 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5404 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5405 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5406 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5407 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5408 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5409 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5410 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5411 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5412 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5413 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5414 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5415 |
" |
8874 | 5416 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5417 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
5418 |
#(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
|
5419 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5420 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5421 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5422 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5423 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5424 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5425 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5426 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5427 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5428 |
#(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
|
5429 |
#(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
|
5430 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5431 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5432 |
|
8874 | 5433 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5434 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5435 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5436 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5437 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5438 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5439 |
#(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
|
5440 |
#(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
|
5441 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5442 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5443 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5444 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5445 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5446 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5447 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5448 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5449 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5450 |
|
368 | 5451 |
indexOfSubCollection:aCollection |
9234 | 5452 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 5453 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5454 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 5455 |
|
5456 |
" |
|
8874 | 5457 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
5458 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
5459 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
5460 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
5461 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
5462 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
5463 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
5464 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 5465 |
" |
5466 |
! |
|
5467 |
||
602 | 5468 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
5469 |
"find a subcollection. If found, return the index; |
|
5470 |
if not found, return the result of evaluating exceptionBlock." |
|
5471 |
||
5472 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
5473 |
! |
|
5474 |
||
368 | 5475 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5476 |
"find a subcollection starting at index. |
368 | 5477 |
If found, return the index; if not found, return 0." |
5478 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5479 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 5480 |
! |
5481 |
||
5482 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
5483 |
"find a subcollection, starting at index. If found, return the index; |
|
5484 |
if not found, return the result of evaluating exceptionBlock. |
|
5485 |
This is a q&d hack - not very efficient" |
|
5486 |
||
8874 | 5487 |
|same first |
368 | 5488 |
mySize "{Class: SmallInteger }" |
5489 |
checkIndex "{Class: SmallInteger }" |
|
5490 |
cmpIndex "{Class: SmallInteger }" |
|
5491 |
sz "{Class: SmallInteger }"| |
|
5492 |
||
5493 |
mySize := self size. |
|
5494 |
sz := aCollection size. |
|
5495 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
5496 |
first := aCollection at:1. |
|
5497 |
||
8874 | 5498 |
checkIndex := startIndex - 1. |
368 | 5499 |
[true] whileTrue:[ |
8874 | 5500 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
5501 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5502 |
||
5503 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
5504 |
^ 0 |
|
5505 |
]. |
|
5506 |
same := true. |
|
5507 |
cmpIndex := 1. |
|
5508 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5509 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
5510 |
same := false |
|
5511 |
] ifTrue:[ |
|
5512 |
cmpIndex := cmpIndex + 1 |
|
5513 |
] |
|
5514 |
]. |
|
5515 |
same ifTrue:[^ checkIndex]. |
|
5516 |
] |
|
5517 |
||
5518 |
" |
|
5519 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
5520 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
5521 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
5522 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
5523 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
5524 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 5525 |
" |
602 | 5526 |
! |
5527 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5528 |
lastIndexOfSubCollection:aCollection |
8874 | 5529 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5530 |
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
|
5531 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5532 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5533 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5534 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5535 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5536 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5537 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5538 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5539 |
"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
|
5540 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5541 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5542 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5543 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5544 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5545 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5546 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5547 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5548 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5549 |
"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
|
5550 |
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
|
5551 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5552 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5553 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5554 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5555 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5556 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5557 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5558 |
"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
|
5559 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5560 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5561 |
|
8874 | 5562 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5563 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5564 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5565 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5566 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5567 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5568 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5569 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5570 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5571 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5572 |
|
8874 | 5573 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5574 |
checkIndex > mySize ifTrue:[ |
8874 | 5575 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5576 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5577 |
[true] whileTrue:[ |
8874 | 5578 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
5579 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5580 |
||
5581 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
5582 |
^ 0 |
|
5583 |
]. |
|
5584 |
same := true. |
|
5585 |
cmpIndex := 1. |
|
5586 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5587 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
5588 |
same := false |
|
5589 |
] ifTrue:[ |
|
5590 |
cmpIndex := cmpIndex + 1 |
|
5591 |
] |
|
5592 |
]. |
|
5593 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
5594 |
] |
|
5595 |
||
5596 |
" |
|
5597 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
5598 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
5599 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
5600 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
5601 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
5602 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
5603 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
5604 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
5605 |
#(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
|
5606 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5607 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5608 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5609 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5610 |
|
7264 | 5611 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5612 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5613 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5614 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5615 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5616 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5617 |
|
10194 | 5618 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5619 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5620 |
" |
8874 | 5621 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5622 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5623 |
returns: true - if found |
10194 | 5624 |
false - if not found |
8874 | 5625 |
" |
5626 |
||
5627 |
" |
|
5628 |
#(10 20 30 40 50 60 70) includes:99 |
|
5629 |
#(10 20 30 40 50 60 70) includes:40 |
|
5630 |
#(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
|
5631 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5632 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5633 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5634 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5635 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5636 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5637 |
"search the collection for anElement; |
602 | 5638 |
if found, return the index otherwise return 0. |
8874 | 5639 |
The comparison is done using = |
602 | 5640 |
(i.e. equality test - not identity test)." |
5641 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5642 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5643 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5644 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5645 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5646 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5647 |
returns: elementIndex - if found |
8874 | 5648 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5649 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5650 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5651 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5652 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5653 |
#(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
|
5654 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5655 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5656 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5657 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5658 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5659 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5660 |
"search the collection for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5661 |
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
|
5662 |
exceptionBlock. |
8874 | 5663 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5664 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5665 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5666 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5667 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5668 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5669 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5670 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5671 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5672 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5673 |
args: anElement : <object> |
8874 | 5674 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5675 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5676 |
returns: elementIndex - if found |
8874 | 5677 |
exceptionBlock value - if not found |
5678 |
" |
|
5679 |
||
5680 |
" |
|
5681 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
5682 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
5683 |
#(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
|
5684 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5685 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5686 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5687 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5688 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5689 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 5690 |
"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
|
5691 |
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
|
5692 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5693 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5694 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5695 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5696 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5697 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5698 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 5699 |
self at:idx put:replacement. |
5700 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5701 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5702 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5703 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5704 |
" |
8874 | 5705 |
args: elementToFind : <object> |
5706 |
replacement : <integer> |
|
5707 |
start : <integer> |
|
5708 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5709 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5710 |
returns: elementIndex - if found (and replaced) |
8874 | 5711 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5712 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5713 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5714 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5715 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5716 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5717 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5718 |
(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
|
5719 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5720 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5721 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5722 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5723 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5724 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5725 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5726 |
"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
|
5727 |
if found, return the index otherwise return 0. |
8874 | 5728 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5729 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5730 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5731 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5732 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5733 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5734 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5735 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5736 |
startIndex to:stop do:[:index | |
8874 | 5737 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5738 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5739 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5740 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5741 |
" |
8874 | 5742 |
args: anElement : <object> |
5743 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5744 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5745 |
returns: elementIndex - if found |
8874 | 5746 |
0 - if not found |
5747 |
" |
|
5748 |
||
5749 |
" |
|
5750 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
5751 |
#(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
|
5752 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5753 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5754 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5755 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5756 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5757 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5758 |
"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
|
5759 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5760 |
If found (within the range), return the index, otherwise return 0. |
8874 | 5761 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5762 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5763 |
|
602 | 5764 |
|startIndex "{ Class: SmallInteger }" |
5765 |
stopIndex "{ Class: SmallInteger }" | |
|
5766 |
||
5767 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5768 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5769 |
startIndex to:stopIndex do:[:index | |
8874 | 5770 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5771 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5772 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5773 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5774 |
" |
8874 | 5775 |
args: anElement : <object> |
5776 |
start : <integer> |
|
5777 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5778 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5779 |
returns: elementIndex - if found |
8874 | 5780 |
exceptionBlock value - if not found |
5781 |
" |
|
5782 |
||
5783 |
" |
|
5784 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
5785 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
5786 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
5787 |
#(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
|
5788 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5789 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5790 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5791 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5792 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5793 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5794 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5795 |
"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
|
5796 |
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
|
5797 |
exceptionBlock. |
8874 | 5798 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5799 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5800 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5801 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5802 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5803 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5804 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5805 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5806 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5807 |
" |
8874 | 5808 |
args: anElement : <object> |
5809 |
start : <integer> |
|
5810 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5811 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5812 |
returns: elementIndex - if found |
8874 | 5813 |
exceptionBlock value - if not found |
5814 |
" |
|
5815 |
||
5816 |
" |
|
5817 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
5818 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
5819 |
#(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
|
5820 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5821 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5822 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5823 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5824 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5825 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5826 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5827 |
if found, return the index otherwise return 0. |
8874 | 5828 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5829 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5830 |
|
8874 | 5831 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5832 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5833 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5834 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5835 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5836 |
returns: elementIndex - if found |
8874 | 5837 |
0 - if not found |
5838 |
" |
|
5839 |
||
5840 |
" |
|
5841 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
5842 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
5843 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
5844 |
#(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
|
5845 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5846 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5847 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5848 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5849 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5850 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5851 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5852 |
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
|
5853 |
exceptionBlock. |
8874 | 5854 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5855 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5856 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5857 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5858 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5859 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5860 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5861 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5862 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5863 |
" |
8874 | 5864 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
5865 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
5866 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
5867 |
#(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
|
5868 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5869 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5870 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5871 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5872 |
"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
|
5873 |
if found, return the index otherwise return 0. |
8874 | 5874 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5875 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5876 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5877 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5878 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5879 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5880 |
startIndex to:1 by:-1 do:[:index | |
8874 | 5881 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 5882 |
]. |
5883 |
^ 0 |
|
5884 |
||
5885 |
" |
|
8874 | 5886 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
5887 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
5888 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
5889 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
5890 |
#(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
|
5891 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5892 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5893 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5894 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 5895 |
"search the collection backwards for anElement starting the search at |
5896 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5897 |
otherwise return the value of the exceptionBlock. |
8874 | 5898 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5899 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5900 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5901 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5902 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5903 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5904 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5905 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5906 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5907 |
" |
8874 | 5908 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
5909 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
5910 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
5911 |
#(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
|
5912 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5913 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5914 |
|
4788 | 5915 |
lastIndexOfAny:aCollection |
5916 |
"search the collection backwards for any in aCollection; |
|
5917 |
if found, return the index, otherwise return 0. |
|
8874 | 5918 |
The comparison is done using = |
4788 | 5919 |
(i.e. equality test - not identity test)." |
5920 |
||
8874 | 5921 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 5922 |
|
5923 |
" |
|
5924 |
args: aCollection : <collection> |
|
5925 |
||
5926 |
returns: elementIndex - if found |
|
8874 | 5927 |
0 - if not found |
5928 |
" |
|
5929 |
||
5930 |
" |
|
5931 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 5932 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 5933 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
5934 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 5935 |
" |
5936 |
||
5937 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
5938 |
! |
|
5939 |
||
5940 |
lastIndexOfAny:aCollection startingAt:start |
|
5941 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
5942 |
if found, return the index, otherwise return 0. |
|
8874 | 5943 |
The comparison is done using = |
4788 | 5944 |
(i.e. equality test - not identity test)." |
5945 |
||
5946 |
|startIndex "{ Class: SmallInteger }"| |
|
5947 |
||
5948 |
startIndex := start. |
|
5949 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 5950 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 5951 |
]. |
5952 |
^ 0 |
|
5953 |
||
5954 |
" |
|
8874 | 5955 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
5956 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 5957 |
" |
5958 |
! |
|
5959 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5960 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5961 |
"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
|
5962 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5963 |
if found, return the index otherwise return nil. |
8874 | 5964 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5965 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5966 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5967 |
NOTICE: nil is returned here if the element is not found, |
8874 | 5968 |
although this is somewhat strange (the indexOf-search |
5969 |
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
|
5970 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5971 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5972 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5973 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5974 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5975 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5976 |
startIndex to:stop do:[:index | |
8874 | 5977 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5978 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5979 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5980 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5981 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5982 |
args: anElement : <object> |
8874 | 5983 |
start : <integer> |
5984 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5985 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5986 |
returns: elementIndex - if found |
8874 | 5987 |
nil - if not found |
5988 |
" |
|
5989 |
||
5990 |
" |
|
5991 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
5992 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
5993 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 5994 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5995 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5996 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 5997 |
! |
5998 |
||
5999 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6000 |
"search the collection for anElement, starting the search at index start |
|
6001 |
and stopping at stop; |
|
6002 |
if found, return the index otherwise return the value of the |
|
6003 |
exceptionBlock. |
|
8874 | 6004 |
The comparison is done using = |
602 | 6005 |
(i.e. equality test - not identity test)." |
6006 |
||
6007 |
|index| |
|
6008 |
||
6009 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6010 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 6011 |
^ index |
6012 |
||
6013 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6014 |
args: anElement : <object> |
8874 | 6015 |
start : <integer> |
6016 |
stop : <integer> |
|
6017 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6018 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6019 |
returns: elementIndex - if found |
8874 | 6020 |
valueImplementor value - if not found |
6021 |
" |
|
6022 |
||
6023 |
" |
|
6024 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6025 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6026 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6027 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6028 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6029 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 6030 |
! |
6031 |
||
6032 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6033 |
"search the collection for anElement, starting the search at index start; |
|
6034 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6035 |
If found (within the range), return the index, otherwise return nil. |
8874 | 6036 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6037 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6038 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6039 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6040 |
although this is somewhat strange (the indexOf-search |
6041 |
methods return 0), this is done for VW compatibility." |
|
2275 | 6042 |
|
6043 |
|startIndex "{ Class: SmallInteger }" |
|
6044 |
stopIndex "{ Class: SmallInteger }" | |
|
6045 |
||
6046 |
startIndex := startSearchIndex. |
|
6047 |
stopIndex := endSearchIndex max:1. |
|
6048 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 6049 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6050 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6051 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6052 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6053 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6054 |
args: anElement : <object> |
8874 | 6055 |
start : <integer> |
6056 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6057 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6058 |
returns: elementIndex - if found |
8874 | 6059 |
nil - if not found |
6060 |
" |
|
6061 |
||
6062 |
" |
|
6063 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6064 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6065 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6066 |
" |
6067 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6068 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6069 |
! |
6070 |
||
6071 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6072 |
"search the collection for anElement, starting the search at index start |
|
6073 |
and stopping at stop, doing a reverse search; |
|
6074 |
if found, return the index otherwise return the value of the |
|
6075 |
exceptionBlock. |
|
8874 | 6076 |
The comparison is done using = |
2275 | 6077 |
(i.e. equality test - not identity test)." |
6078 |
||
6079 |
|index| |
|
6080 |
||
6081 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6082 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6083 |
^ index |
|
6084 |
||
6085 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6086 |
args: anElement : <object> |
8874 | 6087 |
start : <integer> |
6088 |
stop : <integer> |
|
6089 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6090 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6091 |
returns: elementIndex - if found |
8874 | 6092 |
valueImplementor value - if not found |
6093 |
" |
|
6094 |
||
6095 |
" |
|
6096 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6097 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6098 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6099 |
" |
6100 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6101 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6102 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6103 |
|
7264 | 6104 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6105 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6106 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6107 |
"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
|
6108 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6109 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6110 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6111 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6112 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6113 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6114 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6115 |
returns: elementIndex - if found |
8874 | 6116 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6117 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6118 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6119 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6120 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6121 |
#(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
|
6122 |
#(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
|
6123 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6124 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6125 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6126 |
#(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
|
6127 |
#(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
|
6128 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6129 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6130 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6131 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6132 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6133 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6134 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6135 |
"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
|
6136 |
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
|
6137 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6138 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6139 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6140 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6141 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6142 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6143 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6144 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6145 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6146 |
args: anElement : <object> |
8874 | 6147 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6148 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6149 |
returns: elementIndex - if found |
8874 | 6150 |
exceptionBlock value - if not found |
6151 |
" |
|
6152 |
||
6153 |
" |
|
6154 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6155 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6156 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6157 |
#(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
|
6158 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6159 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6160 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6161 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6162 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6163 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6164 |
"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
|
6165 |
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
|
6166 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6167 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6168 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6169 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6170 |
args: anElement : <object> |
8874 | 6171 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6172 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6173 |
returns: elementIndex - if found |
8874 | 6174 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6175 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6176 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6177 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6178 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6179 |
"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
|
6180 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6181 |
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
|
6182 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6183 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6184 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6185 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6186 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6187 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6188 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6189 |
startIndex to:stop do:[:index | |
8874 | 6190 |
el := self at:index. |
6191 |
anElement == el ifTrue:[^ index]. |
|
6192 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6193 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6194 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6195 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6196 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6197 |
args: anElement : <object> |
8874 | 6198 |
anotherElement : <object> |
6199 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6200 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6201 |
returns: elementIndex - if found |
8874 | 6202 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6203 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6204 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6205 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6206 |
#(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
|
6207 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6208 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6209 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6210 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6211 |
"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
|
6212 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6213 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6214 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6215 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6216 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6217 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6218 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6219 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6220 |
startIndex to:stop do:[:index | |
8874 | 6221 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6222 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6223 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6224 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6225 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6226 |
args: anElement : <object> |
8874 | 6227 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6228 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6229 |
returns: elementIndex - if found |
8874 | 6230 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6231 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6232 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6233 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6234 |
#(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
|
6235 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6236 |
#(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
|
6237 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6238 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6239 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6240 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6241 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6242 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6243 |
"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
|
6244 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6245 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6246 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6247 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6248 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6249 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6250 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6251 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6252 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6253 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6254 |
startIndex to:stopIndex do:[:index | |
8874 | 6255 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6256 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6257 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6258 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6259 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6260 |
args: anElement : <object> |
8874 | 6261 |
start : <integer> |
6262 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6263 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6264 |
returns: elementIndex - if found |
8874 | 6265 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6266 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6267 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6268 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6269 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6270 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6271 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6272 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6273 |
"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
|
6274 |
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
|
6275 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6276 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6277 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6278 |
|index| |
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 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6281 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6282 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6283 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6284 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6285 |
args: anElement : <object> |
8874 | 6286 |
start : <integer> |
6287 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6288 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6289 |
returns: elementIndex - if found |
8874 | 6290 |
exceptionBlock value - if not found |
6291 |
" |
|
6292 |
||
6293 |
" |
|
6294 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6295 |
#(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
|
6296 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6297 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6298 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6299 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6300 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6301 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6302 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6303 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6304 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6305 |
|
10194 | 6306 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6307 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6308 |
" |
8874 | 6309 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6310 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6311 |
returns: true - if found |
10194 | 6312 |
false - if not found |
8874 | 6313 |
" |
6314 |
||
6315 |
" |
|
6316 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6317 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6318 |
#(10 20 30 40 50 60 70) includes:40 |
|
6319 |
#(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
|
6320 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6321 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6322 |
|
8874 | 6323 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6324 |
#(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
|
6325 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6326 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6327 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6328 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6329 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6331 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6332 |
if found, return the index otherwise return 0. |
8874 | 6333 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6335 |
|
8874 | 6336 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6337 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6338 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6339 |
args: anElement : <object> |
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 |
returns: elementIndex - if found |
8874 | 6342 |
0 - if not found |
6343 |
" |
|
6344 |
||
6345 |
" |
|
6346 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
6347 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
6348 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
6349 |
#(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
|
6350 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6351 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6352 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
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 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6356 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6357 |
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
|
6358 |
exceptionBlock. |
8874 | 6359 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6360 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6361 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6362 |
|index| |
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 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6365 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6366 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6367 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6368 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6369 |
args: anElement : <object> |
8874 | 6370 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6371 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6372 |
returns: elementIndex - if found |
8874 | 6373 |
exceptionBlock value - if not found |
6374 |
" |
|
6375 |
||
6376 |
" |
|
6377 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
6378 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
6379 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
6380 |
#(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
|
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 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6384 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6385 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6386 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6387 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6388 |
"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
|
6389 |
if found, return the index otherwise return 0. |
8874 | 6390 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6391 |
(i.e. identity test - not equality test)." |
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 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6394 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6395 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6396 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6397 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6398 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6399 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6400 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6401 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6402 |
args: anElement : <object> |
8874 | 6403 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6404 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6405 |
returns: elementIndex - if found |
8874 | 6406 |
0 - if not found |
6407 |
" |
|
6408 |
||
6409 |
" |
|
6410 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
6411 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
6412 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
6413 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
6414 |
#(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
|
6415 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6416 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6417 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6418 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6419 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6420 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6421 |
"search the collection backwards for anElement starting the search at |
6422 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6423 |
otherwise return the value of the exceptionBlock. |
8874 | 6424 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6425 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6426 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6427 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6428 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6429 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6430 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6431 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6432 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6433 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6434 |
args: anElement : <object> |
8874 | 6435 |
start : <integer> |
6436 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6437 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6438 |
returns: elementIndex - if found |
8874 | 6439 |
exceptionBlock value - if not found |
6440 |
" |
|
6441 |
||
6442 |
" |
|
6443 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
6444 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6445 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6446 |
#(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
|
6447 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6448 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6449 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6450 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 6451 |
! ! |
6452 |
||
6453 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
6454 |
||
6958 | 6455 |
detectFirstInOrder:sortBlock |
6456 |
"find the first element of the collection sorted with sortBlock" |
|
6457 |
||
6458 |
|selectedForNow start| |
|
6459 |
||
6460 |
start := true. |
|
6461 |
self do:[:element| |
|
8874 | 6462 |
start ifTrue:[ |
6463 |
start := false. |
|
6464 |
selectedForNow := element. |
|
6465 |
] ifFalse:[ |
|
6466 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
6467 |
selectedForNow := element. |
|
6468 |
]. |
|
6469 |
]. |
|
6958 | 6470 |
]. |
6471 |
||
6472 |
^ selectedForNow |
|
6473 |
||
6474 |
" |
|
8874 | 6475 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
6476 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
6477 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
6478 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 6479 |
" |
6480 |
! |
|
6481 |
||
5358 | 6482 |
mergeSort |
8874 | 6483 |
"sort the collection using a mergeSort algorithm. |
6484 |
The elements are compared using'<' |
|
5358 | 6485 |
i.e. they should offer a magnitude-like protocol. |
6486 |
||
6487 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 6488 |
the best possible for all situations |
5358 | 6489 |
See also #quickSort and #randomizedSort for other sort algorithms |
6490 |
with different worst- and average case behavior)" |
|
6491 |
||
6492 |
|stop| |
|
6493 |
||
6494 |
stop := self size. |
|
6495 |
(stop > 1) ifTrue:[ |
|
8874 | 6496 |
self mergeSort:[:a :b | a < b] from:1 to:stop |
5358 | 6497 |
] |
6498 |
||
6499 |
" |
|
6500 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
6501 |
||
6502 |
|data| |
|
6503 |
data := Random new next:100000. |
|
6504 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
6505 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 6506 |
data reverse. |
5358 | 6507 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
6508 |
||
6509 |
data := Random new next:100000. |
|
6510 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6511 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6512 |
data reverse. |
5358 | 6513 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6514 |
||
6515 |
data := Random new next:100000. |
|
6516 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
6517 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 6518 |
data reverse. |
5358 | 6519 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
6520 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6521 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6522 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6523 |
mergeSort:sortBlock |
8874 | 6524 |
"sort the collection using a mergeSort algorithm. |
6525 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6526 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6527 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6528 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 6529 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6530 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6531 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6532 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6533 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6534 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6535 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6536 |
(stop > 1) ifTrue:[ |
8874 | 6537 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6538 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6539 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6540 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6541 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6542 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6543 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6544 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6545 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6546 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6547 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6548 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6549 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6550 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6551 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6552 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 6553 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6554 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6555 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6556 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6557 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6558 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 6559 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6560 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6561 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6562 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6563 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6564 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 6565 |
"Sort the given range of indices using the mergesort algorithm. |
6566 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 6567 |
as many comparisons as heapsort or quicksort." |
6568 |
||
8874 | 6569 |
"Details: recursively split the range to be sorted into two halves, |
6570 |
mergesort each half, then merge the two halves together. |
|
6571 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
6572 |
and forth between the receiver and this copy. |
|
6573 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 6574 |
resulting in the receiver being completely sorted." |
6575 |
||
6576 |
| temp | |
|
6577 |
||
6578 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
6579 |
startIndex = stopIndex ifTrue: [^ self]. |
|
6580 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 6581 |
ifFalse: [self error: 'bad start index']. |
5358 | 6582 |
stopIndex <= self size |
8874 | 6583 |
ifFalse: [self error: 'bad stop index']. |
5358 | 6584 |
temp := self clone. |
6585 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
6586 |
||
6587 |
" |
|
8874 | 6588 |
#(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
|
6589 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6590 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6591 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6592 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6593 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6594 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6595 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6596 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6597 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6598 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6599 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6600 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6601 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6602 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6603 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 6604 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6605 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6606 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6607 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6608 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6609 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6610 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6611 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6612 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6613 |
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
|
6614 |
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
|
6615 |
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
|
6616 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6617 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6618 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6619 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6620 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6621 |
(stop > 1) ifTrue:[ |
8874 | 6622 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6623 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6624 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6625 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6626 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 6627 |
|
6628 |
|data| |
|
6629 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6630 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6631 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 6632 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6633 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6634 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6635 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6636 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6637 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6638 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6639 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6640 |
"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
|
6641 |
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
|
6642 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6643 |
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
|
6644 |
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
|
6645 |
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
|
6646 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6647 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6648 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6649 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6650 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6651 |
(stop > 1) ifTrue:[ |
8874 | 6652 |
sortBlock numArgs == 3 ifTrue:[ |
6653 |
"/ TODO: pass a collating policy to aBlock |
|
6654 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
6655 |
] ifFalse:[ |
|
6656 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
6657 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6658 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6659 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6660 |
" |
8874 | 6661 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6662 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6663 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6664 |
#(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
|
6665 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6666 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6667 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6668 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6669 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6670 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6671 |
"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
|
6672 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6673 |
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
|
6674 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6675 |
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
|
6676 |
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
|
6677 |
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
|
6678 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6679 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6680 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6681 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6682 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6683 |
(stop > 1) ifTrue:[ |
8874 | 6684 |
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
|
6685 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6686 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6687 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6688 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6689 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6690 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6691 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6692 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6693 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6694 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6695 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6696 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6697 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6698 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6699 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6700 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6701 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6702 |
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
|
6703 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6704 |
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
|
6705 |
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
|
6706 |
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
|
6707 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6708 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6709 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6710 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6711 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6712 |
(stop > 1) ifTrue:[ |
8874 | 6713 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6714 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6715 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6716 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 6717 |
! |
6718 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6719 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6720 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6721 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6722 |
|
8874 | 6723 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6724 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6725 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6726 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6727 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6728 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6729 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6730 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6731 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6732 |
"/ 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
|
6733 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6734 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6735 |
"/ 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
|
6736 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6737 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6738 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6739 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6740 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6741 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6742 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6743 |
|
8874 | 6744 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6745 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6746 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6747 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6748 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6749 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6750 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6751 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6752 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6753 |
"/ 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
|
6754 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6755 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6756 |
"/ 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
|
6757 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6758 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6759 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6760 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6761 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6762 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6763 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6764 |
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
|
6765 |
|
8874 | 6766 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6767 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6768 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6769 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6770 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6771 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6772 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6773 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6774 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6775 |
"/ 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
|
6776 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6777 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6778 |
"/ 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
|
6779 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6780 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6781 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6782 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6783 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6784 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6785 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6786 |
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
|
6787 |
|
8874 | 6788 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6789 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6790 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6791 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6792 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6793 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6794 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6795 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6796 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6797 |
"/ 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
|
6798 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6799 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6800 |
"/ 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
|
6801 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6802 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6803 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6804 |
|
602 | 6805 |
reverse |
7335 | 6806 |
"destructively reverse the order of the elements inplace. |
6807 |
WARNING: this is a destructive operation, which modifies the receiver." |
|
602 | 6808 |
|
6809 |
|lowIndex "{ Class: SmallInteger }" |
|
6810 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6811 |
t1 t2| |
602 | 6812 |
|
6813 |
hiIndex := self size. |
|
6814 |
lowIndex := 1. |
|
6815 |
[lowIndex < hiIndex] whileTrue:[ |
|
8874 | 6816 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
6817 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
6818 |
||
6819 |
lowIndex := lowIndex + 1. |
|
6820 |
hiIndex := hiIndex - 1 |
|
602 | 6821 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6822 |
|
69 | 6823 |
" |
602 | 6824 |
#(4 5 6 7 7) reverse |
6825 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 6826 |
" |
1 | 6827 |
! |
6828 |
||
5004 | 6829 |
reversed |
6830 |
"return a copy with elements in reverse order" |
|
6831 |
||
6832 |
^ self copy reverse |
|
6833 |
||
6834 |
" |
|
8874 | 6835 |
#(4 5 6 7 7) reversed |
5004 | 6836 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
6837 |
" |
|
6838 |
! |
|
6839 |
||
1 | 6840 |
sort |
6841 |
"sort the collection inplace. The elements are compared using |
|
2069 | 6842 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6843 |
|
159 | 6844 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6845 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6846 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6847 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 6848 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6849 |
self quickSort |
1 | 6850 |
|
95 | 6851 |
" |
6852 |
#(1 16 7 98 3 19 4 0) sort |
|
6853 |
||
6854 |
|data| |
|
6855 |
data := Random new next:100000. |
|
6856 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6857 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6858 |
data reverse. |
95 | 6859 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6860 |
" |
|
2069 | 6861 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6862 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 6863 |
! |
6864 |
||
6865 |
sort:sortBlock |
|
6866 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6867 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6868 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6869 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6870 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6871 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6872 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6873 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6874 |
self quickSort:sortBlock |
1 | 6875 |
|
95 | 6876 |
" |
8874 | 6877 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6878 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6879 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6880 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 6881 |
" |
2069 | 6882 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
6883 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 6884 |
! |
6885 |
||
6886 |
sort:sortBlock with:aCollection |
|
6887 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 6888 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6889 |
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
|
6890 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6891 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6892 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6893 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6894 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6895 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6896 |
self quickSort:sortBlock with:aCollection |
1 | 6897 |
|
69 | 6898 |
" |
6899 |
|c1 c2| |
|
1 | 6900 |
c1 := #(1 16 7 9). |
6901 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
6902 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 6903 |
c1 printNL. |
6904 |
c2 printNL |
|
69 | 6905 |
" |
2069 | 6906 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6907 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 6908 |
! |
6909 |
||
602 | 6910 |
sortWith:aCollection |
2069 | 6911 |
"sort the receiver collection inplace, using '<' to compare elements. |
6912 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6913 |
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
|
6914 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6915 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6916 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6917 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6918 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 6919 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6920 |
self quickSortWith:aCollection |
424 | 6921 |
! |
6922 |
||
9072 | 6923 |
sortedBy: aBlock |
6924 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
|
6925 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
6926 |
||
6927 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
6928 |
! |
|
6929 |
||
602 | 6930 |
topologicalSort:sortBlock |
6931 |
"sort the collection inplace using a sloooow sort algorithm. |
|
6932 |
This algorithm has O-square runtime behavior and should be used only |
|
6933 |
in special situations. |
|
6934 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
6935 |
a>c (for example, to sort classes by inheritance) |
|
6936 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6937 |
In all other situations, use #sort; which implements the quicksort algorithm. |
10812 | 6938 |
|
6939 |
If there are cycles, this algorithm may loop endless!! |
|
6940 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
77 | 6941 |
" |
602 | 6942 |
|
6943 |
|smallestIndex "{ Class: SmallInteger }" |
|
6944 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6945 |
index2 "{ Class: SmallInteger }" |
602 | 6946 |
smallest thisOne| |
6947 |
||
6948 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
6949 |
||
6950 |
end := self size. |
|
862 | 6951 |
1 to:end-1 do:[:index | |
10812 | 6952 |
smallest := self at:index. |
6953 |
smallestIndex := index. |
|
6954 |
||
6955 |
index2 := index + 1. |
|
6956 |
[ |
|
6957 |
thisOne := self at:index2. |
|
6958 |
(index2 ~~ smallestIndex |
|
6959 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
6960 |
smallestIndex := index2. |
|
6961 |
smallest := thisOne. |
|
6962 |
index2 := index + 1. |
|
6963 |
] ifFalse:[ |
|
6964 |
index2 := index2 + 1. |
|
6965 |
]. |
|
6966 |
] doWhile:[index2 <= end]. |
|
862 | 6967 |
|
6968 |
"/ (index + 1) to:end do:[:index2 | |
|
6969 |
"/ thisOne := self at:index2. |
|
6970 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
6971 |
"/ smallestIndex := index2. |
|
6972 |
"/ smallest := thisOne |
|
6973 |
"/ ]. |
|
6974 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6975 |
|
10812 | 6976 |
(smallestIndex ~~ index) ifTrue:[ |
6977 |
thisOne := self at:index. |
|
6978 |
self at:index put:smallest. |
|
6979 |
self at:smallestIndex put:thisOne |
|
6980 |
]. |
|
1 | 6981 |
] |
95 | 6982 |
|
77 | 6983 |
" |
8874 | 6984 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
6985 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
10812 | 6986 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
8874 | 6987 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
6988 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 6989 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6990 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6991 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 6992 |
! ! |
602 | 6993 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6994 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6995 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6996 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6997 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6998 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 6999 |
however, having a common protocol with indexed collections |
7000 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7001 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7002 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7003 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7004 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7005 |
" |
8874 | 7006 |
#(1 2 3) includesKey:4 |
7007 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7008 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7009 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7010 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7011 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7012 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7013 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7014 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7015 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7016 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7017 |
|
2069 | 7018 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7019 |
|
7020 |
version |
|
10812 | 7021 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.249 2007-12-06 21:46:33 stefan Exp $' |
628 | 7022 |
! ! |
6820 | 7023 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7024 |
SequenceableCollection initialize! |