author | Claus Gittinger <cg@exept.de> |
Mon, 04 Jun 2007 21:32:08 +0200 | |
changeset 10590 | 6f1ae91a406e |
parent 10479 | fcbd291cce60 |
child 10749 | 967a9e8adfc0 |
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 |
|
6183 | 611 |
"#(1 2 3 4) permutationsDo: [:each | Transcript cr; show: each printString]" |
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 | |
619 |
self swap: anInteger with: i. |
|
620 |
self permutationsStartingAt: anInteger + 1 do: aBlock. |
|
621 |
self swap: anInteger with: i |
|
6183 | 622 |
] |
623 |
||
624 |
"Modified: / 13.11.2001 / 13:48:49 / cg" |
|
4414 | 625 |
! ! |
626 |
||
7261 | 627 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 628 |
|
629 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
630 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
631 |
! ! |
|
632 |
||
1 | 633 |
!SequenceableCollection methodsFor:'accessing'! |
634 |
||
1366 | 635 |
after:anObject |
636 |
"return the element, after anObject. |
|
5231 | 637 |
If anObject is not in the receiver, report an error; |
638 |
if anObject is the last in the receiver, return nil. |
|
1366 | 639 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
640 |
||
641 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
642 |
||
643 |
" |
|
8874 | 644 |
#(4 3 2 1) asOrderedCollection after:3. |
645 |
#(4 3 2 1) asOrderedCollection after:5 |
|
646 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 647 |
" |
648 |
||
649 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
650 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
651 |
! |
|
652 |
||
653 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 654 |
"return the element, after anObject. |
5231 | 655 |
If anObject is the last in the receiver, return nil. |
656 |
If there is no such element (anObject is not found), |
|
1366 | 657 |
return the value from exceptionBlock." |
658 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
659 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
660 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
661 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
662 |
idx ~~ 0 ifTrue:[ |
8874 | 663 |
idx == self size ifTrue:[^ nil]. |
664 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
665 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
666 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
667 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
668 |
" |
8874 | 669 |
#(4 3 2 1) after:3 ifAbsent:nil. |
670 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
671 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
672 |
(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
|
673 |
" |
1366 | 674 |
! |
675 |
||
6967 | 676 |
anElement |
8874 | 677 |
"return any element from the collection, |
6967 | 678 |
report an error if there is none" |
679 |
||
680 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
681 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
682 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
683 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
684 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
685 |
" |
6967 | 686 |
! |
687 |
||
32 | 688 |
at:index ifAbsent:exceptionBlock |
8874 | 689 |
"return the element at index if valid. |
690 |
If the index is invalid, return the result of evaluating |
|
302 | 691 |
the exceptionblock. |
8874 | 692 |
NOTICE: |
693 |
in ST-80, this message is only defined for Dictionaries, |
|
694 |
however, having a common protocol with indexed collections |
|
695 |
often simplifies things." |
|
32 | 696 |
|
8579 | 697 |
(index between:1 and:self size) ifFalse:[ |
8874 | 698 |
^ exceptionBlock value |
32 | 699 |
]. |
700 |
^ self at:index |
|
701 |
||
95 | 702 |
" |
703 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 704 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
705 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 706 |
|
95 | 707 |
(Dictionary with:(#foo -> #bar) |
8874 | 708 |
with:(#frob -> #baz)) |
709 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 710 |
" |
362 | 711 |
! |
712 |
||
1366 | 713 |
before:anObject |
714 |
"return the element before the argument, anObject. |
|
5231 | 715 |
If anObject is not in the receiver, report an error; |
716 |
if anObject is the first in the receiver, return nil. |
|
1366 | 717 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
718 |
||
719 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
720 |
||
721 |
" |
|
8874 | 722 |
#(4 3 2 1) asOrderedCollection before:3. |
723 |
#(4 3 2 1) asOrderedCollection before:4 |
|
724 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 725 |
" |
726 |
||
727 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
728 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
729 |
! |
|
730 |
||
731 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 732 |
"return the element, before anObject. |
5231 | 733 |
If anObject is the first in the receiver, return nil. |
734 |
If there is no such element (anObject is not found), |
|
1366 | 735 |
return the value from exceptionBlock." |
736 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
737 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
738 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
739 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
740 |
idx ~~ 0 ifTrue:[ |
8874 | 741 |
idx == 1 ifTrue:[^ nil]. |
742 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
743 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
744 |
^ exceptionBlock value |
1366 | 745 |
|
5231 | 746 |
" |
8874 | 747 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
748 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
749 |
#(4 3 2 1) asOrderedCollection before:5. |
|
750 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
751 |
#(4 3 2 1) asOrderedCollection before:4. |
|
752 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
753 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 754 |
" |
755 |
||
1366 | 756 |
! |
757 |
||
602 | 758 |
first |
8874 | 759 |
"return the first element; |
5231 | 760 |
report an error, if the collection is empty." |
602 | 761 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
762 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 763 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
764 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
765 |
" |
8874 | 766 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
767 |
returns: firstElement <object> |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
770 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 771 |
! |
772 |
||
5887 | 773 |
first:n |
774 |
"return the n first elements of the collection. |
|
775 |
Raises an error if there are not enough elements in the receiver." |
|
776 |
||
777 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
778 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
779 |
||
780 |
^ self copyFirst:n |
|
781 |
||
782 |
" |
|
8874 | 783 |
#(1 2 3 4 5) first:3 |
784 |
#(1 2 3 4 5) first:6 |
|
785 |
#(1 2 3 4 5) asSet first:3 |
|
786 |
'hello world' first:5 |
|
5887 | 787 |
" |
788 |
! |
|
789 |
||
362 | 790 |
keyAtEqualValue:value |
791 |
"return the index of a value. |
|
792 |
This is normally not used (use indexOf:), but makes the |
|
793 |
protocol more compatible with dictionaries." |
|
794 |
||
795 |
^ self indexOf:value |
|
796 |
! |
|
797 |
||
798 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
799 |
"return the index of a value. |
|
800 |
This is normally not used (use indexOf:), but makes the |
|
801 |
protocol more compatible with dictionaries." |
|
802 |
||
803 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 804 |
! |
805 |
||
806 |
keyAtValue:value |
|
807 |
"return the index of a value. |
|
808 |
This is normally not used (use indexOf:), but makes the |
|
809 |
protocol more compatible with dictionaries." |
|
810 |
||
811 |
^ self identityIndexOf:value |
|
812 |
! |
|
813 |
||
814 |
keyAtValue:value ifAbsent:exceptionBlock |
|
815 |
"return the index of a value. |
|
816 |
This is normally not used (use indexOf:), but makes the |
|
817 |
protocol more compatible with dictionaries." |
|
818 |
||
819 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
820 |
! |
|
821 |
||
822 |
last |
|
5231 | 823 |
"return the last element; |
824 |
report an error, if the collection is empty." |
|
602 | 825 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
826 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
827 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
828 |
(sz := self size) == 0 ifFalse:[ |
8874 | 829 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
830 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
831 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
832 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
833 |
|
5231 | 834 |
" |
835 |
#(1 2 3 4 5) last |
|
836 |
#() last |
|
837 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
838 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
839 |
|
5887 | 840 |
last:n |
841 |
"return the n last elements of the collection. |
|
842 |
Raises an error if there are not enough elements in the receiver." |
|
843 |
||
844 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
845 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
846 |
||
847 |
^ self copyLast:n |
|
848 |
||
849 |
" |
|
8874 | 850 |
#(1 2 3 4 5) last:3 |
851 |
#(1 2 3 4 5) last:6 |
|
852 |
#(1 2 3 4 5) asSet last:3 |
|
853 |
'hello world' last:5 |
|
5887 | 854 |
" |
855 |
! |
|
856 |
||
4986 | 857 |
nth:n |
5231 | 858 |
"return the nth element; |
859 |
report an error, if the collections size is smaller than n." |
|
4986 | 860 |
|
861 |
^ self at:n |
|
862 |
||
863 |
" |
|
8874 | 864 |
args: |
4986 | 865 |
returns: nth element <object> |
866 |
" |
|
867 |
||
5231 | 868 |
" |
8874 | 869 |
#(10 20 30 40 50) nth:4 |
5231 | 870 |
#(10 20 30 40 50) nth:6 |
871 |
#() nth:1 |
|
872 |
" |
|
873 |
||
4986 | 874 |
! |
875 |
||
4855 | 876 |
second |
5231 | 877 |
"return the second element; |
878 |
report an error, if the collections size is smaller than 2." |
|
4855 | 879 |
|
880 |
^ self at:2 |
|
881 |
||
882 |
" |
|
8874 | 883 |
args: |
4855 | 884 |
returns: secondElement <object> |
885 |
" |
|
886 |
||
887 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
888 |
! |
|
889 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
890 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
891 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
892 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
893 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
894 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
895 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
896 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
897 |
self at:index2 put: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 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 900 |
! ! |
901 |
||
602 | 902 |
!SequenceableCollection methodsFor:'adding & removing'! |
903 |
||
904 |
add:anObject |
|
905 |
"append the argument, anObject to the collection. |
|
906 |
Return the argument, anObject. |
|
907 |
||
908 |
Notice, that this is modifies the receiver NOT a copy. |
|
909 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 910 |
due to the grow:-message, which is inefficient for fixed size |
602 | 911 |
collections (i.e. for Strings and Arrays it is not recommened)." |
912 |
||
913 |
|newSize| |
|
914 |
||
915 |
newSize := self size + 1. |
|
916 |
self grow:newSize. |
|
917 |
self at:newSize put:anObject. |
|
918 |
^ anObject |
|
919 |
||
920 |
" |
|
921 |
|a| |
|
922 |
a := #(1 2 3 4 5 6 7 8). |
|
923 |
a add:'hello'. |
|
924 |
a |
|
925 |
" |
|
926 |
" |
|
927 |
|c| |
|
928 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
929 |
c add:'hello'. |
|
930 |
c |
|
931 |
" |
|
932 |
! |
|
933 |
||
934 |
add:anElement beforeIndex:index |
|
935 |
"insert the first argument, anObject into the collection before slot index. |
|
936 |
Return the receiver (sigh - ST-80 compatibility). |
|
937 |
||
938 |
Notice, that this is modifies the receiver NOT a copy. |
|
939 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 940 |
due to the grow:-message, which is inefficient for fixed size |
602 | 941 |
collections (i.e. for Strings and Arrays it is not recommened)." |
942 |
||
943 |
|newSize| |
|
944 |
||
945 |
newSize := self size + 1. |
|
946 |
self grow:newSize. |
|
947 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
948 |
self at:index put:anElement |
|
949 |
||
950 |
" |
|
951 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
952 |
" |
|
202 | 953 |
! |
954 |
||
602 | 955 |
addFirst:anObject |
956 |
"prepend the argument, anObject to the collection. |
|
957 |
Return the argument, anObject. |
|
958 |
||
959 |
Notice, that this is modifies the receiver NOT a copy. |
|
960 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 961 |
due to the grow:-message, which is inefficient for fixed size |
602 | 962 |
collections (i.e. for Strings and Arrays it is not recommened)." |
963 |
||
964 |
|newSize| |
|
965 |
||
966 |
newSize := self size + 1. |
|
967 |
self grow:newSize. |
|
968 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
969 |
self at:1 put:anObject. |
|
970 |
^ anObject |
|
971 |
||
972 |
" |
|
8874 | 973 |
|a| |
974 |
a:= #(1 2 3 4 5 6 7 8). |
|
975 |
a addFirst:'hello'. |
|
976 |
a |
|
602 | 977 |
" |
978 |
" |
|
979 |
|c| |
|
980 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
981 |
c addFirst:'hello'. |
|
982 |
c |
|
983 |
" |
|
69 | 984 |
! |
985 |
||
602 | 986 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
987 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
988 |
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
|
989 |
Use equality compare (=) to search for an occurrence. |
602 | 990 |
|
991 |
Notice, that this is modifies the receiver NOT a copy. |
|
992 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 993 |
due to the grow:-message, which is inefficient for fixed size |
602 | 994 |
collections (i.e. for Strings and Arrays it is not recommened)." |
995 |
||
8874 | 996 |
|any |
602 | 997 |
dstIndex "{ Class: SmallInteger }" |
998 |
sz "{ Class: SmallInteger }"| |
|
999 |
||
1000 |
dstIndex := 1. |
|
1001 |
any := false. |
|
1002 |
sz := self size. |
|
1003 |
1 to:sz do:[:srcIndex | |
|
8874 | 1004 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1005 |
any := true |
|
1006 |
] ifFalse:[ |
|
1007 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1008 |
self at:dstIndex put:(self at:srcIndex) |
|
1009 |
]. |
|
1010 |
dstIndex := dstIndex + 1 |
|
1011 |
] |
|
602 | 1012 |
]. |
1013 |
any ifTrue:[ |
|
8874 | 1014 |
self grow:dstIndex - 1. |
1015 |
^ anElement |
|
602 | 1016 |
]. |
1017 |
^ aBlock value |
|
1018 |
||
1019 |
" |
|
1422 | 1020 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1021 |
#(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
|
1022 |
#(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
|
1023 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1024 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1025 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1026 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1027 |
! |
1028 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1029 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1030 |
"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
|
1031 |
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
|
1032 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1033 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1034 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1035 |
"/ 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
|
1036 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1037 |
|runIndex removed element| |
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 |
removed := self species new. |
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 := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1042 |
[runIndex <= self size] whileTrue:[ |
8874 | 1043 |
element := self at:runIndex. |
1044 |
(aBlock value:element) ifTrue:[ |
|
1045 |
removed add:element. |
|
1046 |
self removeAtIndex:runIndex |
|
1047 |
] ifFalse:[ |
|
1048 |
runIndex := runIndex + 1 |
|
1049 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1050 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1051 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1052 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1053 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1054 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1055 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1056 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1057 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1058 |
Transcript showCR:coll |
|
1369
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 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1061 |
"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
|
1062 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1063 |
|
602 | 1064 |
removeAtIndex:anIndex |
1065 |
"remove the element stored at anIndex. Return the removed object. |
|
1066 |
||
1067 |
Notice, that this is modifies the receiver NOT a copy. |
|
1068 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1069 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1070 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1071 |
||
1072 |
|element| |
|
1073 |
||
1074 |
element := self at:anIndex. |
|
1075 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1076 |
^ element |
|
1077 |
||
1078 |
" |
|
1079 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1080 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1081 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1082 |
" |
|
13 | 1083 |
! |
1084 |
||
602 | 1085 |
removeFirst |
1086 |
"remove the first element of the receiver and return it. |
|
1087 |
||
1088 |
Notice, that this is modifies the receiver NOT a copy. |
|
1089 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1090 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1091 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1092 |
||
1093 |
^ self removeAtIndex:1 |
|
1094 |
||
1095 |
" |
|
1096 |
|a| |
|
1097 |
a := #(1 2 3 4 5 6). |
|
1098 |
a removeFirst. |
|
8874 | 1099 |
a |
602 | 1100 |
" |
13 | 1101 |
! |
1102 |
||
2294 | 1103 |
removeFromIndex:startIndex |
1104 |
"remove the elements stored at indexes from startIndex to the end. |
|
1105 |
Return the receiver. |
|
1106 |
Returning the receiver is a historic leftover - it may at one |
|
1107 |
time return a collection of the removed elements. |
|
1108 |
||
8874 | 1109 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1110 |
therefore any other users of the receiver will also see this change. |
1111 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1112 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1113 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1114 |
||
1115 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1116 |
||
1117 |
" |
|
8874 | 1118 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1119 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1120 |
" |
|
1121 |
||
1122 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1123 |
! |
|
1124 |
||
602 | 1125 |
removeFromIndex:startIndex toIndex:endIndex |
1126 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1127 |
Return the receiver. |
|
1128 |
Returning the receiver is a historic leftover - it may at one |
|
1129 |
time return a collection of the removed elements. |
|
1130 |
||
8874 | 1131 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1132 |
therefore any other users of the receiver will also see this change. |
1133 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1134 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1135 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1136 |
||
1137 |
|size newSize| |
|
1138 |
||
1139 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1140 |
self grow:(startIndex - 1) |
602 | 1141 |
] ifFalse:[ |
8874 | 1142 |
newSize := size - endIndex + startIndex - 1. |
1143 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1144 |
self grow:newSize |
|
602 | 1145 |
] |
1146 |
||
1147 |
"/ |newSize| |
|
1148 |
"/ |
|
1149 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1150 |
"/ newSize <= 0 ifTrue:[ |
|
1151 |
"/ self grow:0 |
|
1152 |
"/ ] ifFalse:[ |
|
1153 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1154 |
"/ self grow:newSize |
|
1155 |
"/ ] |
|
1156 |
||
1157 |
" |
|
8874 | 1158 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1159 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1160 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1161 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1162 |
" |
1163 |
! |
|
1164 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1165 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1166 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1167 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1168 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1169 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1170 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1171 |
Also note, that it may be a slow operation for some collections, |
8874 | 1172 |
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
|
1173 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1174 |
|
2363 | 1175 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1176 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1177 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1178 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1179 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1180 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1181 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1182 |
1 to:sz do:[:srcIndex | |
8874 | 1183 |
el := self at:srcIndex. |
1184 |
(anElement == el) ifTrue:[ |
|
1185 |
any := true |
|
1186 |
] ifFalse:[ |
|
1187 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1188 |
self at:dstIndex put:el |
|
1189 |
]. |
|
1190 |
dstIndex := dstIndex + 1 |
|
1191 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1192 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1193 |
any ifTrue:[ |
8874 | 1194 |
self grow:dstIndex - 1. |
1195 |
^ anElement |
|
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 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1198 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1199 |
" |
8874 | 1200 |
#(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
|
1201 |
#(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
|
1202 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1203 |
|
2363 | 1204 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1205 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1206 |
|
602 | 1207 |
removeIndex:index |
1208 |
"remove the argument stored at index. Return the receiver. |
|
1209 |
||
1210 |
Notice, that this is modifies the receiver NOT a copy. |
|
1211 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1212 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1213 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1214 |
||
1215 |
self removeFromIndex:index toIndex:index |
|
1216 |
||
1217 |
" |
|
1218 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1219 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1220 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1221 |
" |
|
77 | 1222 |
! |
1223 |
||
602 | 1224 |
removeLast |
1225 |
"remove the last element of the receiver and return it. |
|
1226 |
||
1227 |
Notice, that this is modifies the receiver NOT a copy. |
|
1228 |
Also note, that it may be a slow operation for some collections, |
|
1229 |
due to the grow:-message, which is inefficient for fixed size |
|
1230 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1231 |
||
8874 | 1232 |
^ self removeAtIndex:(self size) |
602 | 1233 |
|
1234 |
" |
|
1235 |
|a| |
|
1236 |
a := #(1 2 3 4 5 6). |
|
1237 |
a removeLast. |
|
8874 | 1238 |
a |
602 | 1239 |
" |
1240 |
! ! |
|
1241 |
||
1242 |
!SequenceableCollection methodsFor:'comparing'! |
|
1243 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1244 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1245 |
"compare two sequenceable collections" |
8874 | 1246 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1247 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1248 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1249 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1250 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1251 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1252 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1253 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1254 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1255 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1256 |
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
|
1257 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1258 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1259 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1260 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1261 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1262 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1263 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1264 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1265 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1266 |
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
|
1267 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1268 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1269 |
(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
|
1270 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1271 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1272 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1273 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1274 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1275 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1276 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1277 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1278 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1279 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1280 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1281 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1282 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1283 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1284 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1285 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1286 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1287 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1288 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1289 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1290 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1291 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1292 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1293 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1294 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1295 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1296 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1297 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1298 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1299 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1300 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1301 |
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
|
1302 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1303 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1304 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1305 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1306 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1307 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1308 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1309 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1310 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
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 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1313 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1314 |
"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
|
1315 |
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
|
1316 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1317 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1318 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1319 |
|
602 | 1320 |
= aCollection |
1321 |
"return true if the receiver and aCollection represent collections |
|
1322 |
with equal contents." |
|
1323 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1324 |
| stop "{ Class: SmallInteger }" | |
602 | 1325 |
|
1326 |
(aCollection == self) ifTrue:[^true]. |
|
1327 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1328 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1329 |
|
1330 |
stop := self size. |
|
1331 |
stop == (aCollection size) ifFalse:[^false]. |
|
1332 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1333 |
1 to:stop do:[:index| |
9366 | 1334 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1335 |
]. |
1336 |
^ true |
|
1337 |
||
1338 |
" |
|
8874 | 1339 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1340 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1341 |
#($1 $2 $3 $4 $5) = '12345' |
|
1342 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1343 |
" |
1344 |
! |
|
1345 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1346 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1347 |
"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
|
1348 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1349 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1350 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1351 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1352 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1353 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1354 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1355 |
"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
|
1356 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1357 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1358 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1359 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1360 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1361 |
|
8874 | 1362 |
commonPrefixWith:aCollection |
6507 | 1363 |
"return the common prefix of myself and the argument, aCollection. |
1364 |
If there is none, an empty collection is returned." |
|
1365 |
||
1366 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1367 |
||
1368 |
" |
|
8874 | 1369 |
'hello' commonPrefixWith:'hello' |
1370 |
'hello' commonPrefixWith:'hElLo' |
|
1371 |
'hello' commonPrefixWith:'hello world' |
|
1372 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1373 |
||
1374 |
'hello world' commonPrefixWith:'hello' |
|
1375 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1376 |
||
1377 |
'abcd' commonPrefixWith:'bcde' |
|
1378 |
||
1379 |
'abcd' commonPrefixWith:'abab' |
|
1380 |
'abcd' commonPrefixWith:'ab' |
|
1381 |
'abcd' commonPrefixWith:'ababab' |
|
1382 |
'abcd' commonPrefixWith:'abcdef' |
|
1383 |
||
1384 |
'abab' commonPrefixWith:'abcd' |
|
1385 |
'ab' commonPrefixWith:'abcd' |
|
1386 |
'ababab' commonPrefixWith:'abcd' |
|
1387 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1388 |
" |
1389 |
! |
|
1390 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1391 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1392 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1393 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1394 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1395 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1396 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1397 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1398 |
1 to:matchLen do:[:idx | |
8874 | 1399 |
|elHere elThere same| |
1400 |
||
1401 |
elHere := self at:idx. |
|
1402 |
elThere := aCollection at:idx. |
|
1403 |
||
1404 |
ignoreCase ifTrue:[ |
|
1405 |
same := elHere sameAs:elThere |
|
1406 |
] ifFalse:[ |
|
1407 |
same := elHere = elThere |
|
1408 |
]. |
|
1409 |
same ifFalse:[ |
|
1410 |
^ self copyTo:(idx - 1). |
|
1411 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1412 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1413 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1414 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1415 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1416 |
" |
8874 | 1417 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1418 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1419 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1420 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1421 |
||
1422 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1423 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1424 |
||
1425 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1426 |
||
1427 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1428 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1429 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1430 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1431 |
||
1432 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1433 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1434 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1435 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1436 |
" |
|
1437 |
! |
|
1438 |
||
1439 |
commonSuffixWith:aCollection |
|
6507 | 1440 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1441 |
If there is none, an empty collection is returned." |
|
1442 |
||
1443 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1444 |
||
1445 |
" |
|
8874 | 1446 |
'hello' commonSuffixWith:'hello' |
1447 |
'hello' commonSuffixWith:'hElLo' |
|
1448 |
'hello' commonSuffixWith:'hello world' |
|
1449 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1450 |
'hello2 world' commonSuffixWith:'hello world' |
|
1451 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1452 |
||
1453 |
'hello world' commonSuffixWith:'world' |
|
1454 |
'hello WoRlD' commonSuffixWith:'world' |
|
1455 |
||
1456 |
'dcba' commonSuffixWith:'edcb' |
|
1457 |
||
1458 |
'dcba' commonSuffixWith:'baba' |
|
1459 |
'dcba' commonSuffixWith:'ba' |
|
1460 |
'dcba' commonSuffixWith:'bababa' |
|
1461 |
'dcba' commonSuffixWith:'fdcba' |
|
1462 |
||
1463 |
'baba' commonSuffixWith:'dcba' |
|
1464 |
'ba' commonSuffixWith:'dcba' |
|
1465 |
'bababa' commonSuffixWith:'dcba' |
|
1466 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1467 |
" |
1468 |
! |
|
1469 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1470 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1471 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1472 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1473 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1474 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1475 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1476 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1477 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1478 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1479 |
1 to:matchLen do:[:idx | |
8874 | 1480 |
|elHere elThere same| |
1481 |
||
1482 |
elHere := self at:(l1 - idx + 1). |
|
1483 |
elThere := aCollection at:(l2 - idx + 1). |
|
1484 |
||
1485 |
ignoreCase ifTrue:[ |
|
1486 |
same := elHere sameAs:elThere |
|
1487 |
] ifFalse:[ |
|
1488 |
same := elHere = elThere |
|
1489 |
]. |
|
1490 |
same ifFalse:[ |
|
1491 |
^ self copyFrom:(l1 - idx + 2). |
|
1492 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1493 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1494 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1495 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1496 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1497 |
" |
8874 | 1498 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1499 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1500 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1501 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1502 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1503 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1504 |
||
1505 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1506 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1507 |
||
1508 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1509 |
||
1510 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1511 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1512 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1513 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1514 |
||
1515 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1516 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1517 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1518 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1519 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1520 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1521 |
|
5813 | 1522 |
deepSameContentsAs:aCollection |
1523 |
"return true, if the receiver and the arg have the same contents |
|
1524 |
in both the named instance vars and any indexed instVars. |
|
1525 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1526 |
||
1527 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1528 |
unused space" |
|
1529 |
||
1530 |
|index "{ Class: SmallInteger }" |
|
1531 |
stop "{ Class: SmallInteger }" | |
|
1532 |
||
1533 |
(aCollection == self) ifTrue:[^true]. |
|
1534 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1535 |
||
1536 |
stop := self size. |
|
1537 |
stop == (aCollection size) ifFalse:[^false]. |
|
1538 |
||
1539 |
index := 1. |
|
1540 |
[index <= stop] whileTrue:[ |
|
8874 | 1541 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1542 |
index := index + 1 |
|
5813 | 1543 |
]. |
1544 |
^ true |
|
1545 |
||
1546 |
" |
|
8874 | 1547 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1548 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1549 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1550 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1551 |
" |
1552 |
! |
|
1553 |
||
602 | 1554 |
endsWith:aCollection |
1555 |
"return true, if the receivers last elements match those |
|
1556 |
of aCollection" |
|
1557 |
||
1558 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1559 |
index2 "{ Class: SmallInteger }" |
1560 |
stop "{ Class: SmallInteger }" |
|
602 | 1561 |
sz "{ Class: SmallInteger }"| |
1562 |
||
1563 |
(aCollection == self) ifTrue:[^ true]. |
|
1564 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1565 |
||
1566 |
stop := aCollection size. |
|
77 | 1567 |
sz := self size. |
602 | 1568 |
stop > sz ifTrue:[^false]. |
1569 |
||
1570 |
index1 := sz. |
|
1571 |
index2 := stop. |
|
1572 |
[index2 > 0] whileTrue:[ |
|
8874 | 1573 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1574 |
index1 := index1 - 1. |
|
1575 |
index2 := index2 - 1 |
|
77 | 1576 |
]. |
602 | 1577 |
^ true |
1578 |
||
1579 |
" |
|
1580 |
'abcde' endsWith:#($d $e) |
|
8874 | 1581 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1582 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1583 |
" |
|
1584 |
! |
|
1585 |
||
7278 | 1586 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1587 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1588 |
|
1589 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1590 |
||
1591 |
" |
|
1592 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1593 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1594 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1595 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1596 |
" |
1597 |
! |
|
1598 |
||
602 | 1599 |
hash |
1600 |
"return a hash key for the receiver" |
|
1601 |
||
1602 |
"this hash is stupid - but for larger collections, the hashing |
|
1603 |
time can become much bigger than the time lost in added probing. |
|
1604 |
Time will show ..." |
|
1605 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1606 |
|mySize h| |
3653 | 1607 |
|
1608 |
mySize := self size. |
|
1609 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1610 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1611 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1612 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1613 |
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
|
1614 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1615 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1616 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1617 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1618 |
"/ 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
|
1619 |
"/ ^ ((((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
|
1620 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1621 |
" |
8874 | 1622 |
#(1 2 3 4 5 6) hash |
1623 |
(1 to:6) hash |
|
1624 |
||
1625 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1626 |
#(1 2 3 4 5) hash |
|
602 | 1627 |
" |
3653 | 1628 |
|
1629 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1630 |
! |
1631 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1632 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1633 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1634 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1635 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1636 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1637 |
^ self |
8874 | 1638 |
sameContentsAs:aCollection |
1639 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1640 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1641 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1642 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1643 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1644 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1645 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1646 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
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 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1649 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1650 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1651 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1652 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1653 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1654 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1655 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1656 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1657 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1658 |
" |
8874 | 1659 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1660 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1661 |
#($1 $2 $3 $4 $5) = '12345' |
|
1662 |
||
1663 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1664 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1665 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1666 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1667 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1668 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1669 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1670 |
|
6118 | 1671 |
isSameSequenceAs: otherCollection |
8874 | 1672 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1673 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1674 |
" |
|
1675 |
||
1676 |
| size | |
|
1677 |
||
1678 |
(size := self size) = otherCollection size ifFalse: [^false]. |
|
1679 |
||
1680 |
1 to: size do:[:index | |
|
8874 | 1681 |
(self at: index) = (otherCollection at: index) ifFalse: [^false] |
6118 | 1682 |
]. |
1683 |
^ true |
|
1684 |
! |
|
1685 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1686 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1687 |
"return true if the receiver and aCollection represent collections |
8874 | 1688 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1689 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1690 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1691 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1692 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1693 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1694 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1695 |
stop "{ Class: SmallInteger }" | |
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 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1698 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1699 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1700 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1701 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1702 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1703 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1704 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1705 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1706 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1707 |
[index <= stop] whileTrue:[ |
8874 | 1708 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1709 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1710 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1711 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1712 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1713 |
" |
8874 | 1714 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
1715 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1716 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1717 |
#(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
|
1718 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1719 |
#('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
|
1720 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1721 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1722 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1723 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1724 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1725 |
|
602 | 1726 |
startsWith:aCollection |
1727 |
"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
|
1728 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1729 |
If the argument is empty, true is returned." |
602 | 1730 |
|
1731 |
|index "{ Class: SmallInteger }" |
|
1732 |
stop "{ Class: SmallInteger }" | |
|
1733 |
||
1734 |
(aCollection == self) ifTrue:[^true]. |
|
1735 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1736 |
||
1737 |
stop := aCollection size. |
|
1738 |
stop > self size ifTrue:[^false]. |
|
1739 |
||
1740 |
index := 1. |
|
1741 |
[index <= stop] whileTrue:[ |
|
8874 | 1742 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1743 |
index := index + 1 |
|
602 | 1744 |
]. |
1745 |
^ true |
|
1746 |
||
1747 |
" |
|
1748 |
'abcde' startsWith:#($a $b $c) |
|
1749 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1750 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1751 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 1752 |
" |
7279 | 1753 |
! |
1754 |
||
1755 |
startsWithAnyOf:aCollectionOfCollections |
|
1756 |
"return true, if the receiver starts with any in aCollection" |
|
1757 |
||
1758 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
1759 |
||
1760 |
" |
|
1761 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
1762 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 1763 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
1764 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 1765 |
" |
13 | 1766 |
! ! |
1767 |
||
260 | 1768 |
!SequenceableCollection methodsFor:'converting'! |
1769 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1770 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 1771 |
"return a collection containing the subcollections (separated by anElement) |
1772 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1773 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1774 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1775 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1776 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1777 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1778 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1779 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1780 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1781 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1782 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1783 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1784 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1785 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1786 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1787 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1788 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1789 |
1 to:numberOfCols do:[:lineNr | |
8874 | 1790 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
1791 |
stopIndex == 0 ifTrue:[ |
|
1792 |
stopIndex := self size |
|
1793 |
] ifFalse: [ |
|
1794 |
stopIndex := stopIndex - 1. |
|
1795 |
]. |
|
1796 |
||
1797 |
(stopIndex < startIndex) ifTrue: [ |
|
1798 |
cols add:(myClass new:0) |
|
1799 |
] ifFalse: [ |
|
1800 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
1801 |
]. |
|
1802 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1803 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1804 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1805 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1806 |
" |
8874 | 1807 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1808 |
'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
|
1809 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1810 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1811 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1812 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1813 |
|
9234 | 1814 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
1815 |
"evaluate aBlock for each subcollection generated by separating elements |
|
1816 |
of the receiver by anElement. |
|
1817 |
If anElement occurs multiple times in a row, |
|
1818 |
the block will be invoked with empty collections as argument. |
|
1819 |
This algorithm uses equality-compare to detect the element." |
|
1820 |
||
1821 |
|subCollection |
|
1822 |
endIndex "{ Class:SmallInteger }" |
|
1823 |
startIndex "{ Class:SmallInteger }" |
|
1824 |
stopIndex "{ Class:SmallInteger }" | |
|
1825 |
||
1826 |
startIndex := 0. |
|
1827 |
endIndex := self size. |
|
1828 |
||
1829 |
[startIndex <= endIndex] whileTrue:[ |
|
1830 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
1831 |
stopIndex == 0 ifTrue:[ |
|
1832 |
stopIndex := self size |
|
1833 |
] ifFalse: [ |
|
1834 |
stopIndex := stopIndex - 1. |
|
1835 |
]. |
|
1836 |
||
1837 |
(stopIndex < startIndex) ifTrue: [ |
|
1838 |
subCollection := self species new:0 |
|
1839 |
] ifFalse: [ |
|
1840 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
1841 |
]. |
|
1842 |
aBlock value:subCollection. |
|
1843 |
startIndex := stopIndex + 1 |
|
1844 |
]. |
|
1845 |
||
1846 |
" |
|
1847 |
'' |
|
1848 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1849 |
||
1850 |
'1 one' |
|
1851 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1852 |
||
1853 |
'1 one:2 two:3 three:4 four:5 five' |
|
1854 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1855 |
||
1856 |
'a::b' |
|
1857 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1858 |
||
1859 |
':' |
|
1860 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1861 |
||
1862 |
':a' |
|
1863 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1864 |
||
1865 |
'a:' |
|
1866 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
1867 |
" |
|
1868 |
! |
|
1869 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1870 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 1871 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
1872 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1873 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1874 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1875 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1876 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1877 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1878 |
stopIndex "{ Class:SmallInteger }" | |
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 := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1881 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1882 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1883 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1884 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1885 |
[done] whileFalse:[ |
8874 | 1886 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
1887 |
stopIndex == 0 ifTrue:[ |
|
1888 |
stopIndex := self size. |
|
1889 |
done := true. |
|
1890 |
] ifFalse: [ |
|
1891 |
stopIndex := stopIndex - 1. |
|
1892 |
]. |
|
1893 |
||
1894 |
(stopIndex < startIndex) ifTrue: [ |
|
1895 |
items add:(myClass new:0) |
|
1896 |
] ifFalse: [ |
|
1897 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
1898 |
]. |
|
1899 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1900 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1901 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1902 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1903 |
" |
8874 | 1904 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
1905 |
#(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
|
1906 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1907 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1908 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1909 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 1910 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1911 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1912 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1913 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1914 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1915 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1916 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1917 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1918 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 1919 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
1920 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
1921 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
1922 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
1923 |
#(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
|
1924 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1925 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1926 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1927 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 1928 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1929 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1930 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1931 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1932 |
|words |
8874 | 1933 |
start "{ Class:SmallInteger }" |
1934 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1935 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1936 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1937 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1938 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1939 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1940 |
[start <= mySize] whileTrue:[ |
8874 | 1941 |
"skip multiple separators" |
1942 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
1943 |
start := start + 1 . |
|
1944 |
start > mySize ifTrue:[ |
|
1945 |
^ words |
|
1946 |
]. |
|
1947 |
]. |
|
1948 |
||
1949 |
stop := self findFirst:aBlock startingAt:start. |
|
1950 |
stop == 0 ifTrue:[ |
|
1951 |
words add:(self copyFrom:start to:mySize). |
|
1952 |
^ words |
|
1953 |
]. |
|
1954 |
words add:(self copyFrom:start to:(stop - 1)). |
|
1955 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1956 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1957 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1958 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1959 |
" |
8874 | 1960 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
1961 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1962 |
#(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
|
1963 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1964 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1965 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1966 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1967 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1968 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1969 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1970 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1971 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
1972 |
|
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 |
|
1413 | 1975 |
asStringCollection |
1976 |
"return a new string collection containing the elements; |
|
1977 |
these ought to be strings. (i.e. String or Text instances)" |
|
1978 |
||
1979 |
|newColl sz| |
|
1980 |
||
1981 |
sz := self size. |
|
9162 | 1982 |
newColl := StringCollection withSize:sz. |
1413 | 1983 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
1984 |
^ newColl |
|
1985 |
||
1986 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
1987 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
1988 |
! |
|
1989 |
||
1188 | 1990 |
asStringWith:sepChar |
8874 | 1991 |
"return a string generated by concatenating my elements |
1188 | 1992 |
(which must be strings or nil) and embedding sepChar characters in between. |
1993 |
Nil entries and empty strings are counted as empty lines." |
|
1994 |
||
8874 | 1995 |
^ self |
1996 |
from:1 to:(self size) |
|
1997 |
asStringWith:sepChar |
|
1998 |
compressTabs:false |
|
1999 |
final:nil |
|
1413 | 2000 |
|
2001 |
" |
|
2002 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
2003 |
" |
|
2004 |
||
2005 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 2006 |
! |
2007 |
||
2008 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2009 |
"return part of myself as a string with embedded sepCharacters. |
|
2010 |
My elements must be strings or nil; nil entries and empty strings are |
|
2011 |
taken as empty lines." |
|
2012 |
||
8874 | 2013 |
^ self |
2014 |
from:firstLine to:lastLine |
|
2015 |
asStringWith:sepCharacter |
|
2016 |
compressTabs:false |
|
2017 |
final:nil |
|
1188 | 2018 |
" |
2019 |
creating entries for searchpath: |
|
2020 |
||
8874 | 2021 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2022 |
||
2023 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2024 |
" |
2025 |
||
2026 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2027 |
! |
|
2028 |
||
260 | 2029 |
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
|
2030 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2031 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2032 |
taken as empty lines. |
2033 |
If the argument compressTabs is true, leading spaces are converted |
|
2034 |
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
|
2035 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2036 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2037 |
^ self |
8874 | 2038 |
from:firstLine to:lastLine |
2039 |
asStringWith:sepCharacter |
|
2040 |
compressTabs:compressTabs |
|
2041 |
final:endCharacter |
|
2042 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2043 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2044 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2045 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2046 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2047 |
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
|
2048 |
"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
|
2049 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2050 |
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
|
2051 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2052 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2053 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2054 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2055 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2056 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2057 |
any emphasis. If false, a plain string is returned. |
260 | 2058 |
This method is tuned for big collections, in not creating many |
263 | 2059 |
intermediate strings (has linear runtime). For very small collections |
2060 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2061 |
|
5575 | 2062 |
^ self |
8874 | 2063 |
from:firstLine to:lastLine |
2064 |
asStringWith:sepCharacter |
|
2065 |
compressTabs:compressTabs |
|
2066 |
final:endCharacter |
|
2067 |
withEmphasis:withEmphasis |
|
5575 | 2068 |
! |
2069 |
||
2070 |
asStringWithCRs |
|
8874 | 2071 |
"return a string generated by concatenating my elements |
5575 | 2072 |
(which must be strings or nil) and embedding cr characters in between. |
2073 |
Nil entries and empty strings are counted as empty lines." |
|
2074 |
||
2075 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2076 |
||
2077 |
" |
|
8874 | 2078 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2079 |
|
2080 |
(OrderedCollection new |
|
8874 | 2081 |
add:'hello'; |
2082 |
add:'world'; |
|
2083 |
add:'foo'; |
|
2084 |
add:('bar' asText allBold); |
|
2085 |
yourself) asStringWithCRs |
|
5575 | 2086 |
|
2087 |
Transcript showCR: |
|
8874 | 2088 |
(OrderedCollection new |
2089 |
add:'hello'; |
|
2090 |
add:'world'; |
|
2091 |
add:'foo'; |
|
2092 |
add:('bar' asText allBold); |
|
2093 |
yourself) asStringWithCRs |
|
5575 | 2094 |
" |
2095 |
||
2096 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2097 |
! |
|
2098 |
||
2099 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2100 |
"return a string generated by concatenating some of my elements |
5575 | 2101 |
(which must be strings or nil) and embedding cr characters in between. |
2102 |
Nil entries and empty strings are counted as empty lines." |
|
2103 |
||
2104 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2105 |
||
2106 |
" |
|
8874 | 2107 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2108 |
|
2109 |
" |
|
2110 |
||
2111 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2112 |
! |
|
2113 |
||
2114 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2115 |
"return part of myself as a string with embedded cr's. |
|
2116 |
My elements must be strings or nil. |
|
2117 |
If the argument compressTabs is true, leading spaces are converted |
|
2118 |
to tab-characters (8col tabs). |
|
2119 |
Nil entries and empty strings are taken as empty lines." |
|
2120 |
||
2121 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2122 |
! |
|
2123 |
||
2124 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2125 |
"return part of myself as a string with embedded cr's. |
|
2126 |
My elements must be strings or nil; nil entries and empty strings are |
|
2127 |
taken as empty lines. |
|
2128 |
If the argument compressTabs is true, leading spaces are converted |
|
2129 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2130 |
should be followed by a cr or not." |
|
2131 |
||
2132 |
^ self asStringWith:(Character cr) |
|
8874 | 2133 |
from:firstLine |
2134 |
to:lastLine |
|
2135 |
compressTabs:compressTabs |
|
2136 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2137 |
! |
2138 |
||
2139 |
decodeAsLiteralArray |
|
2140 |
"given a literalEncoding in the receiver, |
|
2141 |
create & return the corresponding object. |
|
2142 |
The inverse operation to #literalArrayEncoding." |
|
2143 |
||
9414 | 2144 |
|clsName cls| |
5575 | 2145 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2146 |
self size == 0 ifTrue:[ |
9358 | 2147 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2148 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2149 |
|
9414 | 2150 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2151 |
clsName isSymbol ifFalse:[ |
9358 | 2152 |
^ MissingClassInLiteralArrayErrorSignal |
2153 |
raiseRequestWith:clsName |
|
9414 | 2154 |
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
|
2155 |
]. |
9414 | 2156 |
cls := Smalltalk classNamed:clsName. |
5575 | 2157 |
cls isNil ifTrue:[ |
9414 | 2158 |
^ MissingClassInLiteralArrayErrorSignal |
2159 |
raiseRequestWith:clsName |
|
2160 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2161 |
]. |
9414 | 2162 |
|
5575 | 2163 |
^ cls decodeFromLiteralArray:self. |
2164 |
||
2165 |
" |
|
10476 | 2166 |
#(Array 10 20) decodeAsLiteralArray |
2167 |
#(10 20) literalArrayEncoding |
|
2168 |
" |
|
2169 |
||
2170 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2171 |
! |
2172 |
||
8874 | 2173 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2174 |
"return part of myself as a string with embedded sepCharacters. |
2175 |
My elements must be strings or nil; nil entries and empty strings are |
|
2176 |
taken as empty lines." |
|
2177 |
||
8874 | 2178 |
^ self |
2179 |
from:firstLine |
|
2180 |
to:lastLine |
|
2181 |
asStringWith:sepCharacter |
|
2182 |
compressTabs:false |
|
2183 |
final:nil |
|
5575 | 2184 |
" |
2185 |
creating entries for searchpath: |
|
2186 |
||
8874 | 2187 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2188 |
||
2189 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2190 |
||
2191 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2192 |
" |
2193 |
||
2194 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2195 |
! |
|
2196 |
||
2197 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2198 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2199 |
My elements must be strings or nil; nil entries and empty strings are |
|
2200 |
taken as empty lines. |
|
2201 |
If the argument compressTabs is true, leading spaces are converted |
|
2202 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2203 |
character (if non-nil)." |
|
2204 |
||
2205 |
^ self |
|
8874 | 2206 |
from:firstLine |
2207 |
to:lastLine |
|
2208 |
asStringWith:sepCharacter |
|
2209 |
compressTabs:compressTabs |
|
2210 |
final:endCharacter |
|
2211 |
withEmphasis:true |
|
5575 | 2212 |
|
2213 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2214 |
! |
|
2215 |
||
2216 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2217 |
"return part of myself as a string or text with embedded sepCharacters |
|
2218 |
and followup endCharacter. |
|
2219 |
My elements must be strings or nil; nil entries and empty strings are |
|
2220 |
taken as empty lines. |
|
2221 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2222 |
If the argument compressTabs is true, leading spaces are converted |
|
2223 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2224 |
character (if non-nil). |
|
2225 |
The withEmphais argument controls if the returned string should preserve |
|
2226 |
any emphasis. If false, a plain string is returned. |
|
2227 |
This method is tuned for big collections, in not creating many |
|
2228 |
intermediate strings (has linear runtime). For very small collections |
|
2229 |
and small strings, it may be faster to use the comma , operation." |
|
2230 |
||
293 | 2231 |
|idx1 "{ Class:SmallInteger }" |
2232 |
idx2 "{ Class:SmallInteger }" |
|
2233 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2234 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2235 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2236 |
newString lineString spaces idx nTabs |
8099 | 2237 |
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
|
2238 |
thisLen anyTab| |
260 | 2239 |
|
2240 |
" |
|
2241 |
first accumulate the size of the string, to avoid |
|
2242 |
countless reallocations. If tabs are compressed, |
|
2243 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2244 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2245 |
" |
8099 | 2246 |
needEmphasis := false. |
2247 |
maxBitsPerCharacter := 8. |
|
1015 | 2248 |
|
260 | 2249 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2250 |
sepCharacter isNil ifTrue:[ |
8874 | 2251 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2252 |
] ifFalse:[ |
8874 | 2253 |
sepCharacter isCharacter ifTrue:[ |
2254 |
sepCnt := 1 |
|
2255 |
] ifFalse:[ |
|
2256 |
sepCnt := sepCharacter size |
|
2257 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2258 |
]. |
260 | 2259 |
|
293 | 2260 |
idx1 := firstLine. |
2261 |
idx2 := lastLine. |
|
2262 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2263 |
lineString := self at:lineIndex. |
2264 |
||
2265 |
lineString notNil ifTrue:[ |
|
2266 |
withEmphasis ifTrue:[ |
|
2267 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2268 |
needEmphasis := true |
|
2269 |
]. |
|
2270 |
]. |
|
2271 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2272 |
totalLength := totalLength + lineString size |
|
2273 |
]. |
|
2274 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2275 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2276 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2277 |
|
8099 | 2278 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2279 |
maxBitsPerCharacter > 16 ifTrue:[ |
2280 |
stringClass := Unicode32String. |
|
2281 |
] ifFalse:[ |
|
2282 |
stringClass := Unicode16String. |
|
2283 |
] |
|
8099 | 2284 |
] ifFalse:[ |
8874 | 2285 |
stringClass := String. |
8099 | 2286 |
]. |
2287 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2288 |
endCharacter notNil ifTrue:[ |
8874 | 2289 |
endCharacter isCharacter ifTrue:[ |
2290 |
totalLength := totalLength + 1 |
|
2291 |
] ifFalse:[ |
|
2292 |
totalLength := totalLength + endCharacter size |
|
2293 |
]. |
|
260 | 2294 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2295 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2296 |
|
260 | 2297 |
spaces := ' '. |
1016 | 2298 |
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
|
2299 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2300 |
needEmphasis ifTrue:[ |
8874 | 2301 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2302 |
]. |
260 | 2303 |
|
2304 |
" |
|
2305 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2306 |
Be careful with runArrays: |
8874 | 2307 |
replacing individual elements is VERY expensive. |
2308 |
Therefore, create a new runArray from scratch. |
|
260 | 2309 |
" |
2310 |
pos := 1. |
|
293 | 2311 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2312 |
lineString := self at:lineIndex. |
2313 |
thisLen := lineString size. |
|
2314 |
thisLen ~~ 0 ifTrue:[ |
|
2315 |
withEmphasis ifFalse:[ |
|
2316 |
lineString := lineString string. |
|
2317 |
]. |
|
2318 |
||
2319 |
(anyTab := compressTabs) ifTrue:[ |
|
2320 |
" |
|
2321 |
mhmh: could use withTabs from String-class here, |
|
2322 |
but we should avoid creating too many temporary strings |
|
2323 |
(especially, since this method is typically used when converting |
|
2324 |
big texts such as when saving in the filebrowser ...). |
|
2325 |
Therefore, we convert tabs inline here doing a direct replace |
|
2326 |
in newString." |
|
2327 |
||
2328 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2329 |
nTabs := (idx-1) // 8. |
|
2330 |
anyTab := (nTabs > 0) |
|
2331 |
]. |
|
2332 |
anyTab ifTrue:[ |
|
2333 |
"any tabs" |
|
2334 |
||
2335 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2336 |
||
2337 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2338 |
newRuns notNil ifTrue:[ |
|
2339 |
newRuns add:nil withOccurrences:nTabs |
|
2340 |
]. |
|
2341 |
pos := pos + nTabs. |
|
2342 |
||
2343 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2344 |
newRuns notNil ifTrue:[ |
|
2345 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2346 |
idx to:lineString size do:[:pos | |
|
2347 |
newRuns add:(lineString emphasisAt:pos) |
|
2348 |
] |
|
2349 |
] ifFalse:[ |
|
2350 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2351 |
] |
|
2352 |
]. |
|
2353 |
pos := pos + thisLen - (nTabs * 8). |
|
2354 |
] ifFalse:[ |
|
2355 |
newString replaceFrom:pos with:lineString. |
|
2356 |
||
2357 |
newRuns notNil ifTrue:[ |
|
2358 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2359 |
newRuns addAll:(lineString emphasis) |
|
2360 |
] ifFalse:[ |
|
2361 |
newRuns add:nil withOccurrences:lineString size |
|
2362 |
] |
|
2363 |
]. |
|
2364 |
pos := pos + thisLen. |
|
2365 |
]. |
|
2366 |
]. |
|
2367 |
||
2368 |
lineIndex ~~ lastLine ifTrue:[ |
|
2369 |
c := sepCharacter |
|
2370 |
] ifFalse:[ |
|
2371 |
c := endCharacter |
|
2372 |
]. |
|
2373 |
||
2374 |
c notNil ifTrue:[ |
|
2375 |
c isCharacter ifTrue:[ |
|
2376 |
newString at:pos put:c. |
|
2377 |
newRuns notNil ifTrue:[ |
|
2378 |
newRuns add:nil. |
|
2379 |
]. |
|
2380 |
pos := pos + 1 |
|
2381 |
] ifFalse:[ |
|
2382 |
newString replaceFrom:pos with:c. |
|
2383 |
newRuns notNil ifTrue:[ |
|
2384 |
newRuns add:nil withOccurrences:c size |
|
2385 |
]. |
|
2386 |
pos := pos + c size |
|
2387 |
] |
|
2388 |
]. |
|
260 | 2389 |
]. |
2390 |
||
2391 |
" |
|
2392 |
in case of tab compression, the result has to be |
|
2393 |
cut to size ... sorry |
|
2394 |
" |
|
2395 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2396 |
newString := newString copyTo:(pos - 1) |
260 | 2397 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2398 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2399 |
newRuns notNil ifTrue:[ |
8874 | 2400 |
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
|
2401 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2402 |
|
260 | 2403 |
^ newString |
2404 |
||
2405 |
" |
|
2406 |
creating entries for searchpath: |
|
2407 |
||
8874 | 2408 |
#('foo' 'bar' 'baz' '/foo/bar') |
2409 |
asStringWith:$: |
|
2410 |
from:1 to:4 |
|
2411 |
compressTabs:false |
|
2412 |
final:nil |
|
260 | 2413 |
|
2414 |
with trailing colon: |
|
2415 |
||
8874 | 2416 |
#('foo' 'bar' 'baz' '/foo/bar') |
2417 |
asStringWith:$: |
|
2418 |
from:1 to:4 |
|
2419 |
compressTabs:false |
|
2420 |
final:$: |
|
260 | 2421 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2422 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2423 |
|
8874 | 2424 |
#('foo' 'bar' 'baz') |
2425 |
asStringWith:nil |
|
2426 |
from:1 to:3 |
|
2427 |
compressTabs:false |
|
2428 |
final:nil |
|
260 | 2429 |
|
2430 |
creating a string from a collection of lines: |
|
2431 |
||
8874 | 2432 |
#('foo' 'bar' 'baz') |
2433 |
asStringWith:(Character cr) |
|
2434 |
from:1 to:3 |
|
2435 |
compressTabs:false |
|
2436 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2437 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2438 |
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
|
2439 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2440 |
(Array |
8874 | 2441 |
with:'foo' |
2442 |
with:('bar' asText allBold) |
|
2443 |
with:'baz' |
|
2444 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2445 |
) |
|
2446 |
asStringWith:(Character cr) |
|
2447 |
from:1 to:4 |
|
2448 |
compressTabs:false |
|
2449 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2450 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2451 |
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
|
2452 |
|
8874 | 2453 |
#('foo' 'bar' 'baz') |
2454 |
asStringWith:'__' |
|
2455 |
from:1 to:3 |
|
2456 |
compressTabs:false |
|
2457 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2458 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2459 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2460 |
|
8874 | 2461 |
#('foo' 'bar' 'baz') |
2462 |
asStringWith:'__' |
|
2463 |
from:1 to:3 |
|
2464 |
compressTabs:false |
|
2465 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2466 |
|
260 | 2467 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2468 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2469 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2470 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
260 | 2471 |
! ! |
2472 |
||
7253 | 2473 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2474 |
||
8874 | 2475 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2476 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2477 |
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
|
2478 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2479 |
however, physically, no copy is made). |
9291 | 2480 |
Warning: |
2481 |
The slice SHARES the memory for the element-data with the original, |
|
2482 |
this means that any modifications in the original are visible in the slice |
|
2483 |
and vice versa." |
|
7253 | 2484 |
|
2485 |
^ self |
|
9291 | 2486 |
from:startIndex |
2487 |
to:self size |
|
2488 |
by:1 |
|
8874 | 2489 |
|
2490 |
" |
|
2491 |
#(1 2 3 4 5 6 7) from:3 |
|
2492 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2493 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2494 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2495 |
" |
|
2496 |
! |
|
2497 |
||
2498 |
from:startIndex by:step |
|
9291 | 2499 |
"Create a ReindexedCollection from the receiver. |
2500 |
The new collection represents the elements starting at startIndex to the end. |
|
2501 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2502 |
however, physically, no copy is made). |
|
2503 |
Warning: |
|
2504 |
The slice SHARES the memory for the element-data with the original, |
|
2505 |
this means that any modifications in the original are visible in the slice |
|
2506 |
and vice versa." |
|
7253 | 2507 |
|
2508 |
^ self |
|
9291 | 2509 |
from:startIndex |
2510 |
to:(step > 1 |
|
2511 |
ifTrue: [self size] |
|
2512 |
ifFalse: [1]) |
|
2513 |
by:step |
|
8874 | 2514 |
|
2515 |
" |
|
2516 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2517 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2518 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2519 |
( #(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
|
2520 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2521 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2522 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2523 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2524 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2525 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2526 |
Warning: |
2527 |
The slice SHARES the memory for the element-data with the original, |
|
2528 |
this means that any modifications in the original are visible in the slice |
|
2529 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2530 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2531 |
^ self |
9291 | 2532 |
from:startIndex |
2533 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2534 |
|
2535 |
" |
|
2536 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2537 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2538 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2539 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2540 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2541 |
|coll slice| |
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 |
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
|
2544 |
slice := coll from:3 to:7. |
8874 | 2545 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2546 |
coll at:4 put:40. |
8874 | 2547 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2548 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2549 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2550 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2551 |
|coll slice| |
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 |
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
|
2554 |
slice := coll from:3 to:7. |
8874 | 2555 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2556 |
slice at:1 put:40. |
8874 | 2557 |
slice. |
2558 |
" |
|
2559 |
! |
|
2560 |
||
2561 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2562 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2563 |
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
|
2564 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2565 |
however, physically, no copy is made). |
9291 | 2566 |
Warning: |
2567 |
The slice SHARES the memory for the element-data with the original, |
|
2568 |
this means that any modifications in the original are visible in the slice |
|
2569 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2570 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2571 |
^ self |
9291 | 2572 |
from:startIndex |
2573 |
to:endIndex |
|
2574 |
by: (startIndex <= endIndex |
|
2575 |
ifTrue: [1] |
|
2576 |
ifFalse: [-1]) |
|
8874 | 2577 |
|
2578 |
" |
|
2579 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2580 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2581 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2582 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2583 |
" |
|
2584 |
! |
|
2585 |
||
2586 |
from:startIndex to:endIndex by:step |
|
9291 | 2587 |
"Create a ReindexedCollection from the receiver. |
2588 |
The new collection represents the receivers elements up to endIndex. |
|
2589 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2590 |
however, physically, no copy is made). |
|
2591 |
Warning: |
|
2592 |
The slice SHARES the memory for the element-data with the original, |
|
2593 |
this means that any modifications in the original are visible in the slice |
|
2594 |
and vice versa." |
|
7253 | 2595 |
|
2596 |
^ ReindexedCollection |
|
9291 | 2597 |
on:self |
2598 |
from:startIndex |
|
2599 |
to:endIndex |
|
2600 |
by:step |
|
8874 | 2601 |
|
2602 |
" |
|
2603 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2604 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2605 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2606 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2607 |
" |
|
2608 |
! |
|
2609 |
||
2610 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2611 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2612 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2613 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2614 |
however, physically, no copy is made). |
9291 | 2615 |
Warning: |
2616 |
The slice SHARES the memory for the element-data with the original, |
|
2617 |
this means that any modifications in the original are visible in the slice |
|
2618 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2619 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2620 |
^ self |
9291 | 2621 |
from:1 |
2622 |
to:endIndex |
|
2623 |
by:1 |
|
8874 | 2624 |
|
2625 |
" |
|
2626 |
#(1 2 3 4 5 6 7) to:4 |
|
2627 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2628 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2629 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2630 |
" |
|
2631 |
! |
|
2632 |
||
2633 |
to:endIndex by:step |
|
9291 | 2634 |
"Create a ReindexedCollection from the receiver. |
2635 |
The new collection represents the receivers elements up to endIndex. |
|
2636 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2637 |
however, physically, no copy is made). |
|
2638 |
Warning: |
|
2639 |
The slice SHARES the memory for the element-data with the original, |
|
2640 |
this means that any modifications in the original are visible in the slice |
|
2641 |
and vice versa." |
|
7253 | 2642 |
|
2643 |
^ self |
|
9291 | 2644 |
from:(step > 0 |
2645 |
ifTrue: [1] |
|
2646 |
ifFalse: [self size]) |
|
2647 |
to:endIndex |
|
2648 |
by:step |
|
8874 | 2649 |
|
2650 |
" |
|
2651 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2652 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2653 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2654 |
( #(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
|
2655 |
" |
7253 | 2656 |
! ! |
2657 |
||
1 | 2658 |
!SequenceableCollection methodsFor:'copying'! |
2659 |
||
9106 | 2660 |
++ aCollection |
2661 |
"return a new collection formed from concatenating the receiver with |
|
2662 |
the argument. The class of the new collection is determined by the |
|
2663 |
receivers class, so mixing classes is possible, if the second collections |
|
2664 |
elements can be stored into instances of the receivers class." |
|
2665 |
||
2666 |
^ self , aCollection |
|
2667 |
||
2668 |
" |
|
2669 |
#($a $b $c) ++ #(1 2 3) |
|
2670 |
#($a $b $c) ++ '123' |
|
2671 |
'abc' ++ '123' |
|
2672 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2673 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2674 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2675 |
" |
|
2676 |
! |
|
2677 |
||
1 | 2678 |
, aCollection |
2679 |
"return a new collection formed from concatenating the receiver with |
|
44 | 2680 |
the argument. The class of the new collection is determined by the |
2681 |
receivers class, so mixing classes is possible, if the second collections |
|
2682 |
elements can be stored into instances of the receivers class." |
|
1 | 2683 |
|
8874 | 2684 |
|newCollection |
1 | 2685 |
mySize "{ Class: SmallInteger }" |
2686 |
newSize "{ Class: SmallInteger }" |
|
2687 |
otherSize "{ Class: SmallInteger }" |
|
2688 |
dstIndex "{ Class: SmallInteger }"| |
|
2689 |
||
2690 |
mySize := self size. |
|
2691 |
otherSize := aCollection size. |
|
2692 |
newSize := mySize + otherSize. |
|
202 | 2693 |
|
252 | 2694 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 2695 |
|
2696 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
2697 |
dstIndex := mySize + 1. |
|
202 | 2698 |
aCollection isSequenceable ifTrue:[ |
8874 | 2699 |
" |
2700 |
yes, aCollection has indexed elements |
|
2701 |
" |
|
2702 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 2703 |
] ifFalse:[ |
8874 | 2704 |
" |
2705 |
no, enumerate aCollection |
|
2706 |
" |
|
2707 |
aCollection do:[:element | |
|
2708 |
newCollection at:dstIndex put:element. |
|
2709 |
dstIndex := dstIndex + 1 |
|
2710 |
] |
|
1 | 2711 |
]. |
2712 |
^ newCollection |
|
44 | 2713 |
|
95 | 2714 |
" |
2715 |
#($a $b $c) , #(1 2 3) |
|
2716 |
#($a $b $c) , '123' |
|
2717 |
'abc' , '123' |
|
2718 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
2719 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2720 |
'abc' asArray , #(1 2 3 4 5) |
|
2721 |
" |
|
1 | 2722 |
! |
2723 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2724 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2725 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2726 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2727 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2728 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2729 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2730 |
" |
8874 | 2731 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2732 |
'1234567890' copyButFirst:2 |
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 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2735 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2736 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2737 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2738 |
except for the last count elements - for convenience." |
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 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2741 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2742 |
" |
8874 | 2743 |
#($a $b $c $d $e $f $g) copyButLast:2 |
2744 |
'1234567890' copyButLast:2 |
|
5206
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 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2747 |
|
602 | 2748 |
copyFirst:count |
2749 |
"return a new collection consisting of the receivers first count |
|
2750 |
elements - this is just a rename of copyTo: - for compatibility." |
|
2751 |
||
2752 |
^ self copyFrom:1 to:count |
|
2753 |
||
2754 |
" |
|
2755 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
2756 |
'1234567890' copyFirst:4 |
|
2757 |
" |
|
2758 |
! |
|
2759 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2760 |
copyFrom:startIndex |
9234 | 2761 |
"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
|
2762 |
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
|
2763 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2764 |
^ self copyFrom:startIndex to:(self size) |
602 | 2765 |
|
2766 |
" |
|
2767 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
2768 |
'1234567890' copyFrom:2 |
|
8874 | 2769 |
(10 to:19) copyFrom:5 |
602 | 2770 |
" |
2771 |
! |
|
2772 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2773 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2774 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2775 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2776 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2777 |
Returns an empty collection if startIndex is beyond the receivers size. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2778 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2779 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2780 |
^ self |
8874 | 2781 |
copyFrom:startIndex |
2782 |
to:(startIndex + numberOfElements - 1) |
|
2783 |
||
2784 |
" |
|
2785 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
2786 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2787 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2788 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2789 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2790 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
2791 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2792 |
copyFrom:startIndex to:stopIndex |
602 | 2793 |
"return a new collection consisting of receivers elements |
4403 | 2794 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2795 |
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
|
2796 |
Raise an error, if stopIndex is out of range." |
602 | 2797 |
|
2798 |
|newCollection newSize| |
|
2799 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2800 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2801 |
newSize < 0 ifTrue:[ |
8874 | 2802 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
2803 |
]. |
602 | 2804 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2805 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 2806 |
^ newCollection |
2807 |
||
2808 |
" |
|
2809 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
2810 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 2811 |
(1 to:10) copyFrom:2 to:5 |
2812 |
||
2813 |
'1234567890' copyFrom:2 to:15 |
|
2814 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 2815 |
" |
2816 |
! |
|
2817 |
||
2818 |
copyLast:count |
|
2819 |
"return a new collection consisting of the receivers last count |
|
2820 |
elements." |
|
2821 |
||
2822 |
|sz| |
|
2823 |
||
2824 |
sz := self size. |
|
2825 |
^ self copyFrom:(sz - count + 1) to:sz |
|
2826 |
||
2827 |
" |
|
2828 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
2829 |
'1234567890' copyLast:4 |
|
2830 |
" |
|
2831 |
! |
|
2832 |
||
3653 | 2833 |
copyReplaceAll:oldElement with:newElement |
2834 |
"return a copy of the receiver, where all elements equal to oldElement |
|
2835 |
have been replaced by newElement." |
|
2836 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2837 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 2838 |
^ self copy replaceAll:oldElement with:newElement |
2839 |
||
2840 |
" |
|
8874 | 2841 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
2842 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 2843 |
" |
2844 |
||
2845 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
2846 |
! |
|
2847 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2848 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2849 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2850 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2851 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2852 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2853 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2854 |
|
5489 | 2855 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2856 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2857 |
"/ '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
|
2858 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2859 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2860 |
self do:[:el | |
8874 | 2861 |
el = oldObject ifTrue:[ |
2862 |
aCollection do:[:el2 | |
|
2863 |
s nextPut:el2 |
|
2864 |
] |
|
2865 |
] ifFalse:[ |
|
2866 |
s nextPut:el |
|
2867 |
] |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2868 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2869 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2870 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2871 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2872 |
args: oldObject : <object> |
8874 | 2873 |
newObject : <collection> |
3992
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 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2876 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2877 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2878 |
" |
8874 | 2879 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
2880 |
#(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
|
2881 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2882 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
2883 |
|
602 | 2884 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
2885 |
"return a copy of the receiver, where the elements from startIndex to |
|
2886 |
endIndex have been replaced by the elements of aCollection" |
|
2887 |
||
2888 |
|newColl sz replSize| |
|
2889 |
||
2890 |
replSize := aCollection size. |
|
2891 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
2892 |
newColl := self copyEmptyAndGrow:sz. |
|
2893 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
2894 |
newColl replaceFrom:startIndex with:aCollection. |
|
2895 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
2896 |
^ newColl |
|
2897 |
||
2898 |
" |
|
2899 |
#(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 | 2900 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 2901 |
" |
2902 |
! |
|
2903 |
||
2904 |
copyReplaceFrom:startIndex with:aCollection |
|
2905 |
"return a copy of the receiver, where the elements from startIndex to |
|
2906 |
the end have been replaced by the elements of aCollection" |
|
2907 |
||
2908 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
2909 |
||
2910 |
" |
|
8874 | 2911 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
2912 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 2913 |
" |
2914 |
! |
|
2915 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2916 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2917 |
"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
|
2918 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2919 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2920 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2921 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2922 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2923 |
" |
8874 | 2924 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
2925 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
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 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
2928 |
|
602 | 2929 |
copyThrough:element |
2930 |
"return a new collection consisting of the receiver elements |
|
8874 | 2931 |
up-to (AND including) the first occurrence of element." |
602 | 2932 |
|
2933 |
|idx| |
|
2934 |
||
2935 |
idx := self indexOf:element. |
|
2936 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
2937 |
^ self copyFrom:1 to:idx |
|
2938 |
||
2939 |
" |
|
2940 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
2941 |
'1234567890' copyThrough:$5 |
|
2942 |
'1234567890' copyThrough:$a |
|
2943 |
'1234567890' copyThrough:$1 |
|
2944 |
" |
|
2945 |
! |
|
2946 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2947 |
copyTo:stopIndex |
9234 | 2948 |
"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
|
2949 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2950 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2951 |
^ self copyFrom:1 to:stopIndex |
602 | 2952 |
|
2953 |
" |
|
2954 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
2955 |
'1234567890' copyTo:4 |
|
2956 |
" |
|
2957 |
! |
|
2958 |
||
5526 | 2959 |
copyToMax:stop |
2960 |
"return a new collection consisting of receivers elements |
|
8874 | 2961 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 2962 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
2963 |
||
2964 |
^ self copyFrom:1 to:(self size min:stop) |
|
2965 |
||
2966 |
" |
|
2967 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
2968 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
2969 |
" |
|
2970 |
! |
|
2971 |
||
602 | 2972 |
copyUpTo:element |
2973 |
"return a new collection consisting of the receiver elements |
|
8874 | 2974 |
up-to (but excluding) the first occurrence of element; |
602 | 2975 |
or to the end, if element is not included" |
2976 |
||
2977 |
|idx| |
|
2978 |
||
2979 |
idx := self indexOf:element. |
|
2980 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
2981 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
2982 |
^ self copyFrom:1 to:(idx-1) |
|
2983 |
||
2984 |
" |
|
2985 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
2986 |
'1234567890' copyUpTo:$5 |
|
2987 |
'1234567890' copyUpTo:$a |
|
2988 |
'1234567890' copyUpTo:$1 |
|
2989 |
'123456789' copyUpTo:$0 |
|
2990 |
" |
|
2991 |
! |
|
2992 |
||
1 | 2993 |
copyWith:newElement |
233 | 2994 |
"return a new collection containing the receivers elements |
8874 | 2995 |
and the single new element, newElement. |
233 | 2996 |
This is different from concatentation, which expects another collection |
2997 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 2998 |
|
2999 |
|newCollection mySize newSize| |
|
3000 |
||
3001 |
mySize := self size. |
|
3002 |
newSize := mySize + 1. |
|
252 | 3003 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3004 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3005 |
newCollection at:newSize put:newElement. |
|
3006 |
^newCollection |
|
44 | 3007 |
|
95 | 3008 |
" |
3009 |
#(1 2 3 4 5) copyWith:$a |
|
3010 |
'abcdefg' copyWith:$h |
|
3011 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3012 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3013 |
" |
|
1 | 3014 |
! |
3015 |
||
61 | 3016 |
copyWithout:elementToSkip |
3017 |
"return a new collection consisting of a copy of the receiver, with |
|
3018 |
ALL elements equal to elementToSkip are left out. |
|
3019 |
No error is reported, if elementToSkip is not in the collection." |
|
3020 |
||
293 | 3021 |
|n "{ Class: SmallInteger }" |
3022 |
sz "{ Class: SmallInteger }" |
|
3023 |
srcIndex "{ Class: SmallInteger }" |
|
3024 |
dstIndex "{ Class: SmallInteger }" |
|
3025 |
skipIndex "{ Class: SmallInteger }" |
|
3026 |
copy l| |
|
61 | 3027 |
|
8874 | 3028 |
"the code below may look like overkill, |
61 | 3029 |
however, for big collections its better to move data |
3030 |
around in big chunks" |
|
3031 |
||
3032 |
n := self occurrencesOf:elementToSkip. |
|
3033 |
n == 0 ifTrue:[^ self copy]. |
|
3034 |
||
3035 |
sz := self size. |
|
252 | 3036 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3037 |
|
3038 |
srcIndex := 1. |
|
3039 |
dstIndex := 1. |
|
3040 |
||
3041 |
n timesRepeat:[ |
|
8874 | 3042 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3043 |
l := skipIndex - srcIndex. |
|
3044 |
l ~~ 0 ifTrue:[ |
|
3045 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3046 |
with:self startingAt:srcIndex. |
|
3047 |
dstIndex := dstIndex + l |
|
3048 |
]. |
|
3049 |
srcIndex := skipIndex + 1 |
|
61 | 3050 |
]. |
3051 |
l := sz - srcIndex. |
|
3052 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3053 |
with:self startingAt:srcIndex. |
61 | 3054 |
^ copy |
3055 |
||
95 | 3056 |
" |
3057 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3058 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3059 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3060 |
'abcdefghi' copyWithout:$h |
3061 |
'abcdefg' copyWithout:$h |
|
95 | 3062 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3063 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3064 |
#(90 80 70 60 50) copyWithout:70 |
|
3065 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3066 |
" |
|
61 | 3067 |
! |
3068 |
||
602 | 3069 |
copyWithoutFirst:elementToSkip |
3070 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3071 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3072 |
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
|
3073 |
Do not confuse this with copyButFirst:" |
602 | 3074 |
|
3075 |
|copy skipIndex sz| |
|
3076 |
||
3077 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3078 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3079 |
||
3080 |
sz := self size - 1. |
|
3081 |
copy := self copyEmptyAndGrow:sz. |
|
3082 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3083 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3084 |
^ copy |
|
3085 |
||
3086 |
" |
|
3087 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3088 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3089 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3090 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3091 |
" |
|
3092 |
! |
|
3093 |
||
8673 | 3094 |
copyWithoutIdentical:elementToSkip |
3095 |
"return a new collection consisting of a copy of the receiver, with |
|
3096 |
ALL elements identical to elementToSkip are left out. |
|
3097 |
No error is reported, if elementToSkip is not in the collection." |
|
3098 |
||
3099 |
|n "{ Class: SmallInteger }" |
|
3100 |
sz "{ Class: SmallInteger }" |
|
3101 |
srcIndex "{ Class: SmallInteger }" |
|
3102 |
dstIndex "{ Class: SmallInteger }" |
|
3103 |
skipIndex "{ Class: SmallInteger }" |
|
3104 |
copy l| |
|
3105 |
||
8874 | 3106 |
"the code below may look like overkill, |
8673 | 3107 |
however, for big collections its better to move data |
3108 |
around in big chunks" |
|
3109 |
||
3110 |
n := self count:[:el | el == elementToSkip]. |
|
3111 |
n == 0 ifTrue:[^ self copy]. |
|
3112 |
||
3113 |
sz := self size. |
|
3114 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3115 |
||
3116 |
srcIndex := 1. |
|
3117 |
dstIndex := 1. |
|
3118 |
||
3119 |
n timesRepeat:[ |
|
8874 | 3120 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3121 |
l := skipIndex - srcIndex. |
|
3122 |
l ~~ 0 ifTrue:[ |
|
3123 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3124 |
with:self startingAt:srcIndex. |
|
3125 |
dstIndex := dstIndex + l |
|
3126 |
]. |
|
3127 |
srcIndex := skipIndex + 1 |
|
8673 | 3128 |
]. |
3129 |
l := sz - srcIndex. |
|
3130 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3131 |
with:self startingAt:srcIndex. |
8673 | 3132 |
^ copy |
3133 |
||
3134 |
" |
|
8874 | 3135 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3136 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3137 |
" |
3138 |
! |
|
3139 |
||
61 | 3140 |
copyWithoutIndex:omitIndex |
3141 |
"return a new collection consisting of receivers elements |
|
3142 |
without the argument stored at omitIndex" |
|
3143 |
||
3144 |
|copy sz| |
|
3145 |
||
202 | 3146 |
sz := self size - 1. |
252 | 3147 |
copy := self copyEmptyAndGrow:sz. |
293 | 3148 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3149 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3150 |
^ copy |
3151 |
||
95 | 3152 |
" |
3153 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3154 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3155 |
" |
|
1 | 3156 |
! |
3157 |
||
202 | 3158 |
copyWithoutLast:count |
3159 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3160 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3161 |
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
|
3162 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3163 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3164 |
|
9250 | 3165 |
<resource:#obsolete> |
3166 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3167 |
^ self copyTo:(self size - count) |
202 | 3168 |
|
3169 |
" |
|
8874 | 3170 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3171 |
'1234567890' copyWithoutLast:4 |
|
202 | 3172 |
" |
5231 | 3173 |
! |
3174 |
||
3175 |
restAfter:anElement |
|
8366 | 3176 |
"return a new collection consisting of the receivers elements after |
5231 | 3177 |
(but excluding) anElement. |
8366 | 3178 |
If anElement is not in the receiver, the returned collection |
3179 |
will be empty. |
|
3180 |
See also #upTo:." |
|
5231 | 3181 |
|
3182 |
|pos| |
|
3183 |
||
3184 |
pos := self indexOf:anElement. |
|
8366 | 3185 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3186 |
||
5231 | 3187 |
^ self copyFrom:(pos + 1) |
3188 |
||
3189 |
" |
|
8874 | 3190 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3191 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3192 |
'hello world' upTo:Character space |
|
3193 |
'hello world' restAfter:Character space |
|
3194 |
'1234.5678' upTo:$. |
|
3195 |
'1234.5678' restAfter:$. |
|
8366 | 3196 |
" |
5231 | 3197 |
! |
3198 |
||
3199 |
upTo:anElement |
|
3200 |
"return a new collection consisting of the receivers elements upTo |
|
3201 |
(but excluding) anElement. |
|
3202 |
If anElement is not in the receiver, the returned collection |
|
3203 |
will consist of all elements of the receiver. |
|
9307 | 3204 |
See also #restAfter: , #copyFrom:index." |
5231 | 3205 |
|
3206 |
|pos| |
|
3207 |
||
3208 |
pos := self indexOf:anElement. |
|
3209 |
pos == 0 ifTrue:[^ self copy]. |
|
3210 |
||
3211 |
^ self copyFrom:1 to:(pos - 1) |
|
3212 |
||
3213 |
" |
|
8874 | 3214 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3215 |
'hello world' upTo:Character space |
|
3216 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3217 |
'1234.5678' upTo:$. |
|
3218 |
'1234' upTo:$. |
|
5231 | 3219 |
|
3220 |
raises an error: |
|
3221 |
||
8874 | 3222 |
(Dictionary new |
9307 | 3223 |
at:#foo put:'foo'; |
3224 |
at:#bar put:'bar'; |
|
3225 |
at:#baz put:'baz'; |
|
3226 |
yourself) upTo:#bar |
|
8874 | 3227 |
|
5231 | 3228 |
" |
3229 |
||
3230 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3231 |
! |
|
3232 |
||
3233 |
upTo:anElement count:count |
|
3234 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3235 |
(but excluding) count found anElements, i.e. the first found count-1 |
3236 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3237 |
procedure is done from the end of the collection backwards. |
3238 |
If anElement is not in the receiver, the returned collection |
|
3239 |
will consist of all elements of the receiver" |
|
3240 |
||
3241 |
|startPos endPos pos found| |
|
3242 |
||
3243 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3244 |
^ self upTo:anElement |
5231 | 3245 |
]. |
3246 |
||
3247 |
startPos := 1. |
|
3248 |
endPos := self size. |
|
3249 |
found := 0. |
|
3250 |
||
3251 |
count > 1 ifTrue:[ |
|
8874 | 3252 |
pos := 0. |
3253 |
[pos < self size and: [found < count]] |
|
3254 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3255 |
found == count ifTrue:[ |
|
3256 |
endPos := pos - 1 |
|
3257 |
] |
|
3258 |
]. |
|
5231 | 3259 |
|
3260 |
count < 0 ifTrue:[ |
|
8874 | 3261 |
pos := self size + 1. |
3262 |
[pos > 1 and: [found < count abs]] |
|
3263 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3264 |
found == count abs ifTrue:[ |
|
3265 |
startPos := pos + 1 |
|
3266 |
] |
|
3267 |
]. |
|
5231 | 3268 |
|
3269 |
^ self copyFrom: startPos to: endPos |
|
3270 |
||
3271 |
" |
|
8874 | 3272 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3273 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3274 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3275 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3276 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3277 |
" |
|
5563 | 3278 |
! |
3279 |
||
3280 |
upToAny:aCollectionOfObjects |
|
3281 |
"return a new collection consisting of the receivers elements upTo |
|
3282 |
(but excluding) any in aCollectionOfObjects. |
|
3283 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3284 |
will consist of all elements of the receiver. |
|
3285 |
See also #upTo:" |
|
3286 |
||
3287 |
|pos| |
|
3288 |
||
3289 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3290 |
pos == 0 ifTrue:[^ self copy]. |
|
3291 |
||
3292 |
^ self copyFrom:1 to:(pos - 1) |
|
3293 |
||
3294 |
" |
|
8874 | 3295 |
'hello world' upToAny:#($/ $:) |
3296 |
'hello:world' upToAny:#($/ $:) |
|
3297 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3298 |
" |
602 | 3299 |
! ! |
3300 |
||
3301 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3302 |
||
3303 |
collect:aBlock |
|
3304 |
"evaluate the argument, aBlock for every element in the collection |
|
3305 |
and return a collection of the results" |
|
3306 |
||
3307 |
|newCollection |
|
3308 |
sz "{ Class:SmallInteger }"| |
|
3309 |
||
3310 |
sz := self size. |
|
3311 |
newCollection := self copyEmptyAndGrow:sz. |
|
3312 |
1 to:sz do:[:index | |
|
8874 | 3313 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3314 |
]. |
3315 |
^ newCollection |
|
3316 |
||
3317 |
" |
|
8874 | 3318 |
#(one two three four five six) collect:[:element | element asUppercase] |
3319 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3320 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3321 |
" |
202 | 3322 |
! |
3323 |
||
602 | 3324 |
do:aBlock |
3325 |
"evaluate the argument, aBlock for every element in the collection." |
|
3326 |
||
3327 |
|stop "{ Class:SmallInteger }"| |
|
3328 |
||
3329 |
stop := self size. |
|
3330 |
1 to:stop do:[:index | |
|
8874 | 3331 |
aBlock value:(self at:index). |
602 | 3332 |
] |
2927 | 3333 |
|
602 | 3334 |
" |
1422 | 3335 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 3336 |
" |
3337 |
! |
|
3338 |
||
2927 | 3339 |
do:aBlock separatedBy:sepBlock |
3340 |
"evaluate the argument, aBlock for every element in the collection. |
|
3341 |
Between each element (i.e. not before the first element and not after the |
|
3342 |
last element), evaluate sepBlock. |
|
3343 |
This supports printing of collections with elements separated by some |
|
3344 |
string." |
|
3345 |
||
3346 |
|stop "{ Class:SmallInteger }"| |
|
3347 |
||
3348 |
stop := self size. |
|
3349 |
stop < 1 ifTrue:[^ self]. |
|
3350 |
aBlock value:(self at:1). |
|
3351 |
2 to:stop do:[:index | |
|
8874 | 3352 |
sepBlock value. |
3353 |
aBlock value:(self at:index) |
|
2927 | 3354 |
]. |
3355 |
||
3356 |
" |
|
3357 |
#(one two three four five six) |
|
8874 | 3358 |
do:[:each | Transcript show:each] |
3359 |
separatedBy:[Transcript show:' , '] |
|
2927 | 3360 |
" |
3361 |
! |
|
3003 | 3362 |
|
3044 | 3363 |
doWithIndex:aBlock |
3364 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
3365 |
the index as second argument." |
|
3366 |
||
3367 |
self keysAndValuesDo:[:index :value | |
|
8874 | 3368 |
aBlock value:value value:index |
3044 | 3369 |
]. |
3370 |
||
3371 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
3372 |
! |
|
3373 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3374 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3375 |
"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
|
3376 |
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
|
3377 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3378 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3379 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3380 |
" |
8874 | 3381 |
#(one two three four five six) |
3382 |
from:2 |
|
3383 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3384 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3385 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3386 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3387 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3388 |
|
602 | 3389 |
from:startIndex do:aBlock |
3390 |
"evaluate the argument, aBlock for the elements starting with the |
|
3391 |
element at startIndex to the end." |
|
3392 |
||
3393 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 3394 |
|
95 | 3395 |
" |
8874 | 3396 |
#(one two three four five six) |
3397 |
from:3 |
|
3398 |
do:[:element | Transcript showCR:element] |
|
602 | 3399 |
" |
3400 |
! |
|
3401 |
||
7490 | 3402 |
from:startIndex keysAndValuesDo:aBlock |
3403 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
3404 |
element at startIndex to the end." |
|
3405 |
||
3406 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
3407 |
||
3408 |
" |
|
8874 | 3409 |
#(one two three four five six) |
3410 |
from:3 |
|
3411 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 3412 |
" |
3413 |
! |
|
3414 |
||
7337 | 3415 |
from:index1 to:index2 by:stepArg do:aBlock |
3416 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
3417 |
||
3418 |
|start "{ Class:SmallInteger }" |
|
3419 |
stop "{ Class:SmallInteger }" |
|
3420 |
step "{ Class:SmallInteger }" | |
|
3421 |
||
3422 |
start := index1. "/ these assignments force type checking... |
|
3423 |
stop := index2. "/ and guarantee inline loop code below. |
|
3424 |
step := stepArg. |
|
3425 |
||
3426 |
start to:stop by:step do:[:index | |
|
8874 | 3427 |
aBlock value:(self at:index). |
7337 | 3428 |
] |
3429 |
||
3430 |
" |
|
8874 | 3431 |
#(one two three four five six seven eight nine ten) |
3432 |
from:2 |
|
3433 |
to:10 |
|
3434 |
by:2 |
|
3435 |
do:[:element | Transcript showCR:element] |
|
7337 | 3436 |
" |
3437 |
! |
|
3438 |
||
602 | 3439 |
from:start to:stop collect:aBlock |
3440 |
"evaluate the argument, aBlock for the elements indexed by start |
|
3441 |
to stop in the collection and return a collection of the results" |
|
3442 |
||
3443 |
|newCollection sz |
|
3444 |
idx "{ Class:SmallInteger }"| |
|
3445 |
||
3446 |
sz := stop - start + 1. |
|
3447 |
newCollection := self copyEmptyAndGrow:sz. |
|
3448 |
idx := 1. |
|
3449 |
start to:stop do:[:index | |
|
8874 | 3450 |
newCollection at:idx put:(aBlock value:(self at:index)). |
3451 |
idx := idx + 1 |
|
602 | 3452 |
]. |
3453 |
^ newCollection |
|
3454 |
||
3455 |
" |
|
8874 | 3456 |
#(one two three four five six) |
3457 |
from:2 |
|
3458 |
to:4 |
|
3459 |
collect:[:element | element asUppercase] |
|
602 | 3460 |
" |
3461 |
! |
|
3462 |
||
3463 |
from:index1 to:index2 do:aBlock |
|
3464 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3465 |
index2 in the collection" |
|
3466 |
||
3467 |
|start "{ Class:SmallInteger }" |
|
3468 |
stop "{ Class:SmallInteger }" | |
|
3469 |
||
7337 | 3470 |
start := index1. "/ these assignments force type checking... |
3471 |
stop := index2. "/ and guarantee inline loop code below. |
|
3472 |
||
602 | 3473 |
start to:stop do:[:index | |
8874 | 3474 |
aBlock value:(self at:index). |
602 | 3475 |
] |
3476 |
||
3477 |
" |
|
8874 | 3478 |
#(one two three four five six) |
3479 |
from:3 |
|
3480 |
to:5 |
|
3481 |
do:[:element | Transcript showCR:element] |
|
602 | 3482 |
" |
3483 |
! |
|
3484 |
||
6868 | 3485 |
from:index1 to:index2 keysAndValuesDo:aBlock |
3486 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3487 |
index2 in the collection; pass both index and value." |
|
3488 |
||
3489 |
|start "{ Class:SmallInteger }" |
|
3490 |
stop "{ Class:SmallInteger }" | |
|
3491 |
||
7337 | 3492 |
start := index1. "/ these assignments force type checking... |
3493 |
stop := index2. "/ and guarantee inline loop code below. |
|
3494 |
||
6868 | 3495 |
start to:stop do:[:index | |
8874 | 3496 |
aBlock value:index value:(self at:index). |
6868 | 3497 |
] |
3498 |
||
3499 |
" |
|
8874 | 3500 |
#(one two three four five six) |
3501 |
from:3 |
|
3502 |
to:5 |
|
3503 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 3504 |
" |
3505 |
! |
|
3506 |
||
602 | 3507 |
from:index1 to:index2 reverseDo:aBlock |
3508 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3509 |
index2 in the collection. Step in reverse order" |
|
3510 |
||
3511 |
|start "{ Class:SmallInteger }" |
|
3512 |
stop "{ Class:SmallInteger }" | |
|
3513 |
||
7337 | 3514 |
start := index1. "/ these assignments force type checking... |
3515 |
stop := index2. "/ and guarantee inline loop code below. |
|
3516 |
||
602 | 3517 |
stop to:start by:-1 do:[:index | |
8874 | 3518 |
aBlock value:(self at:index). |
602 | 3519 |
] |
3520 |
||
3521 |
" |
|
8874 | 3522 |
#(one two three four five six) |
3523 |
from:3 |
|
3524 |
to:5 |
|
3525 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 3526 |
" |
61 | 3527 |
! |
3528 |
||
6027 | 3529 |
from:startIndex to:stopIndex select:aBlock |
3530 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
3531 |
and return a collection of those elements for which the block return true." |
|
3532 |
||
3533 |
|element newColl species needCopy |
|
3534 |
n "{ Class:SmallInteger }"| |
|
3535 |
||
3536 |
n := stopIndex - startIndex + 1. |
|
3537 |
species := self species. |
|
3538 |
species growIsCheap ifFalse:[ |
|
8874 | 3539 |
newColl := OrderedCollection new:n. |
3540 |
needCopy := true |
|
6027 | 3541 |
] ifTrue:[ |
8874 | 3542 |
newColl := self copyEmpty:n. |
3543 |
needCopy := false |
|
6027 | 3544 |
]. |
3545 |
startIndex to:stopIndex do:[:index | |
|
8874 | 3546 |
element := self at:index. |
3547 |
(aBlock value:element) ifTrue:[ |
|
3548 |
newColl add:element |
|
3549 |
]. |
|
6027 | 3550 |
]. |
3551 |
needCopy ifTrue:[ |
|
8874 | 3552 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 3553 |
]. |
3554 |
^ newColl |
|
3555 |
||
3556 |
" |
|
8874 | 3557 |
#(faba one two three four five six) |
3558 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 3559 |
" |
3560 |
! |
|
3561 |
||
10150 | 3562 |
inGroupsOf:n collect:anNArgBlock |
3563 |
"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
|
3564 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3565 |
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
|
3566 |
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
|
3567 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3568 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3569 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3570 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3571 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3572 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3573 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3574 |
|
10150 | 3575 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
3576 |
"/ 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
|
3577 |
n == 2 ifTrue:[ |
10150 | 3578 |
1 to:stop by:n do:[:index | |
3579 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
3580 |
newCollection at:dstIdx put:rslt. |
|
3581 |
dstIdx := dstIdx + 1. |
|
3582 |
]. |
|
3583 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3584 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3585 |
n == 3 ifTrue:[ |
10150 | 3586 |
1 to:stop by:n do:[:index | |
3587 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3588 |
newCollection at:dstIdx put:rslt. |
|
3589 |
dstIdx := dstIdx + 1. |
|
3590 |
]. |
|
3591 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3592 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3593 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3594 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3595 |
1 to:stop by:n do:[:index | |
10150 | 3596 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3597 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
3598 |
newCollection at:dstIdx put:rslt. |
|
3599 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3600 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3601 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3602 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3603 |
"for groups of 2, this is the same as: |
8874 | 3604 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3605 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 3606 |
|
3607 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 3608 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3609 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3610 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 3611 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
3612 |
" |
|
3613 |
||
3614 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
3615 |
! |
|
3616 |
||
3617 |
inGroupsOf:n do:anNArgBlock |
|
3618 |
"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
|
3619 |
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
|
3620 |
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
|
3621 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3622 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3623 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3624 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3625 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3626 |
|
10150 | 3627 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
3628 |
"/ 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
|
3629 |
n == 2 ifTrue:[ |
10150 | 3630 |
1 to:stop by:n do:[:index | |
3631 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
3632 |
]. |
|
3633 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3634 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3635 |
n == 3 ifTrue:[ |
10150 | 3636 |
1 to:stop by:n do:[:index | |
3637 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
3638 |
]. |
|
3639 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3640 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3641 |
n == 4 ifTrue:[ |
10150 | 3642 |
1 to:stop by:n do:[:index | |
3643 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
3644 |
]. |
|
3645 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3646 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3647 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3648 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3649 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3650 |
1 to:stop by:n do:[:index | |
10150 | 3651 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
3652 |
anNArgBlock valueWithArguments:argVector. |
|
6554
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 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3655 |
"for groups of 2, this is the same as: |
8874 | 3656 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3657 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 3658 |
|
3659 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 3660 |
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
|
3661 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3662 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 3663 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
3664 |
" |
|
3665 |
||
3666 |
"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
|
3667 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3668 |
|
8853 | 3669 |
keysAndValuesCollect:aTwoArgBlock |
3670 |
"evaluate the argument, aBlock for every element in the collection, |
|
3671 |
passing both index and element as arguments. |
|
3672 |
Collect the returned values and return them." |
|
3673 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3674 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3675 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3676 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3677 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3678 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3679 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 3680 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3681 |
|
8853 | 3682 |
^ newCollection |
3683 |
||
3684 |
" |
|
8874 | 3685 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3686 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3687 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 3688 |
|
3689 |
||
3690 |
|d| |
|
3691 |
||
3692 |
d := Dictionary new. |
|
3693 |
d at:'a' put:'A'. |
|
3694 |
d at:'b' put:'B'. |
|
3695 |
d at:'c' put:'C'. |
|
3696 |
d at:'d' put:'D'. |
|
3697 |
d at:'e' put:'E'. |
|
8874 | 3698 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3699 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3700 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
3701 |
ifFalse:[element asLowercase]] |
8853 | 3702 |
" |
3703 |
! |
|
3704 |
||
602 | 3705 |
keysAndValuesDo:aTwoArgBlock |
3706 |
"evaluate the argument, aBlock for every element in the collection, |
|
3707 |
passing both index and element as arguments." |
|
3708 |
||
3709 |
|stop "{ Class:SmallInteger }"| |
|
3710 |
||
3711 |
stop := self size. |
|
3712 |
1 to:stop do:[:index | |
|
8874 | 3713 |
aTwoArgBlock value:index value:(self at:index). |
602 | 3714 |
] |
3715 |
" |
|
8874 | 3716 |
#(one two three four five six) |
3717 |
keysAndValuesDo:[:key :element | |
|
3718 |
Transcript show:key; space; showCR:element |
|
3719 |
] |
|
602 | 3720 |
" |
3721 |
! |
|
3722 |
||
1359 | 3723 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 3724 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 3725 |
in the collection, passing both index and element as arguments." |
3726 |
||
3727 |
|stop "{ Class:SmallInteger }"| |
|
3728 |
||
3729 |
stop := self size. |
|
3730 |
stop to:1 by:-1 do:[:index | |
|
8874 | 3731 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 3732 |
] |
3733 |
||
3734 |
" |
|
8874 | 3735 |
#(one two three four five six) |
3736 |
keysAndValuesReverseDo:[:key :element | |
|
3737 |
Transcript show:key; space; showCR:element |
|
3738 |
] |
|
1359 | 3739 |
" |
3740 |
||
3741 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
3742 |
! |
|
3743 |
||
602 | 3744 |
nonNilElementsDo:aBlock |
3745 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
3746 |
||
3747 |
|stop "{ Class:SmallInteger }" |
|
3748 |
element| |
|
3749 |
||
3750 |
stop := self size. |
|
3751 |
1 to:stop do:[:index | |
|
8874 | 3752 |
(element := self at:index) notNil ifTrue:[ |
3753 |
aBlock value:element. |
|
3754 |
] |
|
602 | 3755 |
] |
3756 |
" |
|
1422 | 3757 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 3758 |
" |
3759 |
! |
|
3760 |
||
10150 | 3761 |
pairWiseCollect:aTwoArgBlock |
3762 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 3763 |
The block is called with 2 arguments for each 2 elements in the receiver. |
3764 |
An error will be reported, if the number of elements in the receiver |
|
3765 |
is not a multiple of 2. |
|
3766 |
Collect the results and return a new collection containing those." |
|
3767 |
||
10150 | 3768 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 3769 |
|
95 | 3770 |
" |
8874 | 3771 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3772 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 3773 |
|
3774 |
||
3775 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 3776 |
pairWiseCollect:[:x :y | x@y] |
3777 |
" |
|
3778 |
||
3779 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
3780 |
! |
|
3781 |
||
3782 |
pairWiseDo:aTwoArgBlock |
|
10385 | 3783 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
3784 |
An error will be reported, if the number of elements in the receiver |
|
602 | 3785 |
is not a multiple of 2." |
3786 |
||
10150 | 3787 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
3788 |
|
602 | 3789 |
" |
8874 | 3790 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 3791 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 3792 |
|
3793 |
||
3794 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 3795 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
3796 |
" |
|
3797 |
||
10385 | 3798 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 3799 |
! |
3800 |
||
602 | 3801 |
reverseDo:aBlock |
3802 |
"evaluate the argument, aBlock for every element in the collection |
|
3803 |
in reverse order" |
|
3804 |
||
3805 |
|sz "{ Class:SmallInteger }"| |
|
3806 |
||
3807 |
sz := self size. |
|
3808 |
sz to:1 by:-1 do:[:index | |
|
8874 | 3809 |
aBlock value:(self at:index). |
602 | 3810 |
] |
3811 |
||
3812 |
" |
|
1422 | 3813 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 3814 |
" |
3815 |
! |
|
3816 |
||
3817 |
select:aBlock |
|
3818 |
"evaluate the argument, aBlock for every element in the collection |
|
3819 |
and return a collection of all elements for which the block return |
|
3820 |
true" |
|
3821 |
||
3822 |
|element newColl species needCopy |
|
3823 |
sz "{ Class:SmallInteger }"| |
|
3824 |
||
3825 |
sz := self size. |
|
3826 |
species := self species. |
|
3827 |
species growIsCheap ifFalse:[ |
|
8874 | 3828 |
newColl := OrderedCollection new:sz. |
3829 |
needCopy := true |
|
602 | 3830 |
] ifTrue:[ |
8874 | 3831 |
newColl := self copyEmpty:sz. |
3832 |
needCopy := false |
|
602 | 3833 |
]. |
3834 |
1 to:sz do:[:index | |
|
8874 | 3835 |
element := self at:index. |
3836 |
(aBlock value:element) ifTrue:[ |
|
3837 |
newColl add:element |
|
3838 |
]. |
|
602 | 3839 |
]. |
3840 |
needCopy ifTrue:[ |
|
8874 | 3841 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 3842 |
]. |
61 | 3843 |
^ newColl |
3844 |
||
95 | 3845 |
" |
8874 | 3846 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
3847 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
3848 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
3849 |
(1 to:9) select:[:element | element odd] |
|
3850 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 3851 |
" |
186 | 3852 |
! |
3853 |
||
602 | 3854 |
with:aSequenceableCollection do:aTwoArgBlock |
3855 |
"evaluate the argument, aBlock for successive elements from |
|
3856 |
each the receiver and the argument, aSequenceableCollection. |
|
3857 |
The second argument, aBlock must be a two-argument block. |
|
3858 |
The collection argument must implement access via a numeric key." |
|
3859 |
||
3860 |
|stop "{ Class: SmallInteger }" | |
|
3861 |
||
3862 |
stop := self size. |
|
3863 |
1 to:stop do:[:index | |
|
8874 | 3864 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 3865 |
] |
186 | 3866 |
" |
8874 | 3867 |
#(one two three four five six) |
3868 |
with:(1 to:10) |
|
3869 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 3870 |
" |
9454 | 3871 |
! |
3872 |
||
3873 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
3874 |
"evaluate the argument, aBlock for successive elements from |
|
3875 |
each the receiver and the argument, aSequenceableCollection. |
|
3876 |
aBlock must be a three-argument block, which get elements from either collection and |
|
3877 |
the index as arguments. |
|
3878 |
The collection argument must implement access via a numeric key." |
|
3879 |
||
3880 |
|stop "{ Class: SmallInteger }" | |
|
3881 |
||
3882 |
stop := self size. |
|
3883 |
1 to:stop do:[:index | |
|
3884 |
aThreeArgBlock |
|
3885 |
value:(self at:index) |
|
3886 |
value:(aSequenceableCollection at:index) |
|
3887 |
value:index. |
|
3888 |
] |
|
3889 |
" |
|
3890 |
#(one two three four five six) |
|
3891 |
with:(1 to:10) |
|
3892 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
3893 |
" |
|
3894 |
||
3895 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 3896 |
! ! |
3897 |
||
4154 | 3898 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 3899 |
|
602 | 3900 |
atAllPut:anObject |
3901 |
"replace all elements of the collection by the argument, anObject. |
|
3902 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3903 |
therefore the change may affect all others referencing the receiver." |
|
3904 |
||
3905 |
self from:1 to:(self size) put:anObject |
|
3906 |
||
3907 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3908 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3909 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3910 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3911 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3912 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3913 |
" |
602 | 3914 |
(Array new:10) atAllPut:1 |
3915 |
(String new:10) atAllPut:$a |
|
3916 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3917 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3918 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 3919 |
! |
3920 |
||
1 | 3921 |
from:index1 to:index2 put:anObject |
3922 |
"replace the elements from index1 to index2 of the collection |
|
44 | 3923 |
by the argument, anObject. |
3924 |
Notice: This operation modifies the receiver, NOT a copy; |
|
3925 |
therefore the change may affect all others referencing the receiver." |
|
1 | 3926 |
|
3927 |
|index "{ Class: SmallInteger }" |
|
3928 |
end "{ Class: SmallInteger }"| |
|
3929 |
||
3930 |
index := index1. |
|
3931 |
end := index2. |
|
3932 |
[index <= end] whileTrue:[ |
|
8874 | 3933 |
self at:index put:anObject. |
3934 |
index := index + 1 |
|
1 | 3935 |
] |
44 | 3936 |
|
95 | 3937 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3938 |
args: index1 : <integer> |
8874 | 3939 |
index2 : <integer> |
3940 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3941 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3942 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3943 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3944 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3945 |
" |
95 | 3946 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
3947 |
'abcdefghijkl' from:3 to:6 put:$X |
|
3948 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3949 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3950 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 3951 |
! |
3952 |
||
3003 | 3953 |
replaceAll:oldObject by:newObject |
3954 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
3955 |
Pleace do no longer use this one; |
|
3956 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
3957 |
||
5872 | 3958 |
<resource:#obsolete> |
3959 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3960 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 3961 |
^ self replaceAll:oldObject with:newObject |
3962 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3963 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3964 |
args: oldObject : <object> |
8874 | 3965 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3966 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3967 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3968 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3969 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3970 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 3971 |
! |
3972 |
||
3973 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
3974 |
"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
|
3975 |
Please do no longer use this one; |
3003 | 3976 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
3977 |
||
5872 | 3978 |
<resource:#obsolete> |
3979 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3980 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 3981 |
^ 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
|
3982 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3983 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3984 |
args: oldObject : <object> |
8874 | 3985 |
newObject : <object> |
3986 |
startIndex : <integer> |
|
3987 |
stopIndex : <integer> |
|
3988 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3989 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3990 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3991 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3992 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 3993 |
! |
3994 |
||
2927 | 3995 |
replaceAll:oldObject with:newObject |
44 | 3996 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
3997 |
|
44 | 3998 |
Notice: This operation modifies the receiver, NOT a copy; |
3999 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4000 |
|
8874 | 4001 |
^ self |
4002 |
replaceAll:oldObject |
|
4003 |
with:newObject |
|
4004 |
from:1 |
|
4005 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4006 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4007 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4008 |
args: oldObject : <object> |
8874 | 4009 |
newObject : <object> |
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4012 |
" |
362 | 4013 |
|
4014 |
" |
|
8874 | 4015 |
'123123abc123' replaceAll:$1 with:$* |
4016 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4017 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4018 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4019 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4020 |
! |
293 | 4021 |
|
2927 | 4022 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4023 |
"replace all oldObjects found between startIndex and endIndex, |
4024 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4025 |
|
362 | 4026 |
Notice: This operation modifies the receiver, NOT a copy; |
4027 |
therefore the change may affect all others referencing the receiver." |
|
4028 |
||
4029 |
|start "{ Class: SmallInteger }" |
|
4030 |
stop "{ Class: SmallInteger }"| |
|
4031 |
||
4032 |
start := startIndex. |
|
4033 |
stop := stopIndex. |
|
4034 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4035 |
(self at:index) = oldObject ifTrue:[ |
4036 |
self at:index put:newObject |
|
4037 |
] |
|
1 | 4038 |
] |
44 | 4039 |
|
95 | 4040 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4041 |
args: oldObject : <object> |
8874 | 4042 |
newObject : <object> |
4043 |
startIndex : <integer> |
|
4044 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4045 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4046 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4047 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4048 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4049 |
" |
2980 | 4050 |
'123123abc123' replaceAll:$1 with:$* |
4051 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4052 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4053 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4054 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4055 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4056 |
! |
4057 |
||
7615 | 4058 |
replaceAllForWhich:aConditionBlock with:newObject |
4059 |
"replace all elements for which aConditionBlock returns true |
|
4060 |
between startIndex and endIndex, by newObject in the receiver. |
|
4061 |
||
4062 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4063 |
therefore the change may affect all others referencing the receiver." |
|
4064 |
||
8874 | 4065 |
self |
4066 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4067 |
from:1 to:(self size) |
|
7615 | 4068 |
|
4069 |
" |
|
4070 |
args: aConditionBlock : <block> |
|
8874 | 4071 |
newObject : <object> |
7615 | 4072 |
|
4073 |
returns: self |
|
4074 |
" |
|
4075 |
! |
|
4076 |
||
4077 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4078 |
"replace all elements for which aConditionBlock returns true |
|
4079 |
between startIndex and endIndex, by newObject in the receiver. |
|
4080 |
||
4081 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4082 |
therefore the change may affect all others referencing the receiver." |
|
4083 |
||
4084 |
|start "{ Class: SmallInteger }" |
|
4085 |
stop "{ Class: SmallInteger }"| |
|
4086 |
||
4087 |
start := startIndex. |
|
4088 |
stop := stopIndex. |
|
4089 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4090 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4091 |
self at:index put:newObject |
|
4092 |
] |
|
7615 | 4093 |
] |
4094 |
||
4095 |
" |
|
4096 |
args: aConditionBlock : <block> |
|
8874 | 4097 |
newObject : <object> |
4098 |
startIndex : <integer> |
|
4099 |
stopIndex : <integer> |
|
7615 | 4100 |
|
4101 |
returns: self |
|
4102 |
" |
|
4103 |
! |
|
4104 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4105 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4106 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4107 |
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
|
4108 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4109 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4110 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4111 |
|
8874 | 4112 |
^ self |
4113 |
replaceAllIdentical:oldObject |
|
4114 |
with:newObject |
|
4115 |
from:1 |
|
4116 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4117 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4118 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4119 |
args: oldObject : <object> |
8874 | 4120 |
newObject : <object> |
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 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4123 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4124 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4125 |
" |
8874 | 4126 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4127 |
#(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
|
4128 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4129 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4130 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4131 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4132 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4133 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4134 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4135 |
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
|
4136 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4137 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4138 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4139 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4140 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4141 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4142 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4143 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4144 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4145 |
startIndex to:stopIndex do:[:index | |
8874 | 4146 |
(self at:index) == oldObject ifTrue:[ |
4147 |
self at:index put:newObject |
|
4148 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4149 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4150 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4151 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4152 |
args: oldObject : <object> |
8874 | 4153 |
newObject : <object> |
4154 |
startIndex : <integer> |
|
4155 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4156 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4157 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4158 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4159 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4160 |
" |
8874 | 4161 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4162 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4163 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4164 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4165 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4166 |
|
3003 | 4167 |
replaceAny:aCollection by:newObject |
4168 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4169 |
Pleace do no longer use this one; |
3003 | 4170 |
instead use #replaceAny:with: for naming consistence." |
4171 |
||
5872 | 4172 |
<resource:#obsolete> |
4173 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4174 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4175 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4176 |
|
4177 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4178 |
args: aCollection : <colleciton of <object> > |
8874 | 4179 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4180 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4181 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4182 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4183 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4184 |
" |
3003 | 4185 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4186 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4187 |
" |
|
4188 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4189 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4190 |
! |
4191 |
||
3003 | 4192 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4193 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4194 |
Pleace do no longer use this one; |
3003 | 4195 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4196 |
|
5872 | 4197 |
<resource:#obsolete> |
4198 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4199 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4200 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4201 |
|
4202 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4203 |
args: aCollection : <colleciton of <object> > |
8874 | 4204 |
newObject : <object> |
4205 |
startIndex : <integer> |
|
4206 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4207 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4208 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4209 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4210 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4211 |
" |
8874 | 4212 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4213 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4214 |
" |
4215 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4216 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4217 |
! |
4218 |
||
4219 |
replaceAny:aCollection with:newObject |
|
4220 |
"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
|
4221 |
|
2927 | 4222 |
Notice: This operation modifies the receiver, NOT a copy; |
4223 |
therefore the change may affect all others referencing the receiver." |
|
4224 |
||
8874 | 4225 |
^ self |
10439 | 4226 |
replaceAny:aCollection |
4227 |
with:newObject |
|
4228 |
from:1 |
|
4229 |
to:self size |
|
4230 |
||
4231 |
" |
|
4232 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
4233 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
4234 |
" |
|
4235 |
||
4236 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 4237 |
! |
4238 |
||
2927 | 4239 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4240 |
"replace all elements within a range, |
4241 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4242 |
|
362 | 4243 |
Notice: This operation modifies the receiver, NOT a copy; |
4244 |
therefore the change may affect all others referencing the receiver." |
|
4245 |
||
4246 |
|start "{ Class: SmallInteger }" |
|
4247 |
stop "{ Class: SmallInteger }"| |
|
4248 |
||
4249 |
start := startIndex. |
|
4250 |
stop := stopIndex. |
|
4251 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4252 |
(aCollection includes:(self at:index)) ifTrue:[ |
4253 |
self at:index put:newObject |
|
4254 |
] |
|
95 | 4255 |
] |
4256 |
||
4257 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4258 |
args: aCollection : <colleciton of <object> > |
8874 | 4259 |
newObject : <object> |
4260 |
startIndex : <integer> |
|
4261 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4262 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4263 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4264 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4265 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4266 |
" |
8874 | 4267 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4268 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4269 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4270 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4271 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4272 |
! |
4273 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4274 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4275 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4276 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4277 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4278 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4279 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4280 |
|
8874 | 4281 |
^ self |
4282 |
replaceFrom:startIndex |
|
4283 |
to:(startIndex + numberOfElements - 1) |
|
4284 |
with:replacementCollection |
|
4285 |
startingAt:1 |
|
4286 |
||
4287 |
" |
|
4288 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4289 |
#($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
|
4290 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4291 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4292 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4293 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4294 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4295 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4296 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4297 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4298 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4299 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4300 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4301 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4302 |
^ self |
8874 | 4303 |
replaceFrom:startIndex |
4304 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4305 |
with:replacementCollection |
|
4306 |
startingAt:repStartIndex |
|
4307 |
||
4308 |
" |
|
4309 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4310 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4311 |
#($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
|
4312 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4313 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4314 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4315 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4316 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4317 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4318 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 4319 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4320 |
|
44 | 4321 |
Notice: This operation modifies the receiver, NOT a copy; |
4322 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4323 |
|
8874 | 4324 |
^ self |
4325 |
replaceFrom:startIndex |
|
4326 |
to:stopIndex |
|
4327 |
with:replacementCollection |
|
4328 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4329 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4330 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4331 |
args: startIndex : <integer> |
8874 | 4332 |
stopIndex : <integer> |
4333 |
replacementCollection : <colleciton of <object> > |
|
3468
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4336 |
" |
61 | 4337 |
|
95 | 4338 |
" |
4339 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
4340 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
4341 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4342 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4343 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 4344 |
! |
4345 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4346 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4347 |
"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
|
4348 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 4349 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4350 |
|
44 | 4351 |
Notice: This operation modifies the receiver, NOT a copy; |
4352 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4353 |
|
4354 |
|srcIndex "{ Class: SmallInteger }" |
|
4355 |
dstIndex "{ Class: SmallInteger }" |
|
4356 |
end "{ Class: SmallInteger }" | |
|
4357 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4358 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 4359 |
" must do reverse copy " |
4360 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
4361 |
dstIndex := stopIndex. |
|
4362 |
end := startIndex. |
|
4363 |
[dstIndex >= end] whileTrue:[ |
|
4364 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
4365 |
srcIndex := srcIndex - 1. |
|
4366 |
dstIndex := dstIndex - 1 |
|
4367 |
]. |
|
4368 |
^ self |
|
1 | 4369 |
]. |
4370 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4371 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4372 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4373 |
end := stopIndex. |
1 | 4374 |
[dstIndex <= end] whileTrue:[ |
8874 | 4375 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
4376 |
srcIndex := srcIndex + 1. |
|
4377 |
dstIndex := dstIndex + 1 |
|
1 | 4378 |
] |
61 | 4379 |
|
95 | 4380 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4381 |
args: startIndex : <integer> |
8874 | 4382 |
stopIndex : <integer> |
4383 |
replacementCollection : <colleciton of <object> > |
|
4384 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4385 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4386 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4387 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4388 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4389 |
" |
8874 | 4390 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
4391 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
4392 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 4393 |
|
4394 |
|c| |
|
4395 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4396 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 4397 |
|
4398 |
|c| |
|
4399 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4400 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 4401 |
" |
3234 | 4402 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4403 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 4404 |
! |
4405 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4406 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4407 |
"replace elements in the receiver starting at startIndex, |
602 | 4408 |
with elements taken from replacementCollection starting at 1 |
4409 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4410 |
|
602 | 4411 |
Notice: This operation modifies the receiver, NOT a copy; |
4412 |
therefore the change may affect all others referencing the receiver." |
|
4413 |
||
8874 | 4414 |
^ self |
4415 |
replaceFrom:startIndex |
|
4416 |
to:(startIndex + replacementCollection size - 1) |
|
4417 |
with:replacementCollection |
|
4418 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4419 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4420 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4421 |
args: startIndex : <integer> |
8874 | 4422 |
replacementCollection : <colleciton of <object> > |
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4425 |
" |
602 | 4426 |
|
4427 |
" |
|
4428 |
'1234567890' replaceFrom:5 with:'abc' |
|
4429 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
4430 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4431 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4432 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 4433 |
! |
4434 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4435 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4436 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4437 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 4438 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4439 |
|
602 | 4440 |
Notice: This operation modifies the receiver, NOT a copy; |
4441 |
therefore the change may affect all others referencing the receiver." |
|
4442 |
||
8874 | 4443 |
^ self |
4444 |
replaceFrom:startIndex |
|
4445 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
4446 |
with:replacementCollection |
|
4447 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4448 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4449 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4450 |
args: startIndex : <integer> |
8874 | 4451 |
replacementCollection : <colleciton of <object> > |
4452 |
offset : <integer> |
|
3468
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4455 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4456 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4457 |
" |
8874 | 4458 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 4459 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
4460 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4461 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4462 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 4463 |
! |
4464 |
||
61 | 4465 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
4466 |
"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
|
4467 |
|
61 | 4468 |
Notice: This operation modifies the destination collection, NOT a copy; |
4469 |
therefore the change may affect all others referencing this object." |
|
4470 |
||
8874 | 4471 |
destColl |
4472 |
replaceFrom:destStartIndex |
|
4473 |
to:destEndIndex |
|
4474 |
with:self |
|
4475 |
startingAt:sourceStart |
|
61 | 4476 |
|
4477 |
" |
|
4478 |
|s| |
|
4479 |
s := 'abcdefghijklmnop'. |
|
4480 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
4481 |
s'123defghijklmnop' |
|
4482 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4483 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4484 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 4485 |
! ! |
4486 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4487 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4488 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4489 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4490 |
"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
|
4491 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4492 |
Elements on the left are filled with padElement. |
8874 | 4493 |
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
|
4494 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4495 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4496 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4497 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4498 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4499 |
(len < size) ifTrue:[ |
8874 | 4500 |
s := self species new:size withAll:padElement. |
4501 |
s replaceFrom:(size - len + 1) with:self. |
|
4502 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4503 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4504 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4505 |
" |
8874 | 4506 |
'foo' leftPaddedTo:10 with:$. |
4507 |
'fooBar' leftPaddedTo:5 with:$. |
|
4508 |
123 printString leftPaddedTo:10 with:$. |
|
4509 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
4510 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
4511 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
4512 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
4513 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
4514 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
4515 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4516 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4517 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4518 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4519 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4520 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4521 |
plus pad elements up to length. |
8874 | 4522 |
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
|
4523 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4524 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4525 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4526 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4527 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4528 |
len < newSize ifTrue:[ |
8874 | 4529 |
s := self species new:newSize withAll:padElement. |
4530 |
s replaceFrom:1 to:len with:self. |
|
4531 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4532 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4533 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4534 |
" |
8874 | 4535 |
'foo' paddedTo:10 with:$. |
4536 |
123 printString paddedTo:10 with:$* |
|
4537 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
4538 |
(Float pi printString) paddedTo:15 with:$- |
|
4539 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
4540 |
#[1 2 3 4] paddedTo:6 with:0 |
|
4541 |
#[1 2 3 4] paddedTo:10 with:99 |
|
4542 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4543 |
" |
7116 | 4544 |
! |
4545 |
||
4546 |
paddedToMultipleOf:sizeModulu with:padElement |
|
4547 |
|mySize mod numPad| |
|
4548 |
||
4549 |
mySize := self size. |
|
4550 |
mod := (mySize \\ sizeModulu). |
|
4551 |
mod == 0 ifTrue:[^ self]. |
|
4552 |
||
4553 |
numPad := sizeModulu - mod. |
|
4554 |
^ self , (self species new:numPad withAll:padElement). |
|
4555 |
||
4556 |
" |
|
8874 | 4557 |
#[] paddedToMultipleOf:3 with:0 |
4558 |
#[1] paddedToMultipleOf:3 with:0 |
|
4559 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
4560 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 4561 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
4562 |
||
4563 |
#() paddedToMultipleOf:3 with:nil |
|
4564 |
#(1) paddedToMultipleOf:3 with:nil |
|
4565 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
4566 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
4567 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
4568 |
||
4569 |
'' paddedToMultipleOf:3 with:Character space |
|
4570 |
'a' paddedToMultipleOf:3 with:Character space |
|
4571 |
'ab' paddedToMultipleOf:3 with:Character space |
|
4572 |
'abc' paddedToMultipleOf:3 with:Character space |
|
4573 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
4574 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4575 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4576 |
|
7260 | 4577 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 4578 |
|
5358 | 4579 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 4580 |
"Private!! |
5358 | 4581 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
4582 |
||
4583 |
| i1 i2 val1 val2 out | |
|
4584 |
||
4585 |
i1 := first. |
|
4586 |
i2 := middle + 1. |
|
4587 |
val1 := self at: i1. |
|
4588 |
val2 := self at: i2. |
|
4589 |
out := first - 1. "will be pre-incremented" |
|
4590 |
||
4591 |
"select 'lower' half of the elements based on comparator" |
|
4592 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 4593 |
(aBlock value: val1 value: val2) |
4594 |
ifTrue: [ |
|
4595 |
dst at: (out := out + 1) put: val1. |
|
4596 |
val1 := self at: (i1 := i1 + 1)] |
|
4597 |
ifFalse: [ |
|
4598 |
dst at: (out := out + 1) put: val2. |
|
4599 |
i2 := i2 + 1. |
|
4600 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 4601 |
|
4602 |
"copy the remaining elements" |
|
4603 |
i1 <= middle |
|
8874 | 4604 |
ifTrue: [ |
4605 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
4606 |
ifFalse: [ |
|
4607 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 4608 |
|
4609 |
! |
|
4610 |
||
4611 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
4612 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
4613 |
||
4614 |
| middle | |
|
4615 |
||
4616 |
first = last ifTrue: [^ self]. |
|
4617 |
middle := (first + last) // 2. |
|
4618 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
4619 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
4620 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
4621 |
||
4622 |
||
4623 |
! |
|
4624 |
||
2069 | 4625 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4626 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4627 |
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
|
4628 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4629 |
|
8874 | 4630 |
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
|
4631 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4632 |
|
4633 |
|begin "{ Class: SmallInteger }" |
|
4634 |
end "{ Class: SmallInteger }" |
|
4635 |
b "{ Class: SmallInteger }" |
|
4636 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4637 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4638 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4639 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4640 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4641 |
stack add:inEnd. |
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 notEmpty] whileTrue:[ |
8874 | 4644 |
end := stack removeLast. |
4645 |
begin := stack removeLast. |
|
4646 |
||
4647 |
b := begin. |
|
4648 |
e := end. |
|
4649 |
middleElement := self at:((b + e) // 2). |
|
4650 |
||
4651 |
[b < e] whileTrue:[ |
|
4652 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4653 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4654 |
||
4655 |
(b <= e) ifTrue:[ |
|
4656 |
(b == e) ifFalse:[ |
|
4657 |
temp1 := self at:b. temp2 := self at:e. |
|
4658 |
self at:b put:temp2. self at:e put:temp1 |
|
4659 |
]. |
|
4660 |
b := b + 1. |
|
4661 |
e := e - 1 |
|
4662 |
] |
|
4663 |
]. |
|
4664 |
(begin < e) ifTrue:[ |
|
4665 |
stack add:begin. |
|
4666 |
stack add:e. |
|
4667 |
]. |
|
4668 |
(b < end) ifTrue:[ |
|
4669 |
stack add:b. |
|
4670 |
stack add:end. |
|
4671 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4672 |
] |
2069 | 4673 |
! |
4674 |
||
4675 |
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
|
4676 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4677 |
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
|
4678 |
with degenerated collections." |
2069 | 4679 |
|
4680 |
|begin "{ Class: SmallInteger }" |
|
4681 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4682 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4683 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4684 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4685 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4686 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4687 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4688 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4689 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4690 |
stack add:inEnd. |
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 notEmpty] whileTrue:[ |
8874 | 4693 |
end := stack removeLast. |
4694 |
begin := stack removeLast. |
|
4695 |
||
4696 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4697 |
elB := self at:begin. |
|
4698 |
elE := self at:end. |
|
4699 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4700 |
temp := elB. elB := elE. elE := temp. |
|
4701 |
self at:begin put:elB. |
|
4702 |
self at:end put:elE. |
|
4703 |
]. |
|
4704 |
||
4705 |
nToSort > 2 ifTrue:[ |
|
4706 |
m := (begin + end) // 2. |
|
4707 |
elM := self at:m. |
|
4708 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4709 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4710 |
temp := self at:end. |
|
4711 |
self at:end put:(self at:m). |
|
4712 |
self at:m put:temp. |
|
4713 |
elM := elE |
|
4714 |
] |
|
4715 |
] ifFalse:[ |
|
4716 |
temp := self at:begin. |
|
4717 |
self at:begin put:(self at:m). |
|
4718 |
self at:m put:temp. |
|
4719 |
elM := elB |
|
4720 |
]. |
|
4721 |
nToSort > 3 ifTrue:[ |
|
4722 |
bRun := begin. |
|
4723 |
eRun := end. |
|
4724 |
[ |
|
4725 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4726 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4727 |
bRun <= eRun |
|
4728 |
] whileTrue:[ |
|
4729 |
temp := self at:bRun. |
|
4730 |
self at:bRun put:(self at:eRun). |
|
4731 |
self at:eRun put:temp. |
|
4732 |
]. |
|
4733 |
stack add:begin. |
|
4734 |
stack add:eRun. |
|
4735 |
||
4736 |
stack add:bRun. |
|
4737 |
stack add:end. |
|
4738 |
] |
|
4739 |
] |
|
4740 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4741 |
] |
2069 | 4742 |
! |
4743 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4744 |
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
|
4745 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4746 |
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
|
4747 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4748 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4749 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4750 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4751 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4752 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4753 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4754 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4755 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4756 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4757 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4758 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4759 |
stack add:inEnd. |
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 notEmpty] whileTrue:[ |
8874 | 4762 |
end := stack removeLast. |
4763 |
begin := stack removeLast. |
|
6919
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 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 4766 |
elB := self at:begin. |
4767 |
elE := self at:end. |
|
4768 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
4769 |
temp := elB. elB := elE. elE := temp. |
|
4770 |
self at:begin put:elB. |
|
4771 |
self at:end put:elE. |
|
4772 |
]. |
|
4773 |
||
4774 |
nToSort > 2 ifTrue:[ |
|
4775 |
m := (begin + end) // 2. |
|
4776 |
elM := self at:m. |
|
4777 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
4778 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
4779 |
temp := self at:end. |
|
4780 |
self at:end put:(self at:m). |
|
4781 |
self at:m put:temp. |
|
4782 |
elM := elE |
|
4783 |
] |
|
4784 |
] ifFalse:[ |
|
4785 |
temp := self at:begin. |
|
4786 |
self at:begin put:(self at:m). |
|
4787 |
self at:m put:temp. |
|
4788 |
elM := elB |
|
4789 |
]. |
|
4790 |
nToSort > 3 ifTrue:[ |
|
4791 |
bRun := begin. |
|
4792 |
eRun := end. |
|
4793 |
[ |
|
4794 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
4795 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
4796 |
bRun <= eRun |
|
4797 |
] whileTrue:[ |
|
4798 |
temp := self at:bRun. |
|
4799 |
self at:bRun put:(self at:eRun). |
|
4800 |
self at:eRun put:temp. |
|
4801 |
]. |
|
4802 |
stack add:begin. |
|
4803 |
stack add:eRun. |
|
4804 |
||
4805 |
stack add:bRun. |
|
4806 |
stack add:end. |
|
4807 |
] |
|
4808 |
] |
|
4809 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4810 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4811 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
4812 |
|
2069 | 4813 |
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
|
4814 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4815 |
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
|
4816 |
with degenerated collections." |
2069 | 4817 |
|
4818 |
|begin "{ Class: SmallInteger }" |
|
4819 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4820 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4821 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4822 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4823 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4824 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4825 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4826 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4827 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4828 |
stack add:inEnd. |
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 notEmpty] whileTrue:[ |
8874 | 4831 |
end := stack removeLast. |
4832 |
begin := stack removeLast. |
|
4833 |
||
4834 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
4835 |
||
4836 |
elB := self at:begin. |
|
4837 |
elE := self at:end. |
|
4838 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
4839 |
temp := elB. elB := elE. elE := temp. |
|
4840 |
self at:begin put:elB. |
|
4841 |
self at:end put:elE. |
|
4842 |
||
4843 |
temp := aCollection at:end. |
|
4844 |
aCollection at:end put:(aCollection at:begin). |
|
4845 |
aCollection at:begin put:temp. |
|
4846 |
]. |
|
4847 |
||
4848 |
nToSort > 2 ifTrue:[ |
|
4849 |
m := (begin + end) // 2. |
|
4850 |
elM := self at:m. |
|
4851 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
4852 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
4853 |
temp := self at:end. |
|
4854 |
self at:end put:(self at:m). |
|
4855 |
self at:m put:temp. |
|
4856 |
||
4857 |
temp := aCollection at:end. |
|
4858 |
aCollection at:end put:(aCollection at:m). |
|
4859 |
aCollection at:m put:temp. |
|
4860 |
||
4861 |
elM := elE |
|
4862 |
] |
|
4863 |
] ifFalse:[ |
|
4864 |
temp := self at:begin. |
|
4865 |
self at:begin put:(self at:m). |
|
4866 |
self at:m put:temp. |
|
4867 |
||
4868 |
temp := aCollection at:begin. |
|
4869 |
aCollection at:begin put:(aCollection at:m). |
|
4870 |
aCollection at:m put:temp. |
|
4871 |
elM := elB |
|
4872 |
]. |
|
4873 |
nToSort > 3 ifTrue:[ |
|
4874 |
bRun := begin. |
|
4875 |
eRun := end. |
|
4876 |
[ |
|
4877 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
4878 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
4879 |
bRun <= eRun |
|
4880 |
] whileTrue:[ |
|
4881 |
temp := self at:bRun. |
|
4882 |
self at:bRun put:(self at:eRun). |
|
4883 |
self at:eRun put:temp. |
|
4884 |
||
4885 |
temp := aCollection at:bRun. |
|
4886 |
aCollection at:bRun put:(aCollection at:eRun). |
|
4887 |
aCollection at:eRun put:temp. |
|
4888 |
]. |
|
4889 |
stack add:begin. |
|
4890 |
stack add:eRun. |
|
4891 |
||
4892 |
stack add:bRun. |
|
4893 |
stack add:end. |
|
4894 |
] |
|
4895 |
] |
|
4896 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4897 |
] |
2069 | 4898 |
! |
4899 |
||
4900 |
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
|
4901 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4902 |
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
|
4903 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4904 |
|
8874 | 4905 |
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
|
4906 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 4907 |
|
4908 |
|begin "{ Class: SmallInteger }" |
|
4909 |
end "{ Class: SmallInteger }" |
|
4910 |
b "{ Class: SmallInteger }" |
|
4911 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4912 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4913 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4914 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4915 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4916 |
stack add:inEnd. |
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 notEmpty] whileTrue:[ |
8874 | 4919 |
end := stack removeLast. |
4920 |
begin := stack removeLast. |
|
4921 |
||
4922 |
b := begin. |
|
4923 |
e := end. |
|
4924 |
middleElement := self at:((b + e) // 2). |
|
4925 |
||
4926 |
[b < e] whileTrue:[ |
|
4927 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
4928 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
4929 |
||
4930 |
(b <= e) ifTrue:[ |
|
4931 |
(b == e) ifFalse:[ |
|
4932 |
temp1 := self at:b. temp2 := self at:e. |
|
4933 |
self at:b put:temp2. self at:e put:temp1. |
|
4934 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
4935 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
4936 |
]. |
|
4937 |
b := b + 1. |
|
4938 |
e := e - 1 |
|
4939 |
] |
|
4940 |
]. |
|
4941 |
(begin < e) ifTrue:[ |
|
4942 |
stack add:begin. |
|
4943 |
stack add:e. |
|
4944 |
]. |
|
4945 |
(b < end) ifTrue:[ |
|
4946 |
stack add:b. |
|
4947 |
stack add:end. |
|
4948 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
4949 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4950 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4951 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4952 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4953 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4954 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4955 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4956 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4957 |
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
|
4958 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4959 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4960 |
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
|
4961 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4962 |
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
|
4963 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
4964 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4965 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4966 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4967 |
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
|
4968 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4969 |
"/ 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
|
4970 |
^ 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
|
4971 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
4972 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4973 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4974 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4975 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4976 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4977 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4978 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4979 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4980 |
"/ 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
|
4981 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4982 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4983 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4984 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4985 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4986 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4987 |
"/ "/ 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
|
4988 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4989 |
"/ 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
|
4990 |
"/ 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
|
4991 |
"/ 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
|
4992 |
"/ aCollection notNil ifTrue:[ |
8874 | 4993 |
"/ 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
|
4994 |
"/ 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
|
4995 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4996 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4997 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
4998 |
"/ "/ now proceed as usual |
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 |
"/ 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
|
5001 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5002 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5003 |
"/ [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
|
5004 |
"/ [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
|
5005 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5006 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5007 |
"/ (b == e) ifFalse:[ |
8874 | 5008 |
"/ 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
|
5009 |
"/ 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
|
5010 |
"/ aCollection notNil ifTrue:[ |
8874 | 5011 |
"/ 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
|
5012 |
"/ 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
|
5013 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5014 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5015 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5016 |
"/ e := e - 1 |
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 |
"/ (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
|
5020 |
"/ (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
|
5021 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5022 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5023 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5024 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5025 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5026 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5027 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5028 |
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
|
5029 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5030 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5031 |
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
|
5032 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5033 |
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
|
5034 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5035 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5036 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5037 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5038 |
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
|
5039 |
"/ 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
|
5040 |
^ 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
|
5041 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5042 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5043 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5044 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5045 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5046 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5047 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5048 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5049 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5050 |
"/ 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
|
5051 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5052 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5053 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5054 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5055 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5056 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5057 |
"/ "/ 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
|
5058 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5059 |
"/ 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
|
5060 |
"/ 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
|
5061 |
"/ 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
|
5062 |
"/ aCollection notNil ifTrue:[ |
8874 | 5063 |
"/ 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
|
5064 |
"/ 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
|
5065 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5066 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5067 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5068 |
"/ "/ now proceed as usual |
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 |
"/ 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
|
5071 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5072 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5073 |
"/ [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
|
5074 |
"/ [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
|
5075 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5076 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5077 |
"/ (b == e) ifFalse:[ |
8874 | 5078 |
"/ 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
|
5079 |
"/ 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
|
5080 |
"/ aCollection notNil ifTrue:[ |
8874 | 5081 |
"/ 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
|
5082 |
"/ 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
|
5083 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5084 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5085 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5086 |
"/ e := e - 1 |
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 |
"/ (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
|
5090 |
"/ (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
|
5091 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5092 |
"/ "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
|
5093 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5094 |
! ! |
5095 |
||
602 | 5096 |
!SequenceableCollection methodsFor:'queries'! |
5097 |
||
5098 |
firstIndex |
|
5099 |
"return the first elements index" |
|
5100 |
||
5101 |
^ 1 |
|
5102 |
! |
|
5103 |
||
5104 |
isSequenceable |
|
5105 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5106 |
||
5107 |
^ true |
|
1 | 5108 |
! |
5109 |
||
602 | 5110 |
isSequenceableCollection |
5111 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5112 |
This method is a historic leftover and will be removed." |
|
5113 |
||
5872 | 5114 |
<resource:#obsolete> |
5115 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5116 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5117 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5118 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5119 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5120 |
! |
5121 |
||
5358 | 5122 |
isSorted |
5123 |
"return true. if my elements are sorted (already)" |
|
5124 |
||
5125 |
|lastEl el| |
|
5126 |
||
5127 |
lastEl := self first. |
|
5128 |
2 to:self size do:[:i | |
|
8874 | 5129 |
el := self at:i. |
5130 |
el >= lastEl ifFalse: [^ false]. |
|
5131 |
lastEl := el |
|
5358 | 5132 |
]. |
5133 |
^ true |
|
5134 |
||
5135 |
" |
|
8874 | 5136 |
#(1 2 3 5 10 100) isSorted |
5137 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5138 |
" |
5139 |
! |
|
5140 |
||
5141 |
isSortedBy:aBlock |
|
5142 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5143 |
||
5144 |
|lastEl el| |
|
5145 |
||
5146 |
lastEl := self first. |
|
5147 |
2 to:self size do:[:i | |
|
8874 | 5148 |
el := self at:i. |
5149 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5150 |
lastEl := el |
|
5358 | 5151 |
]. |
5152 |
^ true |
|
5153 |
||
5154 |
" |
|
8874 | 5155 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5156 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5157 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5158 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5159 |
" |
5160 |
||
5161 |
! |
|
5162 |
||
602 | 5163 |
keys |
5164 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5165 |
||
5166 |
|sz| |
|
5167 |
||
1 | 5168 |
sz := self size. |
602 | 5169 |
sz == 0 ifTrue:[ |
8874 | 5170 |
^ #() |
61 | 5171 |
]. |
602 | 5172 |
^ 1 to:sz |
1 | 5173 |
! |
5174 |
||
602 | 5175 |
lastIndex |
5176 |
"return the last elements index" |
|
5177 |
||
5178 |
^ self size |
|
61 | 5179 |
! |
5180 |
||
602 | 5181 |
size |
5182 |
"return the number of elements in the collection. |
|
5183 |
concrete implementations must define this" |
|
5184 |
||
5185 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5186 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5187 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5188 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5189 |
"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
|
5190 |
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
|
5191 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5192 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5193 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5194 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5195 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5196 |
! ! |
5197 |
||
5198 |
!SequenceableCollection methodsFor:'searching'! |
|
5199 |
||
3887 | 5200 |
detect:aBlock startingAt:startIndex |
5201 |
"find the first element, for which evaluation of the argument, aBlock |
|
5202 |
return true, starting the search with startIndex. |
|
5203 |
If none does so, report an error" |
|
5204 |
||
5205 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5206 |
||
5207 |
" |
|
8874 | 5208 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5209 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5210 |
" |
5211 |
||
5212 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5213 |
! |
|
5214 |
||
5215 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5216 |
"find the first element, for which evaluation of the argument, aBlock |
|
5217 |
return true, starting the search with startIndex. |
|
5717 | 5218 |
If none does so, return the evaluation of exceptionBlock" |
5219 |
||
5220 |
|stop| |
|
3887 | 5221 |
|
5222 |
stop := self size. |
|
5717 | 5223 |
self from:startIndex to:stop do:[:el | |
8874 | 5224 |
(aBlock value:el) ifTrue:[ |
5225 |
^ el |
|
5226 |
]. |
|
3887 | 5227 |
]. |
5228 |
^ exceptionBlock value |
|
5229 |
||
5230 |
" |
|
8874 | 5231 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5232 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5233 |
" |
5234 |
||
5235 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5236 |
! |
|
5237 |
||
602 | 5238 |
findFirst:aBlock |
10453 | 5239 |
"find the index of the first element, for which evaluation of the argument, aBlock |
3887 | 5240 |
returns true; return its index or 0 if none detected. |
5241 |
This is much like #detect, however, here an INDEX is returned, |
|
5242 |
while #detect returns the element." |
|
602 | 5243 |
|
5244 |
|stop "{ Class: SmallInteger }" | |
|
5245 |
||
32 | 5246 |
stop := self size. |
602 | 5247 |
1 to:stop do:[:index | |
10453 | 5248 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5249 |
]. |
5250 |
^ 0 |
|
159 | 5251 |
|
95 | 5252 |
" |
602 | 5253 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] |
5254 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3) and:[x even]] |
|
8874 | 5255 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 8)] |
602 | 5256 |
'one.two.three' findFirst:[:c | (c == $.)] |
8874 | 5257 |
'__one.two.three' findFirst:[:c | (c ~= $_)] |
5258 |
'one.two.three' findFirst:[:c | (c ~= $_)] |
|
95 | 5259 |
" |
3887 | 5260 |
|
5261 |
"Modified: / 21.10.1998 / 18:48:10 / cg" |
|
1 | 5262 |
! |
5263 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5264 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5265 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5266 |
returns true; return its index or 0 if none detected. |
5267 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5268 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5269 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5270 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5271 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5272 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5273 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5274 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5275 |
start to:stop do:[:index | |
8874 | 5276 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5277 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5278 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5279 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5280 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5281 |
#(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
|
5282 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5283 |
" |
3887 | 5284 |
|
5285 |
"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
|
5286 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5287 |
|
602 | 5288 |
findLast:aBlock |
5289 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5290 |
returns true. |
5291 |
Return its index or 0 if none detected." |
|
602 | 5292 |
|
5293 |
|start "{ Class: SmallInteger }"| |
|
5294 |
||
5295 |
start := self size. |
|
5296 |
start to:1 by:-1 do:[:index | |
|
8874 | 5297 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5298 |
]. |
5299 |
^ 0 |
|
5300 |
||
5301 |
" |
|
602 | 5302 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5303 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5304 |
" |
5305 |
! |
|
5306 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5307 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5308 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5309 |
returns true. Start the search at startIndex. |
5310 |
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
|
5311 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5312 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5313 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5314 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5315 |
start to:1 by:-1 do:[:index | |
8874 | 5316 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
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 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5319 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5320 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5321 |
#(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
|
5322 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
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 |
|
8193 | 5326 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
5327 |
"find the last element, for which evaluation of the argument, aBlock |
|
5328 |
returns true. Start the search at startIndex. |
|
5329 |
End the search at endIndex or when an element is found. |
|
5330 |
Return its index or 0 if none detected." |
|
5331 |
||
5332 |
|start "{ Class: SmallInteger }"| |
|
5333 |
||
5334 |
start := startIndex. |
|
5335 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 5336 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 5337 |
]. |
5338 |
^ 0 |
|
5339 |
||
5340 |
" |
|
5341 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
5342 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
5343 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
5344 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
5345 |
" |
|
5346 |
! |
|
5347 |
||
159 | 5348 |
indexOfAny:aCollection |
5349 |
"search the collection for an element in aCollection. |
|
5350 |
if found, return the index otherwise return 0. |
|
8874 | 5351 |
The comparison is done using = |
159 | 5352 |
(i.e. equality test - not identity test). |
5353 |
||
5354 |
Notice, that for big collections, the runtime of this search |
|
5355 |
grows proportional to size(receiver) * size(aCollection). |
|
5356 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5357 |
||
5358 |
^ self indexOfAny:aCollection startingAt:1 |
|
5359 |
||
5360 |
" |
|
5361 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
5362 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
5363 |
" |
|
5364 |
! |
|
5365 |
||
5366 |
indexOfAny:aCollection startingAt:start |
|
5367 |
"search the collection for an element in aCollection, |
|
5368 |
starting the search at index start; |
|
5369 |
if found, return the index otherwise return 0. |
|
8874 | 5370 |
The comparison is done using = |
159 | 5371 |
(i.e. equality test - not identity test). |
5372 |
||
5373 |
Notice, that for big collections, the runtime of this search |
|
5374 |
grows proportional to size(receiver) * size(aCollection). |
|
5375 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5376 |
||
5377 |
||
5378 |
|startIndex "{ Class: SmallInteger }" |
|
5379 |
stop "{ Class: SmallInteger }" | |
|
5380 |
||
5381 |
startIndex := start. |
|
5382 |
stop := self size. |
|
5383 |
startIndex to:stop do:[:index | |
|
8874 | 5384 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 5385 |
]. |
5386 |
^ 0 |
|
5387 |
" |
|
8874 | 5388 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
5389 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 5390 |
" |
1 | 5391 |
! |
5392 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5393 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5394 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5395 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5396 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 5397 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5398 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5399 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5400 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5401 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5402 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
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 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5405 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5406 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5407 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5408 |
val == 0 ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 5412 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5413 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
5414 |
#(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
|
5415 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5416 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5417 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5418 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5419 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5420 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5421 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5422 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5423 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5424 |
#(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
|
5425 |
#(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
|
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 |
|
8874 | 5429 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5430 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
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 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5433 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5434 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5435 |
#(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
|
5436 |
#(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
|
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 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5440 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5441 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
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 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5444 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5445 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5446 |
|
368 | 5447 |
indexOfSubCollection:aCollection |
9234 | 5448 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 5449 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5450 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 5451 |
|
5452 |
" |
|
8874 | 5453 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
5454 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
5455 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
5456 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
5457 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
5458 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
5459 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
5460 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 5461 |
" |
5462 |
! |
|
5463 |
||
602 | 5464 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
5465 |
"find a subcollection. If found, return the index; |
|
5466 |
if not found, return the result of evaluating exceptionBlock." |
|
5467 |
||
5468 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
5469 |
! |
|
5470 |
||
368 | 5471 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5472 |
"find a subcollection starting at index. |
368 | 5473 |
If found, return the index; if not found, return 0." |
5474 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5475 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 5476 |
! |
5477 |
||
5478 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
5479 |
"find a subcollection, starting at index. If found, return the index; |
|
5480 |
if not found, return the result of evaluating exceptionBlock. |
|
5481 |
This is a q&d hack - not very efficient" |
|
5482 |
||
8874 | 5483 |
|same first |
368 | 5484 |
mySize "{Class: SmallInteger }" |
5485 |
checkIndex "{Class: SmallInteger }" |
|
5486 |
cmpIndex "{Class: SmallInteger }" |
|
5487 |
sz "{Class: SmallInteger }"| |
|
5488 |
||
5489 |
mySize := self size. |
|
5490 |
sz := aCollection size. |
|
5491 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
5492 |
first := aCollection at:1. |
|
5493 |
||
8874 | 5494 |
checkIndex := startIndex - 1. |
368 | 5495 |
[true] whileTrue:[ |
8874 | 5496 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
5497 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5498 |
||
5499 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
5500 |
^ 0 |
|
5501 |
]. |
|
5502 |
same := true. |
|
5503 |
cmpIndex := 1. |
|
5504 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5505 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
5506 |
same := false |
|
5507 |
] ifTrue:[ |
|
5508 |
cmpIndex := cmpIndex + 1 |
|
5509 |
] |
|
5510 |
]. |
|
5511 |
same ifTrue:[^ checkIndex]. |
|
5512 |
] |
|
5513 |
||
5514 |
" |
|
5515 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
5516 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
5517 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
5518 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
5519 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
5520 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 5521 |
" |
602 | 5522 |
! |
5523 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5524 |
lastIndexOfSubCollection:aCollection |
8874 | 5525 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5526 |
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
|
5527 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5528 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5529 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5530 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5531 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5532 |
! |
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 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5535 |
"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
|
5536 |
if not found, return the result of evaluating exceptionBlock." |
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 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5539 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5540 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5541 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5542 |
! |
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 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5545 |
"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
|
5546 |
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
|
5547 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5548 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5549 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5550 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
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 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5553 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5554 |
"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
|
5555 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5556 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5557 |
|
8874 | 5558 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5559 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5560 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5561 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5562 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5563 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5564 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5565 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5566 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5567 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5568 |
|
8874 | 5569 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5570 |
checkIndex > mySize ifTrue:[ |
8874 | 5571 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5572 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5573 |
[true] whileTrue:[ |
8874 | 5574 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
5575 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5576 |
||
5577 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
5578 |
^ 0 |
|
5579 |
]. |
|
5580 |
same := true. |
|
5581 |
cmpIndex := 1. |
|
5582 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5583 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
5584 |
same := false |
|
5585 |
] ifTrue:[ |
|
5586 |
cmpIndex := cmpIndex + 1 |
|
5587 |
] |
|
5588 |
]. |
|
5589 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
5590 |
] |
|
5591 |
||
5592 |
" |
|
5593 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
5594 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
5595 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
5596 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
5597 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
5598 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
5599 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
5600 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
5601 |
#(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
|
5602 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5603 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5604 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5605 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5606 |
|
7264 | 5607 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5608 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5609 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5610 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5611 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5612 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5613 |
|
10194 | 5614 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5615 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5616 |
" |
8874 | 5617 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5618 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5619 |
returns: true - if found |
10194 | 5620 |
false - if not found |
8874 | 5621 |
" |
5622 |
||
5623 |
" |
|
5624 |
#(10 20 30 40 50 60 70) includes:99 |
|
5625 |
#(10 20 30 40 50 60 70) includes:40 |
|
5626 |
#(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
|
5627 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5628 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5629 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5630 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
5631 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5632 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5633 |
"search the collection for anElement; |
602 | 5634 |
if found, return the index otherwise return 0. |
8874 | 5635 |
The comparison is done using = |
602 | 5636 |
(i.e. equality test - not identity test)." |
5637 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5638 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5639 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5640 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5641 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5642 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5643 |
returns: elementIndex - if found |
8874 | 5644 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5645 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5648 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5649 |
#(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
|
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 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5653 |
! |
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 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5656 |
"search the collection for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5657 |
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
|
5658 |
exceptionBlock. |
8874 | 5659 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5660 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5661 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5662 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5663 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5664 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5665 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5669 |
args: anElement : <object> |
8874 | 5670 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 5673 |
exceptionBlock value - if not found |
5674 |
" |
|
5675 |
||
5676 |
" |
|
5677 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
5678 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
5679 |
#(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
|
5680 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5681 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5682 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5683 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5684 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5685 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 5686 |
"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
|
5687 |
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
|
5688 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5689 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5690 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5691 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5692 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5693 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5694 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 5695 |
self at:idx put:replacement. |
5696 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5697 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5698 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5699 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5700 |
" |
8874 | 5701 |
args: elementToFind : <object> |
5702 |
replacement : <integer> |
|
5703 |
start : <integer> |
|
5704 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5705 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5706 |
returns: elementIndex - if found (and replaced) |
8874 | 5707 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5708 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5711 |
|a| |
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 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5714 |
(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
|
5715 |
a printNL. |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5718 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5719 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5720 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5721 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5722 |
"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
|
5723 |
if found, return the index otherwise return 0. |
8874 | 5724 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5725 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5726 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5727 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5728 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5729 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5730 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5731 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5732 |
startIndex to:stop do:[:index | |
8874 | 5733 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5734 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5735 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5736 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5737 |
" |
8874 | 5738 |
args: anElement : <object> |
5739 |
start : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 5742 |
0 - if not found |
5743 |
" |
|
5744 |
||
5745 |
" |
|
5746 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
5747 |
#(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
|
5748 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5749 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5750 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5751 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5752 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5753 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5754 |
"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
|
5755 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5756 |
If found (within the range), return the index, otherwise return 0. |
8874 | 5757 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5758 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5759 |
|
602 | 5760 |
|startIndex "{ Class: SmallInteger }" |
5761 |
stopIndex "{ Class: SmallInteger }" | |
|
5762 |
||
5763 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5764 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5765 |
startIndex to:stopIndex do:[:index | |
8874 | 5766 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5767 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5768 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5769 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5770 |
" |
8874 | 5771 |
args: anElement : <object> |
5772 |
start : <integer> |
|
5773 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5774 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5775 |
returns: elementIndex - if found |
8874 | 5776 |
exceptionBlock value - if not found |
5777 |
" |
|
5778 |
||
5779 |
" |
|
5780 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
5781 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
5782 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
5783 |
#(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
|
5784 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5785 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5786 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5787 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
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 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5791 |
"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
|
5792 |
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
|
5793 |
exceptionBlock. |
8874 | 5794 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5795 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5796 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5797 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5798 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5799 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5800 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 5804 |
args: anElement : <object> |
5805 |
start : <integer> |
|
5806 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5807 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5808 |
returns: elementIndex - if found |
8874 | 5809 |
exceptionBlock value - if not found |
5810 |
" |
|
5811 |
||
5812 |
" |
|
5813 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
5814 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
5815 |
#(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
|
5816 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5817 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5818 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5819 |
! |
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 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5822 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5823 |
if found, return the index otherwise return 0. |
8874 | 5824 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5825 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5826 |
|
8874 | 5827 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5828 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5829 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5830 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5831 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5832 |
returns: elementIndex - if found |
8874 | 5833 |
0 - if not found |
5834 |
" |
|
5835 |
||
5836 |
" |
|
5837 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
5838 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
5839 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
5840 |
#(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
|
5841 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5842 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5843 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5844 |
! |
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 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5847 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5848 |
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
|
5849 |
exceptionBlock. |
8874 | 5850 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5851 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5852 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5853 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5854 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5855 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5856 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 5860 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
5861 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
5862 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
5863 |
#(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
|
5864 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5865 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5866 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5867 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5868 |
"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
|
5869 |
if found, return the index otherwise return 0. |
8874 | 5870 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5871 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5872 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5873 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5874 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5875 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5876 |
startIndex to:1 by:-1 do:[:index | |
8874 | 5877 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 5878 |
]. |
5879 |
^ 0 |
|
5880 |
||
5881 |
" |
|
8874 | 5882 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
5883 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
5884 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
5885 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
5886 |
#(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
|
5887 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5888 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5889 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5890 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 5891 |
"search the collection backwards for anElement starting the search at |
5892 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5893 |
otherwise return the value of the exceptionBlock. |
8874 | 5894 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5895 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5896 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5897 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5898 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5899 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5900 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
8874 | 5904 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
5905 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
5906 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
5907 |
#(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
|
5908 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5909 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5910 |
|
4788 | 5911 |
lastIndexOfAny:aCollection |
5912 |
"search the collection backwards for any in aCollection; |
|
5913 |
if found, return the index, otherwise return 0. |
|
8874 | 5914 |
The comparison is done using = |
4788 | 5915 |
(i.e. equality test - not identity test)." |
5916 |
||
8874 | 5917 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 5918 |
|
5919 |
" |
|
5920 |
args: aCollection : <collection> |
|
5921 |
||
5922 |
returns: elementIndex - if found |
|
8874 | 5923 |
0 - if not found |
5924 |
" |
|
5925 |
||
5926 |
" |
|
5927 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 5928 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 5929 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
5930 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 5931 |
" |
5932 |
||
5933 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
5934 |
! |
|
5935 |
||
5936 |
lastIndexOfAny:aCollection startingAt:start |
|
5937 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
5938 |
if found, return the index, otherwise return 0. |
|
8874 | 5939 |
The comparison is done using = |
4788 | 5940 |
(i.e. equality test - not identity test)." |
5941 |
||
5942 |
|startIndex "{ Class: SmallInteger }"| |
|
5943 |
||
5944 |
startIndex := start. |
|
5945 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 5946 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 5947 |
]. |
5948 |
^ 0 |
|
5949 |
||
5950 |
" |
|
8874 | 5951 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
5952 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 5953 |
" |
5954 |
! |
|
5955 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5956 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5957 |
"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
|
5958 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5959 |
if found, return the index otherwise return nil. |
8874 | 5960 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5961 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5962 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5963 |
NOTICE: nil is returned here if the element is not found, |
8874 | 5964 |
although this is somewhat strange (the indexOf-search |
5965 |
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
|
5966 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5967 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5968 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5969 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5970 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5971 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5972 |
startIndex to:stop do:[:index | |
8874 | 5973 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5974 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5975 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5976 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5977 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5978 |
args: anElement : <object> |
8874 | 5979 |
start : <integer> |
5980 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 5983 |
nil - if not found |
5984 |
" |
|
5985 |
||
5986 |
" |
|
5987 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
5988 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
5989 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 5990 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5991 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5992 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 5993 |
! |
5994 |
||
5995 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
5996 |
"search the collection for anElement, starting the search at index start |
|
5997 |
and stopping at stop; |
|
5998 |
if found, return the index otherwise return the value of the |
|
5999 |
exceptionBlock. |
|
8874 | 6000 |
The comparison is done using = |
602 | 6001 |
(i.e. equality test - not identity test)." |
6002 |
||
6003 |
|index| |
|
6004 |
||
6005 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6006 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 6007 |
^ index |
6008 |
||
6009 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6010 |
args: anElement : <object> |
8874 | 6011 |
start : <integer> |
6012 |
stop : <integer> |
|
6013 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6014 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6015 |
returns: elementIndex - if found |
8874 | 6016 |
valueImplementor value - if not found |
6017 |
" |
|
6018 |
||
6019 |
" |
|
6020 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6021 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6022 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6023 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6024 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6025 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 6026 |
! |
6027 |
||
6028 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6029 |
"search the collection for anElement, starting the search at index start; |
|
6030 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6031 |
If found (within the range), return the index, otherwise return nil. |
8874 | 6032 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6033 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6034 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6035 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6036 |
although this is somewhat strange (the indexOf-search |
6037 |
methods return 0), this is done for VW compatibility." |
|
2275 | 6038 |
|
6039 |
|startIndex "{ Class: SmallInteger }" |
|
6040 |
stopIndex "{ Class: SmallInteger }" | |
|
6041 |
||
6042 |
startIndex := startSearchIndex. |
|
6043 |
stopIndex := endSearchIndex max:1. |
|
6044 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 6045 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6046 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6047 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6048 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6049 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6050 |
args: anElement : <object> |
8874 | 6051 |
start : <integer> |
6052 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6055 |
nil - if not found |
6056 |
" |
|
6057 |
||
6058 |
" |
|
6059 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6060 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6061 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6062 |
" |
6063 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6064 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6065 |
! |
6066 |
||
6067 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6068 |
"search the collection for anElement, starting the search at index start |
|
6069 |
and stopping at stop, doing a reverse search; |
|
6070 |
if found, return the index otherwise return the value of the |
|
6071 |
exceptionBlock. |
|
8874 | 6072 |
The comparison is done using = |
2275 | 6073 |
(i.e. equality test - not identity test)." |
6074 |
||
6075 |
|index| |
|
6076 |
||
6077 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6078 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6079 |
^ index |
|
6080 |
||
6081 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6082 |
args: anElement : <object> |
8874 | 6083 |
start : <integer> |
6084 |
stop : <integer> |
|
6085 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6086 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6087 |
returns: elementIndex - if found |
8874 | 6088 |
valueImplementor value - if not found |
6089 |
" |
|
6090 |
||
6091 |
" |
|
6092 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6093 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6094 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6095 |
" |
6096 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6097 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6098 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6099 |
|
7264 | 6100 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6101 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6102 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6103 |
"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
|
6104 |
if found, return the index otherwise return 0." |
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 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6107 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6108 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6109 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6110 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6111 |
returns: elementIndex - if found |
8874 | 6112 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6113 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6114 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6115 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6116 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6117 |
#(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
|
6118 |
#(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
|
6119 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6120 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6121 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6122 |
#(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
|
6123 |
#(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
|
6124 |
|
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6127 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
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 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6131 |
"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
|
6132 |
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
|
6133 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6134 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6135 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6136 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6137 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6138 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6142 |
args: anElement : <object> |
8874 | 6143 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 6146 |
exceptionBlock value - if not found |
6147 |
" |
|
6148 |
||
6149 |
" |
|
6150 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6151 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6152 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6153 |
#(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
|
6154 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6155 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6156 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6157 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6158 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6159 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6160 |
"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
|
6161 |
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
|
6162 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6163 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6164 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6165 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6166 |
args: anElement : <object> |
8874 | 6167 |
anotherElement : <object> |
5520
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 |
returns: elementIndex - if found |
8874 | 6170 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6171 |
" |
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 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6174 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6175 |
"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
|
6176 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6177 |
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
|
6178 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6179 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6180 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6181 |
el | |
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 := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6184 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6185 |
startIndex to:stop do:[:index | |
8874 | 6186 |
el := self at:index. |
6187 |
anElement == el ifTrue:[^ index]. |
|
6188 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6189 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6190 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6191 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6192 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6193 |
args: anElement : <object> |
8874 | 6194 |
anotherElement : <object> |
6195 |
start : <integer> |
|
5520
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 |
returns: elementIndex - if found |
8874 | 6198 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6199 |
" |
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 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6202 |
#(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
|
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 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6206 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6207 |
"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
|
6208 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6209 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6210 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6211 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6212 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6213 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6214 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6215 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6216 |
startIndex to:stop do:[:index | |
8874 | 6217 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6218 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6219 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6220 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6221 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6222 |
args: anElement : <object> |
8874 | 6223 |
start : <integer> |
3468
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 |
returns: elementIndex - if found |
8874 | 6226 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6227 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6228 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6229 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6230 |
#(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
|
6231 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6232 |
#(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
|
6233 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6234 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6235 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6236 |
! |
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 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6239 |
"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
|
6240 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6241 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6242 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6243 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6244 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6245 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6246 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6247 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6248 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6249 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6250 |
startIndex to:stopIndex do:[:index | |
8874 | 6251 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6252 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6253 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6254 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6255 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6256 |
args: anElement : <object> |
8874 | 6257 |
start : <integer> |
6258 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6261 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6262 |
" |
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 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6265 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
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 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6269 |
"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
|
6270 |
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
|
6271 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6272 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6273 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6274 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6275 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6276 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6277 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6281 |
args: anElement : <object> |
8874 | 6282 |
start : <integer> |
6283 |
exceptionBlock : <valueImplementor> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6286 |
exceptionBlock value - if not found |
6287 |
" |
|
6288 |
||
6289 |
" |
|
6290 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6291 |
#(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
|
6292 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6293 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6294 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6295 |
! |
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 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6298 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6299 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6300 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6301 |
|
10194 | 6302 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6303 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6304 |
" |
8874 | 6305 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6306 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6307 |
returns: true - if found |
10194 | 6308 |
false - if not found |
8874 | 6309 |
" |
6310 |
||
6311 |
" |
|
6312 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6313 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6314 |
#(10 20 30 40 50 60 70) includes:40 |
|
6315 |
#(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
|
6316 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6317 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6318 |
|
8874 | 6319 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6320 |
#(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
|
6321 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6322 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6323 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6324 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6325 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6326 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6327 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6328 |
if found, return the index otherwise return 0. |
8874 | 6329 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6331 |
|
8874 | 6332 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6333 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6335 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6336 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6337 |
returns: elementIndex - if found |
8874 | 6338 |
0 - if not found |
6339 |
" |
|
6340 |
||
6341 |
" |
|
6342 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
6343 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
6344 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
6345 |
#(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
|
6346 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6347 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6348 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6349 |
! |
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 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6352 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6353 |
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
|
6354 |
exceptionBlock. |
8874 | 6355 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6356 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6357 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6358 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6359 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6360 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6361 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6365 |
args: anElement : <object> |
8874 | 6366 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 6369 |
exceptionBlock value - if not found |
6370 |
" |
|
6371 |
||
6372 |
" |
|
6373 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
6374 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
6375 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
6376 |
#(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
|
6377 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6378 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6379 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6380 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
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 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6384 |
"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
|
6385 |
if found, return the index otherwise return 0. |
8874 | 6386 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6387 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6388 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6389 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6390 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6391 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6392 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6393 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6396 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6397 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6398 |
args: anElement : <object> |
8874 | 6399 |
start : <integer> |
3468
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 |
returns: elementIndex - if found |
8874 | 6402 |
0 - if not found |
6403 |
" |
|
6404 |
||
6405 |
" |
|
6406 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
6407 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
6408 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
6409 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
6410 |
#(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
|
6411 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6412 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6413 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6414 |
! |
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 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6417 |
"search the collection backwards for anElement starting the search at |
6418 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6419 |
otherwise return the value of the exceptionBlock. |
8874 | 6420 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6421 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6422 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6423 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6424 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6425 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6426 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6430 |
args: anElement : <object> |
8874 | 6431 |
start : <integer> |
6432 |
exceptionBlock : <valueImplementor> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6435 |
exceptionBlock value - if not found |
6436 |
" |
|
6437 |
||
6438 |
" |
|
6439 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
6440 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6441 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6442 |
#(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
|
6443 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6444 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6445 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6446 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 6447 |
! ! |
6448 |
||
6449 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
6450 |
||
6958 | 6451 |
detectFirstInOrder:sortBlock |
6452 |
"find the first element of the collection sorted with sortBlock" |
|
6453 |
||
6454 |
|selectedForNow start| |
|
6455 |
||
6456 |
start := true. |
|
6457 |
self do:[:element| |
|
8874 | 6458 |
start ifTrue:[ |
6459 |
start := false. |
|
6460 |
selectedForNow := element. |
|
6461 |
] ifFalse:[ |
|
6462 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
6463 |
selectedForNow := element. |
|
6464 |
]. |
|
6465 |
]. |
|
6958 | 6466 |
]. |
6467 |
||
6468 |
^ selectedForNow |
|
6469 |
||
6470 |
" |
|
8874 | 6471 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
6472 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
6473 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
6474 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 6475 |
" |
6476 |
! |
|
6477 |
||
5358 | 6478 |
mergeSort |
8874 | 6479 |
"sort the collection using a mergeSort algorithm. |
6480 |
The elements are compared using'<' |
|
5358 | 6481 |
i.e. they should offer a magnitude-like protocol. |
6482 |
||
6483 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 6484 |
the best possible for all situations |
5358 | 6485 |
See also #quickSort and #randomizedSort for other sort algorithms |
6486 |
with different worst- and average case behavior)" |
|
6487 |
||
6488 |
|stop| |
|
6489 |
||
6490 |
stop := self size. |
|
6491 |
(stop > 1) ifTrue:[ |
|
8874 | 6492 |
self mergeSort:[:a :b | a < b] from:1 to:stop |
5358 | 6493 |
] |
6494 |
||
6495 |
" |
|
6496 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
6497 |
||
6498 |
|data| |
|
6499 |
data := Random new next:100000. |
|
6500 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
6501 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 6502 |
data reverse. |
5358 | 6503 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
6504 |
||
6505 |
data := Random new next:100000. |
|
6506 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6507 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6508 |
data reverse. |
5358 | 6509 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6510 |
||
6511 |
data := Random new next:100000. |
|
6512 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
6513 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 6514 |
data reverse. |
5358 | 6515 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
6516 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6517 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6518 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6519 |
mergeSort:sortBlock |
8874 | 6520 |
"sort the collection using a mergeSort algorithm. |
6521 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6522 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6523 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6524 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 6525 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6526 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6527 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6528 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6529 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6530 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6531 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6532 |
(stop > 1) ifTrue:[ |
8874 | 6533 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6534 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6535 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6536 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6537 |
#(1 16 7 98 3 19 4 0) mergeSort |
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 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6540 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6541 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6542 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6543 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6544 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6545 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6546 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6547 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6548 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 6549 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6550 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6551 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6552 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6553 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6554 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 6555 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6556 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6557 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6558 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6559 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6560 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 6561 |
"Sort the given range of indices using the mergesort algorithm. |
6562 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 6563 |
as many comparisons as heapsort or quicksort." |
6564 |
||
8874 | 6565 |
"Details: recursively split the range to be sorted into two halves, |
6566 |
mergesort each half, then merge the two halves together. |
|
6567 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
6568 |
and forth between the receiver and this copy. |
|
6569 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 6570 |
resulting in the receiver being completely sorted." |
6571 |
||
6572 |
| temp | |
|
6573 |
||
6574 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
6575 |
startIndex = stopIndex ifTrue: [^ self]. |
|
6576 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 6577 |
ifFalse: [self error: 'bad start index']. |
5358 | 6578 |
stopIndex <= self size |
8874 | 6579 |
ifFalse: [self error: 'bad stop index']. |
5358 | 6580 |
temp := self clone. |
6581 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
6582 |
||
6583 |
" |
|
8874 | 6584 |
#(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
|
6585 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6586 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6587 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6588 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6589 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6590 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6591 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 6592 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6593 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6594 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6595 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6596 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6597 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6598 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6599 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 6600 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6601 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6602 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6603 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6604 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6605 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6606 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6607 |
'<' i.e. they should offer a magnitude-like protocol. |
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 |
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
|
6610 |
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
|
6611 |
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
|
6612 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6613 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6614 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6615 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6616 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6617 |
(stop > 1) ifTrue:[ |
8874 | 6618 |
self quickSortFrom:1 to:stop |
7636
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6621 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6622 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 6623 |
|
6624 |
|data| |
|
6625 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6626 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6627 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 6628 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6629 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6630 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6631 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6632 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6633 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6634 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6635 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6636 |
"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
|
6637 |
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
|
6638 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6639 |
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
|
6640 |
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
|
6641 |
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
|
6642 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6643 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6644 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6645 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6646 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6647 |
(stop > 1) ifTrue:[ |
8874 | 6648 |
sortBlock numArgs == 3 ifTrue:[ |
6649 |
"/ TODO: pass a collating policy to aBlock |
|
6650 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
6651 |
] ifFalse:[ |
|
6652 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
6653 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6654 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6655 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6656 |
" |
8874 | 6657 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6658 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6659 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6660 |
#(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
|
6661 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6662 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6663 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6664 |
! |
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 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6667 |
"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
|
6668 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6669 |
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
|
6670 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6671 |
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
|
6672 |
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
|
6673 |
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
|
6674 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6675 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6676 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6677 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6678 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6679 |
(stop > 1) ifTrue:[ |
8874 | 6680 |
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
|
6681 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6682 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6683 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6684 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6685 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6686 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6687 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6688 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6689 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6690 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6691 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6692 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6693 |
! |
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 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6696 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6697 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6698 |
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
|
6699 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6700 |
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
|
6701 |
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
|
6702 |
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
|
6703 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6704 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6705 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6706 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6707 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6708 |
(stop > 1) ifTrue:[ |
8874 | 6709 |
self quickSortFrom:1 to:stop with:aCollection |
7636
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6712 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 6713 |
! |
6714 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6715 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6716 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6717 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6718 |
|
8874 | 6719 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6720 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6721 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6722 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6723 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6724 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6725 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6726 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6727 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6728 |
"/ 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
|
6729 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6730 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6731 |
"/ 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
|
6732 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6733 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6734 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6735 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6736 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6737 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6738 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6739 |
|
8874 | 6740 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6741 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6742 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6743 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6744 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6745 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6746 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6747 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6748 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6749 |
"/ 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
|
6750 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6751 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6752 |
"/ 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
|
6753 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6754 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6755 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6756 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6757 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6758 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6759 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6760 |
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
|
6761 |
|
8874 | 6762 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6763 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6764 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6765 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6766 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6767 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6768 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6769 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6770 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6771 |
"/ 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
|
6772 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6773 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6774 |
"/ 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
|
6775 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6776 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6777 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6778 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6779 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6780 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6781 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6782 |
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
|
6783 |
|
8874 | 6784 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6785 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6786 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6787 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6788 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6789 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6790 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6791 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6792 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6793 |
"/ 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
|
6794 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6795 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6796 |
"/ 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
|
6797 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6798 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6799 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6800 |
|
602 | 6801 |
reverse |
7335 | 6802 |
"destructively reverse the order of the elements inplace. |
6803 |
WARNING: this is a destructive operation, which modifies the receiver." |
|
602 | 6804 |
|
6805 |
|lowIndex "{ Class: SmallInteger }" |
|
6806 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6807 |
t1 t2| |
602 | 6808 |
|
6809 |
hiIndex := self size. |
|
6810 |
lowIndex := 1. |
|
6811 |
[lowIndex < hiIndex] whileTrue:[ |
|
8874 | 6812 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
6813 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
6814 |
||
6815 |
lowIndex := lowIndex + 1. |
|
6816 |
hiIndex := hiIndex - 1 |
|
602 | 6817 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6818 |
|
69 | 6819 |
" |
602 | 6820 |
#(4 5 6 7 7) reverse |
6821 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 6822 |
" |
1 | 6823 |
! |
6824 |
||
5004 | 6825 |
reversed |
6826 |
"return a copy with elements in reverse order" |
|
6827 |
||
6828 |
^ self copy reverse |
|
6829 |
||
6830 |
" |
|
8874 | 6831 |
#(4 5 6 7 7) reversed |
5004 | 6832 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
6833 |
" |
|
6834 |
! |
|
6835 |
||
1 | 6836 |
sort |
6837 |
"sort the collection inplace. The elements are compared using |
|
2069 | 6838 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6839 |
|
159 | 6840 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6841 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6842 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6843 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 6844 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6845 |
self quickSort |
1 | 6846 |
|
95 | 6847 |
" |
6848 |
#(1 16 7 98 3 19 4 0) sort |
|
6849 |
||
6850 |
|data| |
|
6851 |
data := Random new next:100000. |
|
6852 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6853 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6854 |
data reverse. |
95 | 6855 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6856 |
" |
|
2069 | 6857 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6858 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 6859 |
! |
6860 |
||
6861 |
sort:sortBlock |
|
6862 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6863 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6864 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6865 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6866 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6867 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6868 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6869 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6870 |
self quickSort:sortBlock |
1 | 6871 |
|
95 | 6872 |
" |
8874 | 6873 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
6874 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
6875 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
6876 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 6877 |
" |
2069 | 6878 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
6879 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 6880 |
! |
6881 |
||
6882 |
sort:sortBlock with:aCollection |
|
6883 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 6884 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6885 |
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
|
6886 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6887 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6888 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6889 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6890 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 6891 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6892 |
self quickSort:sortBlock with:aCollection |
1 | 6893 |
|
69 | 6894 |
" |
6895 |
|c1 c2| |
|
1 | 6896 |
c1 := #(1 16 7 9). |
6897 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
6898 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 6899 |
c1 printNL. |
6900 |
c2 printNL |
|
69 | 6901 |
" |
2069 | 6902 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6903 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 6904 |
! |
6905 |
||
602 | 6906 |
sortWith:aCollection |
2069 | 6907 |
"sort the receiver collection inplace, using '<' to compare elements. |
6908 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6909 |
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
|
6910 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6911 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6912 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6913 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6914 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 6915 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
6916 |
self quickSortWith:aCollection |
424 | 6917 |
! |
6918 |
||
9072 | 6919 |
sortedBy: aBlock |
6920 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
|
6921 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
6922 |
||
6923 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
6924 |
! |
|
6925 |
||
602 | 6926 |
topologicalSort:sortBlock |
6927 |
"sort the collection inplace using a sloooow sort algorithm. |
|
6928 |
This algorithm has O-square runtime behavior and should be used only |
|
6929 |
in special situations. |
|
6930 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
6931 |
a>c (for example, to sort classes by inheritance) |
|
6932 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6933 |
In all other situations, use #sort; which implements the quicksort algorithm. |
77 | 6934 |
" |
602 | 6935 |
|
6936 |
|smallestIndex "{ Class: SmallInteger }" |
|
6937 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6938 |
index2 "{ Class: SmallInteger }" |
602 | 6939 |
smallest thisOne| |
6940 |
||
6941 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
6942 |
||
6943 |
end := self size. |
|
862 | 6944 |
1 to:end-1 do:[:index | |
8874 | 6945 |
smallest := self at:index. |
6946 |
smallestIndex := index. |
|
6947 |
||
6948 |
index2 := index + 1. |
|
6949 |
[ |
|
6950 |
thisOne := self at:index2. |
|
6951 |
(index2 ~~ smallestIndex |
|
6952 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
6953 |
smallestIndex := index2. |
|
6954 |
smallest := thisOne. |
|
6955 |
index2 := index + 1. |
|
6956 |
] ifFalse:[ |
|
6957 |
index2 := index2 + 1. |
|
6958 |
]. |
|
6959 |
] doWhile:[index2 <= end]. |
|
862 | 6960 |
|
6961 |
"/ (index + 1) to:end do:[:index2 | |
|
6962 |
"/ thisOne := self at:index2. |
|
6963 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
6964 |
"/ smallestIndex := index2. |
|
6965 |
"/ smallest := thisOne |
|
6966 |
"/ ]. |
|
6967 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
6968 |
|
8874 | 6969 |
(smallestIndex ~~ index) ifTrue:[ |
6970 |
thisOne := self at:index. |
|
6971 |
self at:index put:smallest. |
|
6972 |
self at:smallestIndex put:thisOne |
|
6973 |
]. |
|
1 | 6974 |
] |
95 | 6975 |
|
77 | 6976 |
" |
8874 | 6977 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
6978 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
6979 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
6980 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 6981 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6982 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
6983 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 6984 |
! ! |
602 | 6985 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6986 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6987 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6988 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6989 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6990 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 6991 |
however, having a common protocol with indexed collections |
6992 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6993 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6994 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6995 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6996 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
6997 |
" |
8874 | 6998 |
#(1 2 3) includesKey:4 |
6999 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7000 |
" |
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 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7003 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7004 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7005 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7006 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7007 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7008 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7009 |
|
2069 | 7010 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7011 |
|
7012 |
version |
|
10590 | 7013 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.247 2007-06-04 19:32:08 cg Exp $' |
628 | 7014 |
! ! |
6820 | 7015 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7016 |
SequenceableCollection initialize! |