author | Claus Gittinger <cg@exept.de> |
Sun, 23 Aug 2009 11:21:08 +0200 | |
changeset 11870 | 4b19d87d7335 |
parent 11855 | 8e32bf8bc50e |
child 11875 | 79f71cb2270b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
8874 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5358 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Collection subclass:#SequenceableCollection |
6183 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'MissingClassInLiteralArrayErrorSignal' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Collections-Abstract' |
|
1 | 19 |
! |
20 |
||
2069 | 21 |
!SequenceableCollection class methodsFor:'documentation'! |
88 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
8874 | 26 |
All Rights Reserved |
1 | 27 |
|
88 | 28 |
This software is furnished under a license and may be used |
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
1 | 36 |
|
88 | 37 |
documentation |
38 |
" |
|
11272 | 39 |
SequenceableCollections have ordered elements which can be accessed via a numeric index. |
40 |
SequenceableCollection is an abstract class - there are no direct instances of it in the system. |
|
1385 | 41 |
|
42 |
The methods found here assume that implementations of (at least) |
|
11272 | 43 |
#at:/#at:put: are implemented (which are always found in Object for indexable objects). |
44 |
||
45 |
For performance, some subclasses redefine more methods, |
|
46 |
knowing the detail of how elements are stored. |
|
47 |
||
8874 | 48 |
Especially, bulk data manipulation (i.e. #replaceFrom:to:with:startingAt:) |
11272 | 49 |
and search methods (i.e. #indexOf:startingAt:) are good candidates for this kind of tuneup. |
50 |
||
51 |
The most heavily used SequentialCollections in the system are probably Array, String, ByteArray |
|
52 |
OrderedCollection and SortedCollection. |
|
1289 | 53 |
|
54 |
[author:] |
|
11272 | 55 |
Claus Gittinger |
1385 | 56 |
|
57 |
[see also:] |
|
11272 | 58 |
Array ByteArray OrderedCollection SortedCollection |
59 |
CharacterArray String |
|
88 | 60 |
" |
61 |
! ! |
|
1 | 62 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
63 |
!SequenceableCollection class methodsFor:'initialization'! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
64 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
65 |
initialize |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
66 |
MissingClassInLiteralArrayErrorSignal isNil ifTrue:[ |
8874 | 67 |
MissingClassInLiteralArrayErrorSignal := Error newSignalMayProceed:true. |
68 |
MissingClassInLiteralArrayErrorSignal nameClass:self message:#missingClassInLiteralArrayErrorSignal. |
|
69 |
MissingClassInLiteralArrayErrorSignal notifierString:'Missing class in literal encoding'. |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
70 |
] |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
71 |
|
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
72 |
"Created: / 18.5.1999 / 14:49:51 / cg" |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
73 |
! ! |
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
74 |
|
2069 | 75 |
!SequenceableCollection class methodsFor:'instance creation'! |
1 | 76 |
|
4807 | 77 |
decodeFromLiteralArray:anArray |
78 |
"create & return a new instance from information encoded in anArray. |
|
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
79 |
Redefined for faster creation." |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
80 |
|
8874 | 81 |
|collection |
8831
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
82 |
sz "{ Class: SmallInteger }"| |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
83 |
|
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
84 |
sz := anArray size. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
85 |
collection := self withSize:sz-1. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
86 |
2 to:sz do:[:idx| collection at:idx-1 put:(anArray at:idx) decodeAsLiteralArray]. |
918f291920c2
Speed up #decodeFromLiteralArray:
Stefan Vogel <sv@exept.de>
parents:
8799
diff
changeset
|
87 |
^ collection |
4807 | 88 |
|
8797 | 89 |
" |
90 |
Array |
|
8874 | 91 |
decodeFromLiteralArray:#(Array 1 2 3) |
8797 | 92 |
" |
4807 | 93 |
! |
94 |
||
1 | 95 |
new:size withAll:element |
360 | 96 |
"return a new collection of size, where all elements are |
97 |
initialized to element." |
|
1 | 98 |
|
99 |
|newCollection| |
|
100 |
||
101 |
newCollection := self new:size. |
|
102 |
newCollection atAllPut:element. |
|
103 |
^ newCollection |
|
360 | 104 |
! |
105 |
||
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
106 |
newWithConcatenationOfAll:aCollectionOfCollections |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
107 |
"this creates and returns a new collection consisting |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
108 |
of the concatenation of all elements of the argument. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
109 |
I.e. it has the same effect as concatenating all elements |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
110 |
of the argument using #, ,but is implemented more efficiently, |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
111 |
by avoiding repeated garbage allocations. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
112 |
This is an O runtime algorithm, in contrast to the #, loop, which is O*O" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
113 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
114 |
|totalSize newColl idx| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
115 |
|
8874 | 116 |
totalSize := aCollectionOfCollections |
117 |
inject:0 |
|
118 |
into:[:sumSoFar :el | sumSoFar + el size]. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
119 |
newColl := self new:totalSize. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
120 |
idx := 1. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
121 |
aCollectionOfCollections do:[:el | |
8874 | 122 |
|sz| |
123 |
||
124 |
sz := el size. |
|
125 |
newColl replaceFrom:idx to:(idx+sz-1) with:el startingAt:1. |
|
126 |
idx := idx + sz |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
127 |
]. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
128 |
^ newColl |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
129 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
130 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
131 |
this method optimizes the following (common) operation: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
132 |
|
8874 | 133 |
|s| |
134 |
||
135 |
s := ''. |
|
136 |
#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') do:[:e| |
|
137 |
s := s , e. |
|
138 |
]. |
|
139 |
s |
|
140 |
||
141 |
String |
|
142 |
newWithConcatenationOfAll:#('hello' ' ' 'world' ' ' 'this is ' 'ST/X') |
|
143 |
||
144 |
String |
|
145 |
newWithConcatenationOfAll:#() |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
146 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
147 |
Array |
8874 | 148 |
newWithConcatenationOfAll:#( (1 2 3 4) (5 6 7 8) (9 10 11 12) ) |
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
149 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
150 |
timing: |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
151 |
------- |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
152 |
speed-break-even is at around 5 elements for strings; |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
153 |
for more elements, #newWithConcatenationOfAll: is much faster. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
154 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
155 |
|arr1 arr2 arr3 t| |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
156 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
157 |
arr1 := #('hello' ' ' 'world' ' ' 'this is ' 'ST/X'). |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
158 |
arr2 := Array new:1000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
159 |
arr3 := Array new:10000 withAll:'hello'. |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
160 |
|
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
161 |
(Array with:arr1 with:arr2 with:arr3) with:#(10000 100 10) do:[:arr :cnt | |
8874 | 162 |
t := Time millisecondsToRun:[ |
163 |
cnt timesRepeat:[ |
|
164 |
String |
|
165 |
newWithConcatenationOfAll:arr |
|
166 |
] |
|
167 |
]. |
|
168 |
Transcript showCR:(arr size printString , ' elements - time for #newWithConcatenationOfAll :' , (t/cnt) asFloat printString , 'mS'). |
|
169 |
Transcript endEntry. |
|
170 |
||
171 |
t := Time millisecondsToRun:[ |
|
172 |
cnt timesRepeat:[ |
|
173 |
|s| |
|
174 |
||
175 |
s := ''. |
|
176 |
arr do:[:e| |
|
177 |
s := s , e. |
|
178 |
]. |
|
179 |
s |
|
180 |
] |
|
181 |
]. |
|
182 |
Transcript showCR:(arr size printString , ' elements - time for loop over #, :' , (t/cnt) asFloat printString , 'mS'). |
|
183 |
Transcript endEntry. |
|
4303
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
184 |
] |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
185 |
" |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
186 |
! |
4a2e5a5cb83d
added #newWithConcatenationOfAll: for tune concatenation of
Claus Gittinger <cg@exept.de>
parents:
4197
diff
changeset
|
187 |
|
360 | 188 |
withSize:size |
189 |
"return a new collection of size. |
|
190 |
For variable size collections, this is different from #new:, |
|
191 |
in that #new: creates an empty collection with preallocated size, |
|
192 |
while #withSize: creates a non empty one." |
|
193 |
||
9162 | 194 |
^ (self new:size) grow:size. |
360 | 195 |
|
196 |
" |
|
197 |
(OrderedCollection new:10) inspect. |
|
198 |
(OrderedCollection withSize:10) inspect. |
|
199 |
(Array new:10) inspect. |
|
200 |
(Array withSize:10) inspect. |
|
201 |
" |
|
1 | 202 |
! ! |
203 |
||
7304 | 204 |
!SequenceableCollection class methodsFor:'Compatibility-Squeak'! |
4370 | 205 |
|
6820 | 206 |
writeStream |
207 |
"create a write-stream on an instance of the receiver-class" |
|
208 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
209 |
^ WriteStream on:(self new:50). |
8220 | 210 |
|
211 |
" |
|
11242 | 212 |
OrderedCollection writeStream |
8220 | 213 |
" |
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
214 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
215 |
"Modified: / 29-03-2007 / 15:20:02 / cg" |
4370 | 216 |
! ! |
217 |
||
5188 | 218 |
!SequenceableCollection class methodsFor:'Signal constants'! |
219 |
||
220 |
missingClassInLiteralArrayErrorSignal |
|
221 |
^ MissingClassInLiteralArrayErrorSignal |
|
222 |
||
223 |
"Created: / 18.5.1999 / 14:50:04 / cg" |
|
224 |
! ! |
|
225 |
||
7304 | 226 |
!SequenceableCollection class methodsFor:'instance creation-multiDimensional'! |
7255 | 227 |
|
228 |
_at:nIndices |
|
229 |
"this is a synthetic selector, generated by the compiler, |
|
230 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 231 |
I.e. |
232 |
Array[n] |
|
7255 | 233 |
generates |
8874 | 234 |
Array _at: n |
7255 | 235 |
" |
236 |
||
237 |
^ self new:nIndices |
|
238 |
! |
|
239 |
||
240 |
_at:dim1 at:dim2 |
|
241 |
"this is a synthetic selector, generated by the compiler, |
|
242 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 243 |
I.e. |
244 |
Array[n,m] |
|
7255 | 245 |
generates |
8874 | 246 |
Array _at:n at:m |
7255 | 247 |
" |
248 |
||
249 |
|data| |
|
250 |
||
251 |
data := self withSize:(dim1 * dim2). |
|
252 |
^ MultiDimensionalArrayAccessor |
|
8874 | 253 |
collection:data |
254 |
dimensions:(Array with:dim1 with:dim2) |
|
7255 | 255 |
! |
256 |
||
257 |
_at:dim1 at:dim2 at:dim3 |
|
258 |
"this is a synthetic selector, generated by the compiler, |
|
259 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 260 |
I.e. |
261 |
Array[n,m,o] |
|
7255 | 262 |
generates |
8874 | 263 |
Array _at:n at:m at:o |
7255 | 264 |
" |
265 |
||
266 |
|data| |
|
267 |
||
268 |
data := self withSize:(dim1 * dim2 * dim3). |
|
8874 | 269 |
^ MultiDimensionalArrayAccessor |
270 |
collection:data |
|
271 |
dimensions:(Array with:dim1 with:dim2 with:dim3) |
|
7255 | 272 |
! ! |
273 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
274 |
!SequenceableCollection class methodsFor:'instance creation-streaming'! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
275 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
276 |
streamContents:blockWithArg |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
277 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
278 |
evaluate blockWithArg, passing that stream, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
279 |
extract and return the streams contents." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
280 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
281 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
282 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
283 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
284 |
blockWithArg value:stream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
285 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
286 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
287 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
288 |
|rslt| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
289 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
290 |
rslt := String streamContents:[:s | s nextPutAll:'hello'; space; nextPutAll:'world'] |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
291 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
292 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
293 |
"Modified: / 29-03-2007 / 15:20:20 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
294 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
295 |
|
10590 | 296 |
streamContents:blockWithArg limitedTo:limit |
297 |
"create a limited write-stream on an instance of the receiver-class, |
|
298 |
evaluate blockWithArg, passing that stream, |
|
299 |
extract and return the streams contents (possibly truncated)." |
|
300 |
||
301 |
|stream| |
|
302 |
||
303 |
stream := self writeStream. |
|
304 |
stream writeLimit:limit. |
|
305 |
blockWithArg value:stream. |
|
306 |
^ stream contents |
|
307 |
||
308 |
"Created: / 04-06-2007 / 17:22:38 / cg" |
|
309 |
! |
|
310 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
311 |
streamContentsEnumerating:aReceiver using:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
312 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
313 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
314 |
and return the collected values. Especially useful, if you have a do-like |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
315 |
enumerator somewhere, and you want this as a collection." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
316 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
317 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
318 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
319 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
320 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
321 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
322 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
323 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
324 |
String streamContentsEnumerating:'hello' using:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
325 |
Array streamContentsEnumerating:Method using:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
326 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
327 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
328 |
"Created: / 29-03-2007 / 15:21:12 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
329 |
! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
330 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
331 |
streamContentsOf:aReceiver enumerator:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
332 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
333 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
334 |
and return the collected values. Especially useful, if you have a do-like |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
335 |
enumerator somewhere, and you want this as a collection." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
336 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
337 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
338 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
339 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
340 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
341 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
342 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
343 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
344 |
String streamContentsOf:'hello' enumerator:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
345 |
Array streamContentsOf:Method enumerator:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
346 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
347 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
348 |
"Created: / 29-03-2007 / 15:05:30 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
349 |
! ! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
350 |
|
8893 | 351 |
!SequenceableCollection class methodsFor:'queries'! |
352 |
||
353 |
isAbstract |
|
11219 | 354 |
"Return if this class is an abstract class. |
355 |
True is returned for SequenceableCollection here; false for subclasses. |
|
356 |
Abstract subclasses must redefine again." |
|
357 |
||
8893 | 358 |
^ self == SequenceableCollection |
359 |
! ! |
|
360 |
||
7261 | 361 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 362 |
|
7796 | 363 |
allButFirst |
8874 | 364 |
"Return a copy of the receiver containing all but the first element. |
7796 | 365 |
Raise an error if there are not enough elements." |
366 |
||
367 |
^ self copyFrom:2 |
|
8571 | 368 |
|
369 |
" |
|
370 |
'1234567890' allButFirst |
|
371 |
" |
|
372 |
! |
|
373 |
||
374 |
allButFirst:n |
|
8874 | 375 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 376 |
Raise an error if there are not enough elements." |
377 |
||
378 |
^ self copyFrom:n+1 |
|
379 |
||
380 |
" |
|
381 |
'1234567890' allButFirst:5 |
|
382 |
" |
|
7796 | 383 |
! |
384 |
||
6149 | 385 |
allButLast |
8874 | 386 |
"Return a copy of the receiver containing all but the last element. |
7796 | 387 |
Raise an error if there are not enough elements." |
6183 | 388 |
|
389 |
^ self allButLast:1 |
|
390 |
||
8571 | 391 |
" |
392 |
'1234567890' allButLast |
|
393 |
" |
|
394 |
||
6183 | 395 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
396 |
! |
|
397 |
||
398 |
allButLast:n |
|
8874 | 399 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 400 |
Raise an error if there are not enough elements." |
6183 | 401 |
|
402 |
^ self copyFrom: 1 to: self size - n |
|
403 |
||
8571 | 404 |
" |
405 |
'1234567890' allButLast:5 |
|
406 |
" |
|
407 |
||
6183 | 408 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 409 |
! |
410 |
||
8874 | 411 |
atPin: index |
4414 | 412 |
"Return the index'th element of me if possible. |
413 |
Return the first or last element if index is out of bounds." |
|
414 |
||
415 |
index < 1 ifTrue:[^ self first]. |
|
416 |
index > self size ifTrue:[^ self last]. |
|
417 |
^ self at:index |
|
418 |
||
419 |
" |
|
8874 | 420 |
#(1 2 3) atPin:4 |
421 |
#(1 2 3) atPin:0 |
|
422 |
#(1 2 3) atPin:1 |
|
423 |
#(1 2 3) atPin:3 |
|
424 |
" |
|
425 |
! |
|
426 |
||
427 |
atRandom |
|
4459 | 428 |
"Return any random element from the receiver" |
429 |
||
8573 | 430 |
^ self atRandom:Random |
431 |
||
432 |
" |
|
8874 | 433 |
#(1 2 3) atRandom |
434 |
" |
|
435 |
! |
|
436 |
||
437 |
atRandom:aRandomGenerator |
|
8573 | 438 |
"Return any random element from the receiver" |
439 |
||
4459 | 440 |
|max idx| |
441 |
||
442 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 443 |
self emptyCollectionError |
4459 | 444 |
]. |
8573 | 445 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 446 |
^ self at:idx |
447 |
||
448 |
" |
|
8874 | 449 |
#(1 2 3) atRandom:(Random new) |
450 |
" |
|
451 |
! |
|
452 |
||
453 |
atWrap:index |
|
4975 | 454 |
"Return the index'th element of me if possible. |
455 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
456 |
||
457 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
458 |
||
459 |
" |
|
8874 | 460 |
#(1 2 3) atWrap:1 |
461 |
#(1 2 3) atWrap:2 |
|
462 |
#(1 2 3) atWrap:3 |
|
463 |
#(1 2 3) atWrap:4 |
|
464 |
#(1 2 3) atWrap:5 |
|
465 |
#(1 2 3) atWrap:6 |
|
466 |
#(1 2 3) atWrap:7 |
|
467 |
#(1 2 3) atWrap:0 |
|
468 |
#(1 2 3) atWrap:-1 |
|
4975 | 469 |
" |
5372 | 470 |
! |
471 |
||
472 |
beginsWith:aCollection |
|
6183 | 473 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 474 |
|
475 |
^ self startsWith:aCollection |
|
6183 | 476 |
|
477 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 478 |
! |
479 |
||
8611 | 480 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 481 |
"squeak uses a different naming here - sigh. |
482 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
483 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
484 |
this method first takes a slice (from:to:) and then collects over that. |
|
485 |
I.e. it is equivalent to: |
|
8874 | 486 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 487 |
|
488 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
489 |
! |
|
490 |
||
7012 | 491 |
copyAfter: anElement |
8874 | 492 |
"Answer a copy of the receiver from after the first occurrence |
493 |
of anElement up to the end. If no such element exists, answer |
|
494 |
an empty copy." |
|
7012 | 495 |
|idx| |
496 |
||
497 |
idx := self indexOf:anElement. |
|
498 |
idx == 0 ifTrue:[idx := self size]. |
|
499 |
^ self copyFrom:idx + 1 |
|
500 |
||
501 |
" |
|
8874 | 502 |
'hello world' copyAfter:$l |
503 |
'123456123456' copyAfter:$2 |
|
504 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
505 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
506 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 507 |
" |
508 |
! |
|
509 |
||
6982 | 510 |
copyAfterLast:element |
8874 | 511 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 512 |
of element to the end; uses = for comparison" |
513 |
||
514 |
|idx| |
|
515 |
||
516 |
idx := self lastIndexOf:element. |
|
517 |
idx == 0 ifTrue:[idx := self size]. |
|
518 |
^ self copyFrom:idx + 1 |
|
519 |
||
520 |
" |
|
8874 | 521 |
'hello world' copyAfterLast:$l |
522 |
'123456123456' copyAfterLast:$2 |
|
523 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
524 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
525 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 526 |
" |
527 |
! |
|
528 |
||
6980 | 529 |
copyUpToLast:element |
8874 | 530 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 531 |
of element; uses = for comparison" |
532 |
||
533 |
|idx| |
|
534 |
||
535 |
idx := self lastIndexOf:element. |
|
536 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
537 |
^ self copyTo:idx - 1 |
|
538 |
||
539 |
" |
|
8874 | 540 |
'hello world' copyUpToLast:$l |
541 |
'123456123456' copyUpToLast:$2 |
|
542 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
543 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
544 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 545 |
" |
546 |
! |
|
547 |
||
8276 | 548 |
copyWithoutAll:elementsToSkip |
549 |
"return a new collection consisting of a copy of the receiver, with |
|
550 |
ALL elements equal to any in elementsToSkip are left out. |
|
551 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
552 |
||
553 |
|n "{ Class: SmallInteger }" |
|
554 |
sz "{ Class: SmallInteger }" |
|
555 |
srcIndex "{ Class: SmallInteger }" |
|
556 |
dstIndex "{ Class: SmallInteger }" |
|
557 |
skipIndex "{ Class: SmallInteger }" |
|
558 |
copy l| |
|
559 |
||
8874 | 560 |
"the code below may look like overkill, |
8276 | 561 |
however, for big collections its better to move data |
562 |
around in big chunks" |
|
563 |
||
564 |
n := self occurrencesOfAny:elementsToSkip. |
|
565 |
n == 0 ifTrue:[^ self copy]. |
|
566 |
||
567 |
sz := self size. |
|
568 |
copy := self copyEmptyAndGrow:(sz - n). |
|
569 |
||
570 |
srcIndex := 1. |
|
571 |
dstIndex := 1. |
|
572 |
||
573 |
n timesRepeat:[ |
|
8874 | 574 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
575 |
l := skipIndex - srcIndex. |
|
576 |
l ~~ 0 ifTrue:[ |
|
577 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
578 |
with:self startingAt:srcIndex. |
|
579 |
dstIndex := dstIndex + l |
|
580 |
]. |
|
581 |
srcIndex := skipIndex + 1 |
|
8276 | 582 |
]. |
583 |
l := sz - srcIndex. |
|
584 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 585 |
with:self startingAt:srcIndex. |
8276 | 586 |
^ copy |
587 |
||
588 |
" |
|
589 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 590 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 591 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
592 |
" |
|
593 |
! |
|
594 |
||
7304 | 595 |
copyWithoutFirst |
596 |
^ self copyFrom:2 |
|
597 |
! |
|
598 |
||
11279 | 599 |
sortBy:aBlock |
600 |
"Sort inplace - destructive" |
|
601 |
||
602 |
self sort:aBlock |
|
603 |
||
604 |
"Created: / 22-10-2008 / 21:25:35 / cg" |
|
4414 | 605 |
! ! |
606 |
||
7261 | 607 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 608 |
|
609 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
610 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
611 |
! ! |
|
612 |
||
1 | 613 |
!SequenceableCollection methodsFor:'accessing'! |
614 |
||
1366 | 615 |
after:anObject |
616 |
"return the element, after anObject. |
|
5231 | 617 |
If anObject is not in the receiver, report an error; |
618 |
if anObject is the last in the receiver, return nil. |
|
1366 | 619 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
620 |
||
621 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
622 |
||
623 |
" |
|
8874 | 624 |
#(4 3 2 1) asOrderedCollection after:3. |
625 |
#(4 3 2 1) asOrderedCollection after:5 |
|
626 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 627 |
" |
628 |
||
629 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
630 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
631 |
! |
|
632 |
||
633 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 634 |
"return the element, after anObject. |
5231 | 635 |
If anObject is the last in the receiver, return nil. |
636 |
If there is no such element (anObject is not found), |
|
1366 | 637 |
return the value from exceptionBlock." |
638 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
639 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
640 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
641 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
642 |
idx ~~ 0 ifTrue:[ |
8874 | 643 |
idx == self size ifTrue:[^ nil]. |
644 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
645 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
646 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
647 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
648 |
" |
8874 | 649 |
#(4 3 2 1) after:3 ifAbsent:nil. |
650 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
651 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
652 |
(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
|
653 |
" |
1366 | 654 |
! |
655 |
||
6967 | 656 |
anElement |
8874 | 657 |
"return any element from the collection, |
6967 | 658 |
report an error if there is none" |
659 |
||
660 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
661 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
662 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
663 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
664 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
665 |
" |
6967 | 666 |
! |
667 |
||
32 | 668 |
at:index ifAbsent:exceptionBlock |
8874 | 669 |
"return the element at index if valid. |
670 |
If the index is invalid, return the result of evaluating |
|
302 | 671 |
the exceptionblock. |
8874 | 672 |
NOTICE: |
673 |
in ST-80, this message is only defined for Dictionaries, |
|
674 |
however, having a common protocol with indexed collections |
|
675 |
often simplifies things." |
|
32 | 676 |
|
8579 | 677 |
(index between:1 and:self size) ifFalse:[ |
8874 | 678 |
^ exceptionBlock value |
32 | 679 |
]. |
680 |
^ self at:index |
|
681 |
||
95 | 682 |
" |
683 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 684 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
685 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 686 |
|
95 | 687 |
(Dictionary with:(#foo -> #bar) |
8874 | 688 |
with:(#frob -> #baz)) |
689 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 690 |
" |
362 | 691 |
! |
692 |
||
1366 | 693 |
before:anObject |
694 |
"return the element before the argument, anObject. |
|
5231 | 695 |
If anObject is not in the receiver, report an error; |
696 |
if anObject is the first in the receiver, return nil. |
|
1366 | 697 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
698 |
||
699 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
700 |
||
701 |
" |
|
8874 | 702 |
#(4 3 2 1) asOrderedCollection before:3. |
703 |
#(4 3 2 1) asOrderedCollection before:4 |
|
704 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 705 |
" |
706 |
||
707 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
708 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
709 |
! |
|
710 |
||
711 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 712 |
"return the element, before anObject. |
5231 | 713 |
If anObject is the first in the receiver, return nil. |
714 |
If there is no such element (anObject is not found), |
|
1366 | 715 |
return the value from exceptionBlock." |
716 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
717 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
718 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
719 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
720 |
idx ~~ 0 ifTrue:[ |
8874 | 721 |
idx == 1 ifTrue:[^ nil]. |
722 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
723 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
724 |
^ exceptionBlock value |
1366 | 725 |
|
5231 | 726 |
" |
8874 | 727 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
728 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
729 |
#(4 3 2 1) asOrderedCollection before:5. |
|
730 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
731 |
#(4 3 2 1) asOrderedCollection before:4. |
|
732 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
733 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 734 |
" |
735 |
||
1366 | 736 |
! |
737 |
||
602 | 738 |
first |
8874 | 739 |
"return the first element; |
5231 | 740 |
report an error, if the collection is empty." |
602 | 741 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
742 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 743 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
744 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
745 |
" |
8874 | 746 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
747 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
748 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
749 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
750 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 751 |
! |
752 |
||
5887 | 753 |
first:n |
754 |
"return the n first elements of the collection. |
|
755 |
Raises an error if there are not enough elements in the receiver." |
|
756 |
||
757 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
758 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
759 |
||
760 |
^ self copyFirst:n |
|
761 |
||
762 |
" |
|
8874 | 763 |
#(1 2 3 4 5) first:3 |
764 |
#(1 2 3 4 5) first:6 |
|
765 |
#(1 2 3 4 5) asSet first:3 |
|
766 |
'hello world' first:5 |
|
5887 | 767 |
" |
768 |
! |
|
769 |
||
362 | 770 |
keyAtEqualValue:value |
771 |
"return the index of a value. |
|
772 |
This is normally not used (use indexOf:), but makes the |
|
773 |
protocol more compatible with dictionaries." |
|
774 |
||
775 |
^ self indexOf:value |
|
776 |
! |
|
777 |
||
778 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
779 |
"return the index of a value. |
|
780 |
This is normally not used (use indexOf:), but makes the |
|
781 |
protocol more compatible with dictionaries." |
|
782 |
||
783 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 784 |
! |
785 |
||
786 |
keyAtValue:value |
|
787 |
"return the index of a value. |
|
788 |
This is normally not used (use indexOf:), but makes the |
|
789 |
protocol more compatible with dictionaries." |
|
790 |
||
791 |
^ self identityIndexOf:value |
|
792 |
! |
|
793 |
||
794 |
keyAtValue:value ifAbsent:exceptionBlock |
|
795 |
"return the index of a value. |
|
796 |
This is normally not used (use indexOf:), but makes the |
|
797 |
protocol more compatible with dictionaries." |
|
798 |
||
799 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
800 |
! |
|
801 |
||
802 |
last |
|
5231 | 803 |
"return the last element; |
804 |
report an error, if the collection is empty." |
|
602 | 805 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
806 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
807 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
808 |
(sz := self size) == 0 ifFalse:[ |
8874 | 809 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
810 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
811 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
812 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
813 |
|
5231 | 814 |
" |
815 |
#(1 2 3 4 5) last |
|
816 |
#() last |
|
817 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
818 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
819 |
|
5887 | 820 |
last:n |
821 |
"return the n last elements of the collection. |
|
822 |
Raises an error if there are not enough elements in the receiver." |
|
823 |
||
824 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
825 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
826 |
||
827 |
^ self copyLast:n |
|
828 |
||
829 |
" |
|
8874 | 830 |
#(1 2 3 4 5) last:3 |
831 |
#(1 2 3 4 5) last:6 |
|
832 |
#(1 2 3 4 5) asSet last:3 |
|
833 |
'hello world' last:5 |
|
5887 | 834 |
" |
835 |
! |
|
836 |
||
4986 | 837 |
nth:n |
5231 | 838 |
"return the nth element; |
839 |
report an error, if the collections size is smaller than n." |
|
4986 | 840 |
|
841 |
^ self at:n |
|
842 |
||
843 |
" |
|
8874 | 844 |
args: |
4986 | 845 |
returns: nth element <object> |
846 |
" |
|
847 |
||
5231 | 848 |
" |
8874 | 849 |
#(10 20 30 40 50) nth:4 |
5231 | 850 |
#(10 20 30 40 50) nth:6 |
851 |
#() nth:1 |
|
852 |
" |
|
853 |
||
4986 | 854 |
! |
855 |
||
4855 | 856 |
second |
5231 | 857 |
"return the second element; |
858 |
report an error, if the collections size is smaller than 2." |
|
4855 | 859 |
|
860 |
^ self at:2 |
|
861 |
||
862 |
" |
|
8874 | 863 |
args: |
4855 | 864 |
returns: secondElement <object> |
865 |
" |
|
866 |
||
867 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
868 |
! |
|
869 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
870 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
871 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
872 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
873 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
874 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
875 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
876 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
877 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
878 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
879 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 880 |
! ! |
881 |
||
602 | 882 |
!SequenceableCollection methodsFor:'adding & removing'! |
883 |
||
884 |
add:anObject |
|
885 |
"append the argument, anObject to the collection. |
|
886 |
Return the argument, anObject. |
|
887 |
||
888 |
Notice, that this is modifies the receiver NOT a copy. |
|
889 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 890 |
due to the grow:-message, which is inefficient for fixed size |
602 | 891 |
collections (i.e. for Strings and Arrays it is not recommened)." |
892 |
||
893 |
|newSize| |
|
894 |
||
895 |
newSize := self size + 1. |
|
896 |
self grow:newSize. |
|
897 |
self at:newSize put:anObject. |
|
898 |
^ anObject |
|
899 |
||
900 |
" |
|
901 |
|a| |
|
902 |
a := #(1 2 3 4 5 6 7 8). |
|
903 |
a add:'hello'. |
|
904 |
a |
|
905 |
" |
|
906 |
" |
|
907 |
|c| |
|
908 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
909 |
c add:'hello'. |
|
910 |
c |
|
911 |
" |
|
912 |
! |
|
913 |
||
914 |
add:anElement beforeIndex:index |
|
915 |
"insert the first argument, anObject into the collection before slot index. |
|
916 |
Return the receiver (sigh - ST-80 compatibility). |
|
917 |
||
918 |
Notice, that this is modifies the receiver NOT a copy. |
|
919 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 920 |
due to the grow:-message, which is inefficient for fixed size |
602 | 921 |
collections (i.e. for Strings and Arrays it is not recommened)." |
922 |
||
923 |
|newSize| |
|
924 |
||
925 |
newSize := self size + 1. |
|
926 |
self grow:newSize. |
|
927 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
928 |
self at:index put:anElement |
|
929 |
||
930 |
" |
|
931 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
932 |
" |
|
202 | 933 |
! |
934 |
||
602 | 935 |
addFirst:anObject |
936 |
"prepend the argument, anObject to the collection. |
|
937 |
Return the argument, anObject. |
|
938 |
||
939 |
Notice, that this is modifies the receiver NOT a copy. |
|
940 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 941 |
due to the grow:-message, which is inefficient for fixed size |
602 | 942 |
collections (i.e. for Strings and Arrays it is not recommened)." |
943 |
||
944 |
|newSize| |
|
945 |
||
946 |
newSize := self size + 1. |
|
947 |
self grow:newSize. |
|
948 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
949 |
self at:1 put:anObject. |
|
950 |
^ anObject |
|
951 |
||
952 |
" |
|
8874 | 953 |
|a| |
954 |
a:= #(1 2 3 4 5 6 7 8). |
|
955 |
a addFirst:'hello'. |
|
956 |
a |
|
602 | 957 |
" |
958 |
" |
|
959 |
|c| |
|
960 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
961 |
c addFirst:'hello'. |
|
962 |
c |
|
963 |
" |
|
69 | 964 |
! |
965 |
||
602 | 966 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
967 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
968 |
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
|
969 |
Use equality compare (=) to search for an occurrence. |
602 | 970 |
|
971 |
Notice, that this is modifies the receiver NOT a copy. |
|
972 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 973 |
due to the grow:-message, which is inefficient for fixed size |
602 | 974 |
collections (i.e. for Strings and Arrays it is not recommened)." |
975 |
||
8874 | 976 |
|any |
602 | 977 |
dstIndex "{ Class: SmallInteger }" |
978 |
sz "{ Class: SmallInteger }"| |
|
979 |
||
980 |
dstIndex := 1. |
|
981 |
any := false. |
|
982 |
sz := self size. |
|
983 |
1 to:sz do:[:srcIndex | |
|
8874 | 984 |
(anElement = (self at:srcIndex)) ifTrue:[ |
985 |
any := true |
|
986 |
] ifFalse:[ |
|
987 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
988 |
self at:dstIndex put:(self at:srcIndex) |
|
989 |
]. |
|
990 |
dstIndex := dstIndex + 1 |
|
991 |
] |
|
602 | 992 |
]. |
993 |
any ifTrue:[ |
|
8874 | 994 |
self grow:dstIndex - 1. |
995 |
^ anElement |
|
602 | 996 |
]. |
997 |
^ aBlock value |
|
998 |
||
999 |
" |
|
1422 | 1000 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1001 |
#(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
|
1002 |
#(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
|
1003 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1004 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1005 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1006 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1007 |
! |
1008 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1009 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1010 |
"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
|
1011 |
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
|
1012 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1013 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1014 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1015 |
"/ 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
|
1016 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1017 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1018 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1019 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1020 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1021 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1022 |
[runIndex <= self size] whileTrue:[ |
8874 | 1023 |
element := self at:runIndex. |
1024 |
(aBlock value:element) ifTrue:[ |
|
1025 |
removed add:element. |
|
1026 |
self removeAtIndex:runIndex |
|
1027 |
] ifFalse:[ |
|
1028 |
runIndex := runIndex + 1 |
|
1029 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1030 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1031 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1032 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1033 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1034 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1035 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1036 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1037 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1038 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1039 |
" |
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 |
"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
|
1042 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1043 |
|
602 | 1044 |
removeAtIndex:anIndex |
1045 |
"remove the element stored at anIndex. Return the removed object. |
|
1046 |
||
1047 |
Notice, that this is modifies the receiver NOT a copy. |
|
1048 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1049 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1050 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1051 |
||
1052 |
|element| |
|
1053 |
||
1054 |
element := self at:anIndex. |
|
1055 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1056 |
^ element |
|
1057 |
||
1058 |
" |
|
1059 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1060 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1061 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1062 |
" |
|
13 | 1063 |
! |
1064 |
||
602 | 1065 |
removeFirst |
1066 |
"remove the first element of the receiver and return it. |
|
1067 |
||
1068 |
Notice, that this is modifies the receiver NOT a copy. |
|
1069 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1070 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1071 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1072 |
||
1073 |
^ self removeAtIndex:1 |
|
1074 |
||
1075 |
" |
|
1076 |
|a| |
|
1077 |
a := #(1 2 3 4 5 6). |
|
1078 |
a removeFirst. |
|
8874 | 1079 |
a |
602 | 1080 |
" |
13 | 1081 |
! |
1082 |
||
2294 | 1083 |
removeFromIndex:startIndex |
1084 |
"remove the elements stored at indexes from startIndex to the end. |
|
1085 |
Return the receiver. |
|
1086 |
Returning the receiver is a historic leftover - it may at one |
|
1087 |
time return a collection of the removed elements. |
|
1088 |
||
8874 | 1089 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1090 |
therefore any other users of the receiver will also see this change. |
1091 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1092 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1093 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1094 |
||
1095 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1096 |
||
1097 |
" |
|
8874 | 1098 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1099 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1100 |
" |
|
1101 |
||
1102 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1103 |
! |
|
1104 |
||
602 | 1105 |
removeFromIndex:startIndex toIndex:endIndex |
1106 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1107 |
Return the receiver. |
|
1108 |
Returning the receiver is a historic leftover - it may at one |
|
1109 |
time return a collection of the removed elements. |
|
1110 |
||
8874 | 1111 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1112 |
therefore any other users of the receiver will also see this change. |
1113 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1114 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1115 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1116 |
||
1117 |
|size newSize| |
|
1118 |
||
1119 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1120 |
self grow:(startIndex - 1) |
602 | 1121 |
] ifFalse:[ |
8874 | 1122 |
newSize := size - endIndex + startIndex - 1. |
1123 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1124 |
self grow:newSize |
|
602 | 1125 |
] |
1126 |
||
1127 |
"/ |newSize| |
|
1128 |
"/ |
|
1129 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1130 |
"/ newSize <= 0 ifTrue:[ |
|
1131 |
"/ self grow:0 |
|
1132 |
"/ ] ifFalse:[ |
|
1133 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1134 |
"/ self grow:newSize |
|
1135 |
"/ ] |
|
1136 |
||
1137 |
" |
|
8874 | 1138 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1139 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1140 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1141 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1142 |
" |
1143 |
! |
|
1144 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1145 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1146 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1147 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1148 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1149 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1150 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1151 |
Also note, that it may be a slow operation for some collections, |
8874 | 1152 |
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
|
1153 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1154 |
|
2363 | 1155 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1156 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1157 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1158 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1159 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1160 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1161 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1162 |
1 to:sz do:[:srcIndex | |
8874 | 1163 |
el := self at:srcIndex. |
1164 |
(anElement == el) ifTrue:[ |
|
1165 |
any := true |
|
1166 |
] ifFalse:[ |
|
1167 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1168 |
self at:dstIndex put:el |
|
1169 |
]. |
|
1170 |
dstIndex := dstIndex + 1 |
|
1171 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1172 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1173 |
any ifTrue:[ |
8874 | 1174 |
self grow:dstIndex - 1. |
1175 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1176 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1177 |
^ aBlock value |
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 |
" |
8874 | 1180 |
#(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
|
1181 |
#(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
|
1182 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1183 |
|
2363 | 1184 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1185 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1186 |
|
602 | 1187 |
removeIndex:index |
1188 |
"remove the argument stored at index. Return the receiver. |
|
1189 |
||
1190 |
Notice, that this is modifies the receiver NOT a copy. |
|
1191 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1192 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1193 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1194 |
||
1195 |
self removeFromIndex:index toIndex:index |
|
1196 |
||
1197 |
" |
|
1198 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1199 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1200 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1201 |
" |
|
77 | 1202 |
! |
1203 |
||
602 | 1204 |
removeLast |
1205 |
"remove the last element of the receiver and return it. |
|
1206 |
||
1207 |
Notice, that this is modifies the receiver NOT a copy. |
|
1208 |
Also note, that it may be a slow operation for some collections, |
|
1209 |
due to the grow:-message, which is inefficient for fixed size |
|
1210 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1211 |
||
8874 | 1212 |
^ self removeAtIndex:(self size) |
602 | 1213 |
|
1214 |
" |
|
1215 |
|a| |
|
1216 |
a := #(1 2 3 4 5 6). |
|
1217 |
a removeLast. |
|
8874 | 1218 |
a |
602 | 1219 |
" |
1220 |
! ! |
|
1221 |
||
11407 | 1222 |
!SequenceableCollection methodsFor:'combinatoric'! |
1223 |
||
1224 |
combinationsDo: aBlock |
|
11463 | 1225 |
"Repeatly evaluate aBlock with all combinations of elements from the receivers elements. |
1226 |
The receivers elements must be collections of the individuals to be taken for the combinations" |
|
11407 | 1227 |
|
1228 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1229 |
||
1230 |
" |
|
1231 |
(Array |
|
11410 | 1232 |
with:($a to:$d)) |
1233 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1234 |
" |
|
1235 |
" |
|
1236 |
(Array |
|
11407 | 1237 |
with:($a to:$d) |
1238 |
with:(1 to: 4)) |
|
1239 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1240 |
" |
|
1241 |
" |
|
1242 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1243 |
" |
|
11408 | 1244 |
"all combinations of two letters are generated with: |
1245 |
(Array |
|
1246 |
with:($a to:$z) |
|
1247 |
with:($a to:$z)) |
|
1248 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1249 |
" |
|
11432 | 1250 |
" |
1251 |
(Array |
|
1252 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1253 |
with:#(A)) |
|
1254 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1255 |
" |
|
11407 | 1256 |
! |
1257 |
||
1258 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1259 |
"a helper for combinationsDo:" |
|
1260 |
||
11431 | 1261 |
|loopedElement| |
1262 |
||
1263 |
loopedElement := self at:anInteger. |
|
1264 |
||
11407 | 1265 |
anInteger == self size ifTrue:[ |
11431 | 1266 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1267 |
^ self. |
11431 | 1268 |
]. |
1269 |
||
1270 |
loopedElement do:[:el | |
|
1271 |
|newPrefix| |
|
1272 |
||
1273 |
newPrefix := (prefix copyWith:el). |
|
1274 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1275 |
]. |
1276 |
||
1277 |
" |
|
1278 |
(Array |
|
1279 |
with:($a to:$d) |
|
1280 |
with:(1 to: 4)) |
|
1281 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1282 |
" |
|
11431 | 1283 |
" |
1284 |
(Array |
|
1285 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1286 |
with:#(A)) |
|
1287 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1288 |
" |
|
11407 | 1289 |
! |
1290 |
||
11462 | 1291 |
lexicographicPermutationsDo:aBlock |
1292 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1293 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1294 |
permutations in lexicographical order (i.e. sorted by size). |
|
1295 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
|
1296 |
the identical same collection (bit with different contents) every time. |
|
1297 |
This non-functional kludge was done for performance." |
|
1298 |
||
1299 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1300 |
||
1301 |
" |
|
1302 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1303 |
" |
|
1304 |
! |
|
1305 |
||
1306 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1307 |
"a helper for lexicographicPermutationsDo:" |
|
1308 |
||
1309 |
|mySize values indices| |
|
1310 |
||
1311 |
mySize := self size. |
|
1312 |
anInteger > mySize ifTrue: [^ self]. |
|
1313 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1314 |
values := self copyFrom:anInteger to:mySize. |
|
1315 |
indices := (anInteger to:mySize) asArray. |
|
1316 |
values sortWith:indices. |
|
1317 |
indices do:[:i | |
|
1318 |
self swap: anInteger with: i. |
|
1319 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1320 |
self swap: anInteger with: i |
|
1321 |
] |
|
1322 |
||
1323 |
" |
|
1324 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1325 |
" |
|
1326 |
! |
|
1327 |
||
1328 |
permutationsDo:aBlock |
|
1329 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1330 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
1331 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
|
11461 | 1332 |
the identical same collection (bit with different contents) every time. |
1333 |
This non-functional kludge was done for performance." |
|
11407 | 1334 |
|
1335 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1336 |
||
1337 |
" |
|
11462 | 1338 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1339 |
" |
1340 |
||
1341 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1342 |
! |
|
1343 |
||
1344 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1345 |
"a helper for permutationsDo:" |
11407 | 1346 |
|
1347 |
|mySize| |
|
1348 |
||
1349 |
mySize := self size. |
|
11462 | 1350 |
anInteger > mySize ifTrue: [^ self]. |
1351 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1352 |
anInteger to:mySize do:[:i | |
1353 |
self swap: anInteger with: i. |
|
11462 | 1354 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1355 |
self swap: anInteger with: i |
1356 |
] |
|
1357 |
||
1358 |
" |
|
1359 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1360 |
|
1361 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1362 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1363 |
" |
1364 |
||
1365 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1366 |
! |
1367 |
||
1368 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1369 |
"a helper for permutationsDo:" |
|
1370 |
||
1371 |
|mySize| |
|
1372 |
||
1373 |
mySize := self size. |
|
1374 |
anInteger > mySize ifTrue: [^ self]. |
|
1375 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1376 |
anInteger to:mySize do:[:i | |
|
1377 |
self swap: anInteger with:i. |
|
1378 |
anotherCollection swap: anInteger with:i. |
|
1379 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1380 |
self swap:anInteger with: i. |
|
1381 |
anotherCollection swap:anInteger with:i. |
|
1382 |
] |
|
1383 |
||
1384 |
" |
|
1385 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1386 |
||
1387 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1388 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1389 |
" |
|
1390 |
||
1391 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1392 |
! |
|
1393 |
||
1394 |
permutationsWith:anotherCollection do:aBlock |
|
1395 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1396 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
1397 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
|
1398 |
the identical same collection (bit with different contents) every time. |
|
1399 |
This non-functional kludge was done for performance." |
|
1400 |
||
1401 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1402 |
||
1403 |
" |
|
1404 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1405 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1406 |
" |
|
1407 |
||
1408 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1409 |
! ! |
1410 |
||
602 | 1411 |
!SequenceableCollection methodsFor:'comparing'! |
1412 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1413 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1414 |
"compare two sequenceable collections" |
8874 | 1415 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1416 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1417 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1418 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1419 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1420 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1421 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1422 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1423 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1424 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1425 |
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
|
1426 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1427 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1428 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1429 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1430 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1431 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1432 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1433 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1434 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1435 |
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
|
1436 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1437 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1438 |
(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
|
1439 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1440 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1441 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1442 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1443 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1444 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1445 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1446 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1447 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1448 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1449 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1450 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1451 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1452 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1453 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1454 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1455 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1456 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1457 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1458 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1459 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1460 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1461 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1462 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1463 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1464 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1465 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1466 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1467 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1468 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1469 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1470 |
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
|
1471 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1472 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1473 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1474 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1475 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1476 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1477 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1478 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1479 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1480 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1481 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1482 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1483 |
"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
|
1484 |
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
|
1485 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1486 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1487 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1488 |
|
602 | 1489 |
= aCollection |
1490 |
"return true if the receiver and aCollection represent collections |
|
1491 |
with equal contents." |
|
1492 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1493 |
| stop "{ Class: SmallInteger }" | |
602 | 1494 |
|
1495 |
(aCollection == self) ifTrue:[^true]. |
|
1496 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1497 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1498 |
|
1499 |
stop := self size. |
|
1500 |
stop == (aCollection size) ifFalse:[^false]. |
|
1501 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1502 |
1 to:stop do:[:index| |
9366 | 1503 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1504 |
]. |
1505 |
^ true |
|
1506 |
||
1507 |
" |
|
8874 | 1508 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1509 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1510 |
#($1 $2 $3 $4 $5) = '12345' |
|
1511 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1512 |
" |
1513 |
! |
|
1514 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1515 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1516 |
"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
|
1517 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1518 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1519 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1520 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1521 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1522 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1523 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1524 |
"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
|
1525 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1526 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1527 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1528 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1529 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1530 |
|
8874 | 1531 |
commonPrefixWith:aCollection |
6507 | 1532 |
"return the common prefix of myself and the argument, aCollection. |
1533 |
If there is none, an empty collection is returned." |
|
1534 |
||
1535 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1536 |
||
1537 |
" |
|
8874 | 1538 |
'hello' commonPrefixWith:'hello' |
1539 |
'hello' commonPrefixWith:'hElLo' |
|
1540 |
'hello' commonPrefixWith:'hello world' |
|
1541 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1542 |
||
1543 |
'hello world' commonPrefixWith:'hello' |
|
1544 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1545 |
||
1546 |
'abcd' commonPrefixWith:'bcde' |
|
1547 |
||
1548 |
'abcd' commonPrefixWith:'abab' |
|
1549 |
'abcd' commonPrefixWith:'ab' |
|
1550 |
'abcd' commonPrefixWith:'ababab' |
|
1551 |
'abcd' commonPrefixWith:'abcdef' |
|
1552 |
||
1553 |
'abab' commonPrefixWith:'abcd' |
|
1554 |
'ab' commonPrefixWith:'abcd' |
|
1555 |
'ababab' commonPrefixWith:'abcd' |
|
1556 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1557 |
" |
1558 |
! |
|
1559 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1560 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1561 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1562 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1563 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1564 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1565 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1566 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1567 |
1 to:matchLen do:[:idx | |
8874 | 1568 |
|elHere elThere same| |
1569 |
||
1570 |
elHere := self at:idx. |
|
1571 |
elThere := aCollection at:idx. |
|
1572 |
||
1573 |
ignoreCase ifTrue:[ |
|
1574 |
same := elHere sameAs:elThere |
|
1575 |
] ifFalse:[ |
|
1576 |
same := elHere = elThere |
|
1577 |
]. |
|
1578 |
same ifFalse:[ |
|
1579 |
^ self copyTo:(idx - 1). |
|
1580 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1581 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1582 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1583 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1584 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1585 |
" |
8874 | 1586 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1587 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1588 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1589 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1590 |
||
1591 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1592 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1593 |
||
1594 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1595 |
||
1596 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1597 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1598 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1599 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1600 |
||
1601 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1602 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1603 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1604 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1605 |
" |
|
1606 |
! |
|
1607 |
||
1608 |
commonSuffixWith:aCollection |
|
6507 | 1609 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1610 |
If there is none, an empty collection is returned." |
|
1611 |
||
1612 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1613 |
||
1614 |
" |
|
8874 | 1615 |
'hello' commonSuffixWith:'hello' |
1616 |
'hello' commonSuffixWith:'hElLo' |
|
1617 |
'hello' commonSuffixWith:'hello world' |
|
1618 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1619 |
'hello2 world' commonSuffixWith:'hello world' |
|
1620 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1621 |
||
1622 |
'hello world' commonSuffixWith:'world' |
|
1623 |
'hello WoRlD' commonSuffixWith:'world' |
|
1624 |
||
1625 |
'dcba' commonSuffixWith:'edcb' |
|
1626 |
||
1627 |
'dcba' commonSuffixWith:'baba' |
|
1628 |
'dcba' commonSuffixWith:'ba' |
|
1629 |
'dcba' commonSuffixWith:'bababa' |
|
1630 |
'dcba' commonSuffixWith:'fdcba' |
|
1631 |
||
1632 |
'baba' commonSuffixWith:'dcba' |
|
1633 |
'ba' commonSuffixWith:'dcba' |
|
1634 |
'bababa' commonSuffixWith:'dcba' |
|
1635 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1636 |
" |
1637 |
! |
|
1638 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1639 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1640 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1641 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1642 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1643 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1644 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1645 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1646 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1647 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1648 |
1 to:matchLen do:[:idx | |
8874 | 1649 |
|elHere elThere same| |
1650 |
||
1651 |
elHere := self at:(l1 - idx + 1). |
|
1652 |
elThere := aCollection at:(l2 - idx + 1). |
|
1653 |
||
1654 |
ignoreCase ifTrue:[ |
|
1655 |
same := elHere sameAs:elThere |
|
1656 |
] ifFalse:[ |
|
1657 |
same := elHere = elThere |
|
1658 |
]. |
|
1659 |
same ifFalse:[ |
|
1660 |
^ self copyFrom:(l1 - idx + 2). |
|
1661 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1662 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1663 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1664 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1665 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1666 |
" |
8874 | 1667 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1668 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1669 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1670 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1671 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1672 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1673 |
||
1674 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1675 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1676 |
||
1677 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1678 |
||
1679 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1680 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1681 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1682 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1683 |
||
1684 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1685 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1686 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1687 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1688 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1689 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1690 |
|
5813 | 1691 |
deepSameContentsAs:aCollection |
1692 |
"return true, if the receiver and the arg have the same contents |
|
1693 |
in both the named instance vars and any indexed instVars. |
|
1694 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1695 |
||
1696 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1697 |
unused space" |
|
1698 |
||
1699 |
|index "{ Class: SmallInteger }" |
|
1700 |
stop "{ Class: SmallInteger }" | |
|
1701 |
||
1702 |
(aCollection == self) ifTrue:[^true]. |
|
1703 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1704 |
||
1705 |
stop := self size. |
|
1706 |
stop == (aCollection size) ifFalse:[^false]. |
|
1707 |
||
1708 |
index := 1. |
|
1709 |
[index <= stop] whileTrue:[ |
|
8874 | 1710 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1711 |
index := index + 1 |
|
5813 | 1712 |
]. |
1713 |
^ true |
|
1714 |
||
1715 |
" |
|
8874 | 1716 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1717 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1718 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1719 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1720 |
" |
1721 |
! |
|
1722 |
||
602 | 1723 |
endsWith:aCollection |
1724 |
"return true, if the receivers last elements match those |
|
1725 |
of aCollection" |
|
1726 |
||
1727 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1728 |
index2 "{ Class: SmallInteger }" |
1729 |
stop "{ Class: SmallInteger }" |
|
602 | 1730 |
sz "{ Class: SmallInteger }"| |
1731 |
||
1732 |
(aCollection == self) ifTrue:[^ true]. |
|
1733 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1734 |
||
1735 |
stop := aCollection size. |
|
77 | 1736 |
sz := self size. |
602 | 1737 |
stop > sz ifTrue:[^false]. |
1738 |
||
1739 |
index1 := sz. |
|
1740 |
index2 := stop. |
|
1741 |
[index2 > 0] whileTrue:[ |
|
8874 | 1742 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1743 |
index1 := index1 - 1. |
|
1744 |
index2 := index2 - 1 |
|
77 | 1745 |
]. |
602 | 1746 |
^ true |
1747 |
||
1748 |
" |
|
1749 |
'abcde' endsWith:#($d $e) |
|
8874 | 1750 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1751 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1752 |
" |
|
1753 |
! |
|
1754 |
||
7278 | 1755 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1756 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1757 |
|
1758 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1759 |
||
1760 |
" |
|
1761 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1762 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1763 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1764 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1765 |
" |
1766 |
! |
|
1767 |
||
11820 | 1768 |
hasEqualElements: otherCollection |
1769 |
"Answer whether the receiver's size is the same as otherCollection's |
|
1770 |
size, and each of the receiver's elements equal the corresponding |
|
1771 |
element of otherCollection. |
|
1772 |
This should probably replace the current definition of #= ." |
|
1773 |
||
1774 |
| size | |
|
1775 |
(otherCollection isKindOf: SequenceableCollection) ifFalse: [^ false]. |
|
1776 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
|
1777 |
1 to: size do: |
|
1778 |
[:index | |
|
1779 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false]]. |
|
1780 |
^ true |
|
1781 |
! |
|
1782 |
||
602 | 1783 |
hash |
1784 |
"return a hash key for the receiver" |
|
1785 |
||
1786 |
"this hash is stupid - but for larger collections, the hashing |
|
1787 |
time can become much bigger than the time lost in added probing. |
|
1788 |
Time will show ..." |
|
1789 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1790 |
|mySize h| |
3653 | 1791 |
|
1792 |
mySize := self size. |
|
1793 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1794 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1795 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1796 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1797 |
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
|
1798 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1799 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1800 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1801 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1802 |
"/ 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
|
1803 |
"/ ^ ((((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
|
1804 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1805 |
" |
8874 | 1806 |
#(1 2 3 4 5 6) hash |
1807 |
(1 to:6) hash |
|
1808 |
||
1809 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1810 |
#(1 2 3 4 5) hash |
|
602 | 1811 |
" |
3653 | 1812 |
|
1813 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1814 |
! |
1815 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1816 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1817 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1818 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1819 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1820 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1821 |
^ self |
8874 | 1822 |
sameContentsAs:aCollection |
1823 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1824 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1825 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1826 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1827 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1828 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1829 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1830 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1831 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1832 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1833 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1834 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1835 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1836 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1837 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1838 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1839 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1840 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1841 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1842 |
" |
8874 | 1843 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1844 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1845 |
#($1 $2 $3 $4 $5) = '12345' |
|
1846 |
||
1847 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1848 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1849 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1850 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1851 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1852 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1853 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1854 |
|
6118 | 1855 |
isSameSequenceAs: otherCollection |
8874 | 1856 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1857 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1858 |
" |
|
1859 |
||
1860 |
| size | |
|
1861 |
||
1862 |
(size := self size) = otherCollection size ifFalse: [^false]. |
|
1863 |
||
1864 |
1 to: size do:[:index | |
|
8874 | 1865 |
(self at: index) = (otherCollection at: index) ifFalse: [^false] |
6118 | 1866 |
]. |
1867 |
^ true |
|
1868 |
! |
|
1869 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1870 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1871 |
"return true if the receiver and aCollection represent collections |
8874 | 1872 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1873 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1874 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1875 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1876 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1877 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1878 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1879 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1880 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1881 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1882 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1883 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 1884 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1885 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1886 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1887 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1888 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1889 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1890 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1891 |
[index <= stop] whileTrue:[ |
8874 | 1892 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
1893 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1894 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1895 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1896 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1897 |
" |
8874 | 1898 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
1899 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
1900 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
1901 |
#(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
|
1902 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1903 |
#('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
|
1904 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1905 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1906 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1907 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1908 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1909 |
|
602 | 1910 |
startsWith:aCollection |
1911 |
"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
|
1912 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
1913 |
If the argument is empty, true is returned." |
602 | 1914 |
|
1915 |
|index "{ Class: SmallInteger }" |
|
1916 |
stop "{ Class: SmallInteger }" | |
|
1917 |
||
1918 |
(aCollection == self) ifTrue:[^true]. |
|
1919 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1920 |
||
1921 |
stop := aCollection size. |
|
1922 |
stop > self size ifTrue:[^false]. |
|
1923 |
||
1924 |
index := 1. |
|
1925 |
[index <= stop] whileTrue:[ |
|
8874 | 1926 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
1927 |
index := index + 1 |
|
602 | 1928 |
]. |
1929 |
^ true |
|
1930 |
||
1931 |
" |
|
1932 |
'abcde' startsWith:#($a $b $c) |
|
1933 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
1934 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 1935 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 1936 |
" |
7279 | 1937 |
! |
1938 |
||
1939 |
startsWithAnyOf:aCollectionOfCollections |
|
1940 |
"return true, if the receiver starts with any in aCollection" |
|
1941 |
||
1942 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
1943 |
||
1944 |
" |
|
1945 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
1946 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 1947 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
1948 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 1949 |
" |
13 | 1950 |
! ! |
1951 |
||
260 | 1952 |
!SequenceableCollection methodsFor:'converting'! |
1953 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1954 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 1955 |
"return a collection containing the subcollections (separated by anElement) |
1956 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1957 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1958 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1959 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1960 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1961 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1962 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1963 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1964 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1965 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1966 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1967 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1968 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1969 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1970 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1971 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1972 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1973 |
1 to:numberOfCols do:[:lineNr | |
8874 | 1974 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
1975 |
stopIndex == 0 ifTrue:[ |
|
1976 |
stopIndex := self size |
|
1977 |
] ifFalse: [ |
|
1978 |
stopIndex := stopIndex - 1. |
|
1979 |
]. |
|
1980 |
||
1981 |
(stopIndex < startIndex) ifTrue: [ |
|
1982 |
cols add:(myClass new:0) |
|
1983 |
] ifFalse: [ |
|
1984 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
1985 |
]. |
|
1986 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1987 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1988 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1989 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1990 |
" |
8874 | 1991 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1992 |
'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
|
1993 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1994 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1995 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1996 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
1997 |
|
9234 | 1998 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
1999 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2000 |
of the receiver by anElement. |
|
2001 |
If anElement occurs multiple times in a row, |
|
2002 |
the block will be invoked with empty collections as argument. |
|
2003 |
This algorithm uses equality-compare to detect the element." |
|
2004 |
||
2005 |
|subCollection |
|
2006 |
endIndex "{ Class:SmallInteger }" |
|
2007 |
startIndex "{ Class:SmallInteger }" |
|
2008 |
stopIndex "{ Class:SmallInteger }" | |
|
2009 |
||
2010 |
startIndex := 0. |
|
2011 |
endIndex := self size. |
|
2012 |
||
2013 |
[startIndex <= endIndex] whileTrue:[ |
|
2014 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2015 |
stopIndex == 0 ifTrue:[ |
|
2016 |
stopIndex := self size |
|
2017 |
] ifFalse: [ |
|
2018 |
stopIndex := stopIndex - 1. |
|
2019 |
]. |
|
2020 |
||
2021 |
(stopIndex < startIndex) ifTrue: [ |
|
2022 |
subCollection := self species new:0 |
|
2023 |
] ifFalse: [ |
|
2024 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2025 |
]. |
|
2026 |
aBlock value:subCollection. |
|
2027 |
startIndex := stopIndex + 1 |
|
2028 |
]. |
|
2029 |
||
2030 |
" |
|
2031 |
'' |
|
2032 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2033 |
||
2034 |
'1 one' |
|
2035 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2036 |
||
2037 |
'1 one:2 two:3 three:4 four:5 five' |
|
2038 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2039 |
||
2040 |
'a::b' |
|
2041 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2042 |
||
2043 |
':' |
|
2044 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2045 |
||
2046 |
':a' |
|
2047 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2048 |
||
2049 |
'a:' |
|
2050 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2051 |
" |
|
2052 |
! |
|
2053 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2054 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2055 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2056 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2057 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2058 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2059 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2060 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2061 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2062 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2063 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2064 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2065 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2066 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2067 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2068 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2069 |
[done] whileFalse:[ |
8874 | 2070 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2071 |
stopIndex == 0 ifTrue:[ |
|
2072 |
stopIndex := self size. |
|
2073 |
done := true. |
|
2074 |
] ifFalse: [ |
|
2075 |
stopIndex := stopIndex - 1. |
|
2076 |
]. |
|
2077 |
||
2078 |
(stopIndex < startIndex) ifTrue: [ |
|
2079 |
items add:(myClass new:0) |
|
2080 |
] ifFalse: [ |
|
2081 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2082 |
]. |
|
2083 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2084 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2085 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2086 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2087 |
" |
8874 | 2088 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2089 |
#(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
|
2090 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2091 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2092 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2093 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2094 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2095 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2096 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2097 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2098 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2099 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2100 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2101 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2102 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2103 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2104 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2105 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2106 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2107 |
#(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
|
2108 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2109 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2110 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2111 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2112 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2113 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2114 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2115 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2116 |
|words |
8874 | 2117 |
start "{ Class:SmallInteger }" |
2118 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2119 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2120 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2121 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2122 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2123 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2124 |
[start <= mySize] whileTrue:[ |
8874 | 2125 |
"skip multiple separators" |
2126 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2127 |
start := start + 1 . |
|
2128 |
start > mySize ifTrue:[ |
|
2129 |
^ words |
|
2130 |
]. |
|
2131 |
]. |
|
2132 |
||
2133 |
stop := self findFirst:aBlock startingAt:start. |
|
2134 |
stop == 0 ifTrue:[ |
|
2135 |
words add:(self copyFrom:start to:mySize). |
|
2136 |
^ words |
|
2137 |
]. |
|
2138 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2139 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2140 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2141 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2142 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2143 |
" |
8874 | 2144 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2145 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2146 |
#(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
|
2147 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2148 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2149 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2150 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2151 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2152 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2153 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2154 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2155 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2156 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2157 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2158 |
|
1413 | 2159 |
asStringCollection |
2160 |
"return a new string collection containing the elements; |
|
2161 |
these ought to be strings. (i.e. String or Text instances)" |
|
2162 |
||
2163 |
|newColl sz| |
|
2164 |
||
2165 |
sz := self size. |
|
9162 | 2166 |
newColl := StringCollection withSize:sz. |
1413 | 2167 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2168 |
^ newColl |
|
2169 |
||
2170 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2171 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2172 |
! |
|
2173 |
||
1188 | 2174 |
asStringWith:sepChar |
8874 | 2175 |
"return a string generated by concatenating my elements |
1188 | 2176 |
(which must be strings or nil) and embedding sepChar characters in between. |
2177 |
Nil entries and empty strings are counted as empty lines." |
|
2178 |
||
8874 | 2179 |
^ self |
2180 |
from:1 to:(self size) |
|
2181 |
asStringWith:sepChar |
|
2182 |
compressTabs:false |
|
2183 |
final:nil |
|
1413 | 2184 |
|
2185 |
" |
|
2186 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
2187 |
" |
|
2188 |
||
2189 |
"Modified: 18.5.1996 / 15:28:14 / cg" |
|
1188 | 2190 |
! |
2191 |
||
2192 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2193 |
"return part of myself as a string with embedded sepCharacters. |
|
2194 |
My elements must be strings or nil; nil entries and empty strings are |
|
2195 |
taken as empty lines." |
|
2196 |
||
8874 | 2197 |
^ self |
2198 |
from:firstLine to:lastLine |
|
2199 |
asStringWith:sepCharacter |
|
2200 |
compressTabs:false |
|
2201 |
final:nil |
|
1188 | 2202 |
" |
2203 |
creating entries for searchpath: |
|
2204 |
||
8874 | 2205 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2206 |
||
2207 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2208 |
" |
2209 |
||
2210 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2211 |
! |
|
2212 |
||
260 | 2213 |
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
|
2214 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2215 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2216 |
taken as empty lines. |
2217 |
If the argument compressTabs is true, leading spaces are converted |
|
2218 |
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
|
2219 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2220 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2221 |
^ self |
8874 | 2222 |
from:firstLine to:lastLine |
2223 |
asStringWith:sepCharacter |
|
2224 |
compressTabs:compressTabs |
|
2225 |
final:endCharacter |
|
2226 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2227 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2228 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2229 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2230 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2231 |
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
|
2232 |
"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
|
2233 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2234 |
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
|
2235 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2236 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2237 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2238 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2239 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2240 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2241 |
any emphasis. If false, a plain string is returned. |
260 | 2242 |
This method is tuned for big collections, in not creating many |
263 | 2243 |
intermediate strings (has linear runtime). For very small collections |
2244 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2245 |
|
5575 | 2246 |
^ self |
8874 | 2247 |
from:firstLine to:lastLine |
2248 |
asStringWith:sepCharacter |
|
2249 |
compressTabs:compressTabs |
|
2250 |
final:endCharacter |
|
2251 |
withEmphasis:withEmphasis |
|
5575 | 2252 |
! |
2253 |
||
2254 |
asStringWithCRs |
|
8874 | 2255 |
"return a string generated by concatenating my elements |
5575 | 2256 |
(which must be strings or nil) and embedding cr characters in between. |
2257 |
Nil entries and empty strings are counted as empty lines." |
|
2258 |
||
2259 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2260 |
||
2261 |
" |
|
8874 | 2262 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2263 |
|
2264 |
(OrderedCollection new |
|
8874 | 2265 |
add:'hello'; |
2266 |
add:'world'; |
|
2267 |
add:'foo'; |
|
2268 |
add:('bar' asText allBold); |
|
2269 |
yourself) asStringWithCRs |
|
5575 | 2270 |
|
2271 |
Transcript showCR: |
|
8874 | 2272 |
(OrderedCollection new |
2273 |
add:'hello'; |
|
2274 |
add:'world'; |
|
2275 |
add:'foo'; |
|
2276 |
add:('bar' asText allBold); |
|
2277 |
yourself) asStringWithCRs |
|
5575 | 2278 |
" |
2279 |
||
2280 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2281 |
! |
|
2282 |
||
2283 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2284 |
"return a string generated by concatenating some of my elements |
5575 | 2285 |
(which must be strings or nil) and embedding cr characters in between. |
2286 |
Nil entries and empty strings are counted as empty lines." |
|
2287 |
||
2288 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2289 |
||
2290 |
" |
|
8874 | 2291 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2292 |
|
2293 |
" |
|
2294 |
||
2295 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2296 |
! |
|
2297 |
||
2298 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2299 |
"return part of myself as a string with embedded cr's. |
|
2300 |
My elements must be strings or nil. |
|
2301 |
If the argument compressTabs is true, leading spaces are converted |
|
2302 |
to tab-characters (8col tabs). |
|
2303 |
Nil entries and empty strings are taken as empty lines." |
|
2304 |
||
2305 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2306 |
! |
|
2307 |
||
2308 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2309 |
"return part of myself as a string with embedded cr's. |
|
2310 |
My elements must be strings or nil; nil entries and empty strings are |
|
2311 |
taken as empty lines. |
|
2312 |
If the argument compressTabs is true, leading spaces are converted |
|
2313 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2314 |
should be followed by a cr or not." |
|
2315 |
||
2316 |
^ self asStringWith:(Character cr) |
|
8874 | 2317 |
from:firstLine |
2318 |
to:lastLine |
|
2319 |
compressTabs:compressTabs |
|
2320 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2321 |
! |
2322 |
||
2323 |
decodeAsLiteralArray |
|
2324 |
"given a literalEncoding in the receiver, |
|
2325 |
create & return the corresponding object. |
|
2326 |
The inverse operation to #literalArrayEncoding." |
|
2327 |
||
9414 | 2328 |
|clsName cls| |
5575 | 2329 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2330 |
self size == 0 ifTrue:[ |
9358 | 2331 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2332 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2333 |
|
9414 | 2334 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2335 |
clsName isSymbol ifFalse:[ |
9358 | 2336 |
^ MissingClassInLiteralArrayErrorSignal |
2337 |
raiseRequestWith:clsName |
|
9414 | 2338 |
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
|
2339 |
]. |
9414 | 2340 |
cls := Smalltalk classNamed:clsName. |
5575 | 2341 |
cls isNil ifTrue:[ |
9414 | 2342 |
^ MissingClassInLiteralArrayErrorSignal |
2343 |
raiseRequestWith:clsName |
|
2344 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2345 |
]. |
9414 | 2346 |
|
5575 | 2347 |
^ cls decodeFromLiteralArray:self. |
2348 |
||
2349 |
" |
|
10476 | 2350 |
#(Array 10 20) decodeAsLiteralArray |
2351 |
#(10 20) literalArrayEncoding |
|
2352 |
" |
|
2353 |
||
2354 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2355 |
! |
2356 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2357 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2358 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2359 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2360 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2361 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2362 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2363 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2364 |
#( |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2365 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2366 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2367 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2368 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2369 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2370 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2371 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2372 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2373 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2374 |
|
8874 | 2375 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2376 |
"return part of myself as a string with embedded sepCharacters. |
2377 |
My elements must be strings or nil; nil entries and empty strings are |
|
2378 |
taken as empty lines." |
|
2379 |
||
8874 | 2380 |
^ self |
2381 |
from:firstLine |
|
2382 |
to:lastLine |
|
2383 |
asStringWith:sepCharacter |
|
2384 |
compressTabs:false |
|
2385 |
final:nil |
|
5575 | 2386 |
" |
2387 |
creating entries for searchpath: |
|
2388 |
||
8874 | 2389 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2390 |
||
2391 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2392 |
||
2393 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2394 |
" |
2395 |
||
2396 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2397 |
! |
|
2398 |
||
2399 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2400 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2401 |
My elements must be strings or nil; nil entries and empty strings are |
|
2402 |
taken as empty lines. |
|
2403 |
If the argument compressTabs is true, leading spaces are converted |
|
2404 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2405 |
character (if non-nil)." |
|
2406 |
||
2407 |
^ self |
|
8874 | 2408 |
from:firstLine |
2409 |
to:lastLine |
|
2410 |
asStringWith:sepCharacter |
|
2411 |
compressTabs:compressTabs |
|
2412 |
final:endCharacter |
|
2413 |
withEmphasis:true |
|
5575 | 2414 |
|
2415 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2416 |
! |
|
2417 |
||
2418 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2419 |
"return part of myself as a string or text with embedded sepCharacters |
|
2420 |
and followup endCharacter. |
|
2421 |
My elements must be strings or nil; nil entries and empty strings are |
|
2422 |
taken as empty lines. |
|
2423 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2424 |
If the argument compressTabs is true, leading spaces are converted |
|
2425 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2426 |
character (if non-nil). |
|
2427 |
The withEmphais argument controls if the returned string should preserve |
|
2428 |
any emphasis. If false, a plain string is returned. |
|
2429 |
This method is tuned for big collections, in not creating many |
|
2430 |
intermediate strings (has linear runtime). For very small collections |
|
2431 |
and small strings, it may be faster to use the comma , operation." |
|
2432 |
||
293 | 2433 |
|idx1 "{ Class:SmallInteger }" |
2434 |
idx2 "{ Class:SmallInteger }" |
|
2435 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2436 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2437 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2438 |
newString lineString spaces idx nTabs |
8099 | 2439 |
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
|
2440 |
thisLen anyTab| |
260 | 2441 |
|
2442 |
" |
|
2443 |
first accumulate the size of the string, to avoid |
|
2444 |
countless reallocations. If tabs are compressed, |
|
2445 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2446 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2447 |
" |
8099 | 2448 |
needEmphasis := false. |
2449 |
maxBitsPerCharacter := 8. |
|
1015 | 2450 |
|
260 | 2451 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2452 |
sepCharacter isNil ifTrue:[ |
8874 | 2453 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2454 |
] ifFalse:[ |
8874 | 2455 |
sepCharacter isCharacter ifTrue:[ |
2456 |
sepCnt := 1 |
|
2457 |
] ifFalse:[ |
|
2458 |
sepCnt := sepCharacter size |
|
2459 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2460 |
]. |
260 | 2461 |
|
293 | 2462 |
idx1 := firstLine. |
2463 |
idx2 := lastLine. |
|
2464 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2465 |
lineString := self at:lineIndex. |
2466 |
||
2467 |
lineString notNil ifTrue:[ |
|
2468 |
withEmphasis ifTrue:[ |
|
2469 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2470 |
needEmphasis := true |
|
2471 |
]. |
|
2472 |
]. |
|
2473 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2474 |
totalLength := totalLength + lineString size |
|
2475 |
]. |
|
2476 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2477 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2478 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2479 |
|
8099 | 2480 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2481 |
maxBitsPerCharacter > 16 ifTrue:[ |
2482 |
stringClass := Unicode32String. |
|
2483 |
] ifFalse:[ |
|
2484 |
stringClass := Unicode16String. |
|
2485 |
] |
|
8099 | 2486 |
] ifFalse:[ |
8874 | 2487 |
stringClass := String. |
8099 | 2488 |
]. |
2489 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2490 |
endCharacter notNil ifTrue:[ |
8874 | 2491 |
endCharacter isCharacter ifTrue:[ |
2492 |
totalLength := totalLength + 1 |
|
2493 |
] ifFalse:[ |
|
2494 |
totalLength := totalLength + endCharacter size |
|
2495 |
]. |
|
260 | 2496 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2497 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2498 |
|
260 | 2499 |
spaces := ' '. |
1016 | 2500 |
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
|
2501 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2502 |
needEmphasis ifTrue:[ |
8874 | 2503 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2504 |
]. |
260 | 2505 |
|
2506 |
" |
|
2507 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2508 |
Be careful with runArrays: |
8874 | 2509 |
replacing individual elements is VERY expensive. |
2510 |
Therefore, create a new runArray from scratch. |
|
260 | 2511 |
" |
2512 |
pos := 1. |
|
293 | 2513 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2514 |
lineString := self at:lineIndex. |
2515 |
thisLen := lineString size. |
|
2516 |
thisLen ~~ 0 ifTrue:[ |
|
2517 |
withEmphasis ifFalse:[ |
|
2518 |
lineString := lineString string. |
|
2519 |
]. |
|
2520 |
||
2521 |
(anyTab := compressTabs) ifTrue:[ |
|
2522 |
" |
|
2523 |
mhmh: could use withTabs from String-class here, |
|
2524 |
but we should avoid creating too many temporary strings |
|
2525 |
(especially, since this method is typically used when converting |
|
2526 |
big texts such as when saving in the filebrowser ...). |
|
2527 |
Therefore, we convert tabs inline here doing a direct replace |
|
2528 |
in newString." |
|
2529 |
||
2530 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2531 |
nTabs := (idx-1) // 8. |
|
2532 |
anyTab := (nTabs > 0) |
|
2533 |
]. |
|
2534 |
anyTab ifTrue:[ |
|
2535 |
"any tabs" |
|
2536 |
||
2537 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2538 |
||
2539 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2540 |
newRuns notNil ifTrue:[ |
|
2541 |
newRuns add:nil withOccurrences:nTabs |
|
2542 |
]. |
|
2543 |
pos := pos + nTabs. |
|
2544 |
||
2545 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2546 |
newRuns notNil ifTrue:[ |
|
2547 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2548 |
idx to:lineString size do:[:pos | |
|
2549 |
newRuns add:(lineString emphasisAt:pos) |
|
2550 |
] |
|
2551 |
] ifFalse:[ |
|
2552 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2553 |
] |
|
2554 |
]. |
|
2555 |
pos := pos + thisLen - (nTabs * 8). |
|
2556 |
] ifFalse:[ |
|
2557 |
newString replaceFrom:pos with:lineString. |
|
2558 |
||
2559 |
newRuns notNil ifTrue:[ |
|
2560 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2561 |
newRuns addAll:(lineString emphasis) |
|
2562 |
] ifFalse:[ |
|
2563 |
newRuns add:nil withOccurrences:lineString size |
|
2564 |
] |
|
2565 |
]. |
|
2566 |
pos := pos + thisLen. |
|
2567 |
]. |
|
2568 |
]. |
|
2569 |
||
2570 |
lineIndex ~~ lastLine ifTrue:[ |
|
2571 |
c := sepCharacter |
|
2572 |
] ifFalse:[ |
|
2573 |
c := endCharacter |
|
2574 |
]. |
|
2575 |
||
2576 |
c notNil ifTrue:[ |
|
2577 |
c isCharacter ifTrue:[ |
|
2578 |
newString at:pos put:c. |
|
2579 |
newRuns notNil ifTrue:[ |
|
2580 |
newRuns add:nil. |
|
2581 |
]. |
|
2582 |
pos := pos + 1 |
|
2583 |
] ifFalse:[ |
|
2584 |
newString replaceFrom:pos with:c. |
|
2585 |
newRuns notNil ifTrue:[ |
|
2586 |
newRuns add:nil withOccurrences:c size |
|
2587 |
]. |
|
2588 |
pos := pos + c size |
|
2589 |
] |
|
2590 |
]. |
|
260 | 2591 |
]. |
2592 |
||
2593 |
" |
|
2594 |
in case of tab compression, the result has to be |
|
2595 |
cut to size ... sorry |
|
2596 |
" |
|
2597 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2598 |
newString := newString copyTo:(pos - 1) |
260 | 2599 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2600 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2601 |
newRuns notNil ifTrue:[ |
8874 | 2602 |
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
|
2603 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2604 |
|
260 | 2605 |
^ newString |
2606 |
||
2607 |
" |
|
2608 |
creating entries for searchpath: |
|
2609 |
||
8874 | 2610 |
#('foo' 'bar' 'baz' '/foo/bar') |
2611 |
asStringWith:$: |
|
2612 |
from:1 to:4 |
|
2613 |
compressTabs:false |
|
2614 |
final:nil |
|
260 | 2615 |
|
2616 |
with trailing colon: |
|
2617 |
||
8874 | 2618 |
#('foo' 'bar' 'baz' '/foo/bar') |
2619 |
asStringWith:$: |
|
2620 |
from:1 to:4 |
|
2621 |
compressTabs:false |
|
2622 |
final:$: |
|
260 | 2623 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2624 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2625 |
|
8874 | 2626 |
#('foo' 'bar' 'baz') |
2627 |
asStringWith:nil |
|
2628 |
from:1 to:3 |
|
2629 |
compressTabs:false |
|
2630 |
final:nil |
|
260 | 2631 |
|
2632 |
creating a string from a collection of lines: |
|
2633 |
||
8874 | 2634 |
#('foo' 'bar' 'baz') |
2635 |
asStringWith:(Character cr) |
|
2636 |
from:1 to:3 |
|
2637 |
compressTabs:false |
|
2638 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2639 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2640 |
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
|
2641 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2642 |
(Array |
8874 | 2643 |
with:'foo' |
2644 |
with:('bar' asText allBold) |
|
2645 |
with:'baz' |
|
2646 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2647 |
) |
|
2648 |
asStringWith:(Character cr) |
|
2649 |
from:1 to:4 |
|
2650 |
compressTabs:false |
|
2651 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2652 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2653 |
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
|
2654 |
|
8874 | 2655 |
#('foo' 'bar' 'baz') |
2656 |
asStringWith:'__' |
|
2657 |
from:1 to:3 |
|
2658 |
compressTabs:false |
|
2659 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2660 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2661 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2662 |
|
8874 | 2663 |
#('foo' 'bar' 'baz') |
2664 |
asStringWith:'__' |
|
2665 |
from:1 to:3 |
|
2666 |
compressTabs:false |
|
2667 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2668 |
|
260 | 2669 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2670 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2671 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2672 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
11827 | 2673 |
! |
2674 |
||
2675 |
subCollections:aBlock |
|
2676 |
"Answser an ordered collection of ordered collections |
|
2677 |
where each subcollection is delimited by an element of the receiver |
|
2678 |
for which the given block evaluates to true." |
|
2679 |
||
2680 |
|str answer current e| |
|
2681 |
||
2682 |
str := self readStream. |
|
2683 |
answer := OrderedCollection new. |
|
2684 |
current := OrderedCollection new. |
|
2685 |
[ str atEnd ] whileFalse:[ |
|
2686 |
e := str next. |
|
2687 |
current add:e. |
|
2688 |
(aBlock value:e) ifTrue:[ |
|
2689 |
answer add:current. |
|
2690 |
current := OrderedCollection new |
|
2691 |
] |
|
2692 |
]. |
|
2693 |
current notEmpty ifTrue:[ |
|
2694 |
answer add:current |
|
2695 |
]. |
|
2696 |
^ answer |
|
2697 |
||
2698 |
" |
|
2699 |
#( 1 2 3 nil 4 5 6 nil 7 8 9 nil ) subCollections:[:el | el isNil]. |
|
2700 |
" |
|
260 | 2701 |
! ! |
2702 |
||
7253 | 2703 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2704 |
||
8874 | 2705 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2706 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2707 |
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
|
2708 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2709 |
however, physically, no copy is made). |
9291 | 2710 |
Warning: |
2711 |
The slice SHARES the memory for the element-data with the original, |
|
2712 |
this means that any modifications in the original are visible in the slice |
|
2713 |
and vice versa." |
|
7253 | 2714 |
|
2715 |
^ self |
|
9291 | 2716 |
from:startIndex |
2717 |
to:self size |
|
2718 |
by:1 |
|
8874 | 2719 |
|
2720 |
" |
|
2721 |
#(1 2 3 4 5 6 7) from:3 |
|
2722 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2723 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2724 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2725 |
" |
|
2726 |
! |
|
2727 |
||
2728 |
from:startIndex by:step |
|
9291 | 2729 |
"Create a ReindexedCollection from the receiver. |
2730 |
The new collection represents the elements starting at startIndex to the end. |
|
2731 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2732 |
however, physically, no copy is made). |
|
2733 |
Warning: |
|
2734 |
The slice SHARES the memory for the element-data with the original, |
|
2735 |
this means that any modifications in the original are visible in the slice |
|
2736 |
and vice versa." |
|
7253 | 2737 |
|
2738 |
^ self |
|
9291 | 2739 |
from:startIndex |
2740 |
to:(step > 1 |
|
2741 |
ifTrue: [self size] |
|
2742 |
ifFalse: [1]) |
|
2743 |
by:step |
|
8874 | 2744 |
|
2745 |
" |
|
2746 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2747 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2748 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2749 |
( #(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
|
2750 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2751 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2752 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2753 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2754 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2755 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2756 |
Warning: |
2757 |
The slice SHARES the memory for the element-data with the original, |
|
2758 |
this means that any modifications in the original are visible in the slice |
|
2759 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2760 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2761 |
^ self |
9291 | 2762 |
from:startIndex |
2763 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2764 |
|
2765 |
" |
|
2766 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2767 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2768 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2769 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2770 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2771 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2772 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2773 |
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
|
2774 |
slice := coll from:3 to:7. |
8874 | 2775 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2776 |
coll at:4 put:40. |
8874 | 2777 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2778 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2779 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2780 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2781 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2782 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2783 |
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
|
2784 |
slice := coll from:3 to:7. |
8874 | 2785 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2786 |
slice at:1 put:40. |
8874 | 2787 |
slice. |
2788 |
" |
|
2789 |
! |
|
2790 |
||
2791 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2792 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2793 |
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
|
2794 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2795 |
however, physically, no copy is made). |
9291 | 2796 |
Warning: |
2797 |
The slice SHARES the memory for the element-data with the original, |
|
2798 |
this means that any modifications in the original are visible in the slice |
|
2799 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2800 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2801 |
^ self |
9291 | 2802 |
from:startIndex |
2803 |
to:endIndex |
|
2804 |
by: (startIndex <= endIndex |
|
2805 |
ifTrue: [1] |
|
2806 |
ifFalse: [-1]) |
|
8874 | 2807 |
|
2808 |
" |
|
2809 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2810 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2811 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2812 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2813 |
" |
|
2814 |
! |
|
2815 |
||
2816 |
from:startIndex to:endIndex by:step |
|
9291 | 2817 |
"Create a ReindexedCollection from the receiver. |
2818 |
The new collection represents the receivers elements up to endIndex. |
|
2819 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2820 |
however, physically, no copy is made). |
|
2821 |
Warning: |
|
2822 |
The slice SHARES the memory for the element-data with the original, |
|
2823 |
this means that any modifications in the original are visible in the slice |
|
2824 |
and vice versa." |
|
7253 | 2825 |
|
2826 |
^ ReindexedCollection |
|
9291 | 2827 |
on:self |
2828 |
from:startIndex |
|
2829 |
to:endIndex |
|
2830 |
by:step |
|
8874 | 2831 |
|
2832 |
" |
|
2833 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
2834 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
2835 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
2836 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
2837 |
" |
|
2838 |
! |
|
2839 |
||
2840 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2841 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2842 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2843 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2844 |
however, physically, no copy is made). |
9291 | 2845 |
Warning: |
2846 |
The slice SHARES the memory for the element-data with the original, |
|
2847 |
this means that any modifications in the original are visible in the slice |
|
2848 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2849 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2850 |
^ self |
9291 | 2851 |
from:1 |
2852 |
to:endIndex |
|
2853 |
by:1 |
|
8874 | 2854 |
|
2855 |
" |
|
2856 |
#(1 2 3 4 5 6 7) to:4 |
|
2857 |
( #(1 2 3 4 5 6 7) to:4) first |
|
2858 |
( #(1 2 3 4 5 6 7) to:4) last |
|
2859 |
( #(1 2 3 4 5 6 7) to:4) size |
|
2860 |
" |
|
2861 |
! |
|
2862 |
||
2863 |
to:endIndex by:step |
|
9291 | 2864 |
"Create a ReindexedCollection from the receiver. |
2865 |
The new collection represents the receivers elements up to endIndex. |
|
2866 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
2867 |
however, physically, no copy is made). |
|
2868 |
Warning: |
|
2869 |
The slice SHARES the memory for the element-data with the original, |
|
2870 |
this means that any modifications in the original are visible in the slice |
|
2871 |
and vice versa." |
|
7253 | 2872 |
|
2873 |
^ self |
|
9291 | 2874 |
from:(step > 0 |
2875 |
ifTrue: [1] |
|
2876 |
ifFalse: [self size]) |
|
2877 |
to:endIndex |
|
2878 |
by:step |
|
8874 | 2879 |
|
2880 |
" |
|
2881 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
2882 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
2883 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
2884 |
( #(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
|
2885 |
" |
7253 | 2886 |
! ! |
2887 |
||
1 | 2888 |
!SequenceableCollection methodsFor:'copying'! |
2889 |
||
9106 | 2890 |
++ aCollection |
2891 |
"return a new collection formed from concatenating the receiver with |
|
2892 |
the argument. The class of the new collection is determined by the |
|
2893 |
receivers class, so mixing classes is possible, if the second collections |
|
2894 |
elements can be stored into instances of the receivers class." |
|
2895 |
||
2896 |
^ self , aCollection |
|
2897 |
||
2898 |
" |
|
2899 |
#($a $b $c) ++ #(1 2 3) |
|
2900 |
#($a $b $c) ++ '123' |
|
2901 |
'abc' ++ '123' |
|
2902 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
2903 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2904 |
'abc' asArray ++ #(1 2 3 4 5) |
|
2905 |
" |
|
2906 |
! |
|
2907 |
||
1 | 2908 |
, aCollection |
2909 |
"return a new collection formed from concatenating the receiver with |
|
44 | 2910 |
the argument. The class of the new collection is determined by the |
2911 |
receivers class, so mixing classes is possible, if the second collections |
|
2912 |
elements can be stored into instances of the receivers class." |
|
1 | 2913 |
|
8874 | 2914 |
|newCollection |
1 | 2915 |
mySize "{ Class: SmallInteger }" |
2916 |
newSize "{ Class: SmallInteger }" |
|
2917 |
otherSize "{ Class: SmallInteger }" |
|
2918 |
dstIndex "{ Class: SmallInteger }"| |
|
2919 |
||
2920 |
mySize := self size. |
|
2921 |
otherSize := aCollection size. |
|
2922 |
newSize := mySize + otherSize. |
|
202 | 2923 |
|
252 | 2924 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 2925 |
|
2926 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
2927 |
dstIndex := mySize + 1. |
|
202 | 2928 |
aCollection isSequenceable ifTrue:[ |
8874 | 2929 |
" |
2930 |
yes, aCollection has indexed elements |
|
2931 |
" |
|
2932 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 2933 |
] ifFalse:[ |
8874 | 2934 |
" |
2935 |
no, enumerate aCollection |
|
2936 |
" |
|
2937 |
aCollection do:[:element | |
|
2938 |
newCollection at:dstIndex put:element. |
|
2939 |
dstIndex := dstIndex + 1 |
|
2940 |
] |
|
1 | 2941 |
]. |
2942 |
^ newCollection |
|
44 | 2943 |
|
95 | 2944 |
" |
2945 |
#($a $b $c) , #(1 2 3) |
|
2946 |
#($a $b $c) , '123' |
|
2947 |
'abc' , '123' |
|
2948 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
2949 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
2950 |
'abc' asArray , #(1 2 3 4 5) |
|
2951 |
" |
|
1 | 2952 |
! |
2953 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2954 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2955 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2956 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2957 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2958 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2959 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2960 |
" |
8874 | 2961 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2962 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2963 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2964 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2965 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2966 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2967 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2968 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2969 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2970 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2971 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2972 |
" |
8874 | 2973 |
#($a $b $c $d $e $f $g) copyButLast:2 |
2974 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2975 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2976 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
2977 |
|
602 | 2978 |
copyFirst:count |
2979 |
"return a new collection consisting of the receivers first count |
|
2980 |
elements - this is just a rename of copyTo: - for compatibility." |
|
2981 |
||
2982 |
^ self copyFrom:1 to:count |
|
2983 |
||
2984 |
" |
|
2985 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
2986 |
'1234567890' copyFirst:4 |
|
2987 |
" |
|
2988 |
! |
|
2989 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2990 |
copyFrom:startIndex |
9234 | 2991 |
"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
|
2992 |
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
|
2993 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2994 |
^ self copyFrom:startIndex to:(self size) |
602 | 2995 |
|
2996 |
" |
|
2997 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
2998 |
'1234567890' copyFrom:2 |
|
8874 | 2999 |
(10 to:19) copyFrom:5 |
602 | 3000 |
" |
3001 |
! |
|
3002 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3003 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3004 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3005 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3006 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3007 |
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
|
3008 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3009 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3010 |
^ self |
8874 | 3011 |
copyFrom:startIndex |
3012 |
to:(startIndex + numberOfElements - 1) |
|
3013 |
||
3014 |
" |
|
3015 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3016 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3017 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3018 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3019 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3020 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3021 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3022 |
copyFrom:startIndex to:stopIndex |
602 | 3023 |
"return a new collection consisting of receivers elements |
4403 | 3024 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3025 |
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
|
3026 |
Raise an error, if stopIndex is out of range." |
602 | 3027 |
|
3028 |
|newCollection newSize| |
|
3029 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3030 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3031 |
newSize < 0 ifTrue:[ |
8874 | 3032 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3033 |
]. |
602 | 3034 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3035 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 3036 |
^ newCollection |
3037 |
||
3038 |
" |
|
3039 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3040 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3041 |
(1 to:10) copyFrom:2 to:5 |
3042 |
||
3043 |
'1234567890' copyFrom:2 to:15 |
|
3044 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 3045 |
" |
3046 |
! |
|
3047 |
||
3048 |
copyLast:count |
|
3049 |
"return a new collection consisting of the receivers last count |
|
3050 |
elements." |
|
3051 |
||
3052 |
|sz| |
|
3053 |
||
3054 |
sz := self size. |
|
3055 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3056 |
||
3057 |
" |
|
3058 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3059 |
'1234567890' copyLast:4 |
|
3060 |
" |
|
3061 |
! |
|
3062 |
||
11841 | 3063 |
copyMapping:map |
3064 |
"return a copy, where elements all replaced via the given mapping. |
|
3065 |
If the element is present as key in map, it is replaced by a corresponding value from the map. |
|
3066 |
Otherwise, it is left unchanged." |
|
3067 |
||
3068 |
|s| |
|
3069 |
||
3070 |
s := WriteStream on:self species new. |
|
3071 |
self do:[:el | |
|
3072 |
s nextPut:(map at:el ifAbsent:el) |
|
3073 |
]. |
|
3074 |
^ s contents |
|
3075 |
||
3076 |
" |
|
3077 |
|map| |
|
3078 |
||
3079 |
map := Dictionary new |
|
3080 |
at:1 put:'one'; |
|
3081 |
at:2 put:'two'; |
|
3082 |
yourself. |
|
3083 |
||
3084 |
#(1 2 3 4 1 2 3 4) copyMapping:map |
|
3085 |
" |
|
3086 |
! |
|
3087 |
||
3088 |
copyMappingFrom:inMap to:outMap |
|
3089 |
"return a copy, where elements all replaced via the given mapping. |
|
3090 |
If the element is present in inmap, it is replaced by a corresponding value from outmap. |
|
3091 |
Otherwise, it is left unchanged." |
|
3092 |
||
3093 |
|d| |
|
3094 |
||
3095 |
d := Dictionary new. |
|
3096 |
inMap keysAndValuesDo:[:k :i | d at:i put:(outMap at:k)]. |
|
3097 |
^ self copyMapping:d |
|
3098 |
||
3099 |
" |
|
3100 |
#(1 2 3 4 1 2 3 4) copyMappingFrom:#(1 2) to:#('one' 'two') |
|
3101 |
'hello' copyMappingFrom:'eo' to:'**' |
|
3102 |
" |
|
3103 |
! |
|
3104 |
||
3653 | 3105 |
copyReplaceAll:oldElement with:newElement |
3106 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3107 |
have been replaced by newElement." |
|
3108 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3109 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3110 |
^ self copy replaceAll:oldElement with:newElement |
3111 |
||
3112 |
" |
|
8874 | 3113 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3114 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3115 |
" |
3116 |
||
3117 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3118 |
! |
|
3119 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3120 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3121 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3122 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3123 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3124 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3125 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3126 |
|
5489 | 3127 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3128 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3129 |
"/ '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
|
3130 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3131 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3132 |
self do:[:el | |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3133 |
el = oldObject ifTrue:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3134 |
s nextPutAll:aCollection |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3135 |
] ifFalse:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3136 |
s nextPut:el |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3137 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3138 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3139 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3140 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3141 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3142 |
args: oldObject : <object> |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3143 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3144 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3145 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3146 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3147 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3148 |
" |
8874 | 3149 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3150 |
#(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
|
3151 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3152 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3153 |
|
602 | 3154 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3155 |
"return a copy of the receiver, where the elements from startIndex to |
|
3156 |
endIndex have been replaced by the elements of aCollection" |
|
3157 |
||
3158 |
|newColl sz replSize| |
|
3159 |
||
3160 |
replSize := aCollection size. |
|
3161 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
3162 |
newColl := self copyEmptyAndGrow:sz. |
|
3163 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3164 |
newColl replaceFrom:startIndex with:aCollection. |
|
3165 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3166 |
^ newColl |
|
3167 |
||
3168 |
" |
|
3169 |
#(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 | 3170 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 3171 |
" |
3172 |
! |
|
3173 |
||
3174 |
copyReplaceFrom:startIndex with:aCollection |
|
3175 |
"return a copy of the receiver, where the elements from startIndex to |
|
3176 |
the end have been replaced by the elements of aCollection" |
|
3177 |
||
3178 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
3179 |
||
3180 |
" |
|
8874 | 3181 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
3182 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 3183 |
" |
3184 |
! |
|
3185 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3186 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3187 |
"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
|
3188 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3189 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3190 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3191 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3192 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3193 |
" |
8874 | 3194 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
3195 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3196 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3197 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3198 |
|
602 | 3199 |
copyThrough:element |
3200 |
"return a new collection consisting of the receiver elements |
|
8874 | 3201 |
up-to (AND including) the first occurrence of element." |
602 | 3202 |
|
3203 |
|idx| |
|
3204 |
||
3205 |
idx := self indexOf:element. |
|
3206 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
3207 |
^ self copyFrom:1 to:idx |
|
3208 |
||
3209 |
" |
|
3210 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
3211 |
'1234567890' copyThrough:$5 |
|
3212 |
'1234567890' copyThrough:$a |
|
3213 |
'1234567890' copyThrough:$1 |
|
3214 |
" |
|
3215 |
! |
|
3216 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3217 |
copyTo:stopIndex |
9234 | 3218 |
"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
|
3219 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3220 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3221 |
^ self copyFrom:1 to:stopIndex |
602 | 3222 |
|
3223 |
" |
|
3224 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
3225 |
'1234567890' copyTo:4 |
|
3226 |
" |
|
3227 |
! |
|
3228 |
||
5526 | 3229 |
copyToMax:stop |
3230 |
"return a new collection consisting of receivers elements |
|
8874 | 3231 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 3232 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
3233 |
||
3234 |
^ self copyFrom:1 to:(self size min:stop) |
|
3235 |
||
3236 |
" |
|
3237 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
3238 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
3239 |
" |
|
3240 |
! |
|
3241 |
||
11844 | 3242 |
copyTransliterating:oldElements to:newElements |
3243 |
"return a copy, where elements all transliterated via the given mapping. |
|
3244 |
The transiteraion works like perl's tr operator. |
|
3245 |
||
3246 |
Transliterates all occurrences of the characters found in the search list with the |
|
3247 |
corresponding character in the replacement list. It returns the number of characters replaced |
|
3248 |
or deleted. |
|
3249 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
|
3250 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3251 |
||
3252 |
Note that tr does not do other character escapes such as \n. |
|
3253 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3254 |
||
3255 |
Note also that the whole range idea is rather unportable between character sets, |
|
3256 |
and even within character sets they may cause results you probably didn't expect. |
|
3257 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3258 |
case (a-e, A-E), or digits (0-4). |
|
3259 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3260 |
" |
|
3261 |
||
3262 |
^ self |
|
3263 |
copyTransliterating:oldElements to:newElements |
|
3264 |
complement:false squashDuplicates:false |
|
3265 |
||
3266 |
" |
|
3267 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
|
3268 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3269 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3270 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3271 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
3272 |
" |
|
3273 |
! |
|
3274 |
||
3275 |
copyTransliterating:oldElements to:newElements |
|
3276 |
complement:complement squashDuplicates:squashDuplicates |
|
3277 |
||
3278 |
"return a copy, where elements all transliterated via the given mapping. |
|
3279 |
The transiteraion works like perl's tr operator. |
|
3280 |
||
3281 |
Transliterates all occurrences of the characters found in the search list with the |
|
3282 |
corresponding character in the replacement list. It returns the number of characters replaced |
|
3283 |
or deleted. |
|
3284 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
|
3285 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3286 |
||
3287 |
Note that tr does not do other character escapes such as \n. |
|
3288 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3289 |
||
3290 |
Note also that the whole range idea is rather unportable between character sets, |
|
3291 |
and even within character sets they may cause results you probably didn't expect. |
|
3292 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3293 |
case (a-e, A-E), or digits (0-4). |
|
3294 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3295 |
||
3296 |
If complement is true, non-matching chars are affected. |
|
3297 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
|
3298 |
" |
|
3299 |
||
3300 |
|map s1 s2 range1 range2 c1 c2 outStream prevXlated| |
|
3301 |
||
3302 |
map := Dictionary new. |
|
3303 |
||
3304 |
s1 := oldElements readStream. |
|
3305 |
s2 := newElements readStream. |
|
3306 |
range1 := range2 := nil. |
|
3307 |
[ |
|
3308 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
|
3309 |
range1 notNil or:[ s1 atEnd not ] |
|
3310 |
] whileTrue:[ |
|
3311 |
range1 notNil ifTrue:[ |
|
3312 |
c1 := range1 next. |
|
3313 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3314 |
] ifFalse:[ |
|
3315 |
c1 := s1 next. |
|
3316 |
s1 peek == $\ ifTrue:[ |
|
3317 |
s1 next. |
|
3318 |
] ifFalse:[ |
|
3319 |
s1 peek == $- ifTrue:[ |
|
3320 |
s1 next. |
|
3321 |
range1 := (c1 to:s1 next) readStream. |
|
3322 |
c1 := range1 next. |
|
3323 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3324 |
]. |
|
3325 |
]. |
|
3326 |
]. |
|
3327 |
range2 notNil ifTrue:[ |
|
3328 |
c2 := range2 next. |
|
3329 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3330 |
] ifFalse:[ |
|
3331 |
s2 atEnd ifTrue:[ |
|
3332 |
"/ keep c2 |
|
3333 |
] ifFalse:[ |
|
3334 |
c2 := s2 next. |
|
3335 |
s2 peek == $\ ifTrue:[ |
|
3336 |
s1 next. |
|
3337 |
] ifFalse:[ |
|
3338 |
s2 peek == $- ifTrue:[ |
|
3339 |
s2 next. |
|
3340 |
range2 := (c2 to:s2 next) readStream. |
|
3341 |
c2 := range2 next. |
|
3342 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3343 |
]. |
|
3344 |
]. |
|
3345 |
]. |
|
3346 |
]. |
|
3347 |
map at:c1 put:c2 |
|
3348 |
]. |
|
3349 |
||
3350 |
outStream := WriteStream on:(self species new). |
|
3351 |
self do:[:el | |
|
3352 |
|xLated out| |
|
3353 |
||
3354 |
complement ifTrue:[ |
|
3355 |
(map includesKey:el) ifTrue:[ |
|
3356 |
xLated := out := el. |
|
3357 |
] ifFalse:[ |
|
3358 |
out := newElements at:1 ifAbsent:nil. |
|
3359 |
]. |
|
3360 |
] ifFalse:[ |
|
3361 |
(map includesKey:el) ifTrue:[ |
|
3362 |
xLated := out := map at:el. |
|
3363 |
] ifFalse:[ |
|
3364 |
out := el. |
|
3365 |
]. |
|
3366 |
]. |
|
3367 |
out notNil ifTrue:[ |
|
3368 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
|
3369 |
"/ ignore |
|
3370 |
] ifFalse:[ |
|
3371 |
outStream nextPut:out. |
|
3372 |
]. |
|
3373 |
prevXlated := out. |
|
3374 |
]. |
|
3375 |
]. |
|
3376 |
^ outStream contents |
|
3377 |
||
3378 |
" |
|
3379 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
|
3380 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
|
3381 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3382 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3383 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3384 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
3385 |
||
3386 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3387 |
complement:false squashDuplicates:false |
|
3388 |
||
3389 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3390 |
complement:false squashDuplicates:true |
|
3391 |
||
3392 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3393 |
complement:false squashDuplicates:false |
|
3394 |
||
3395 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3396 |
complement:true squashDuplicates:false |
|
3397 |
||
3398 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3399 |
complement:true squashDuplicates:false |
|
3400 |
||
3401 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3402 |
complement:false squashDuplicates:false |
|
3403 |
||
3404 |
- delete all a-zA-Z |
|
3405 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3406 |
complement:false squashDuplicates:false |
|
3407 |
||
3408 |
- delete all except a-zA-Z |
|
3409 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3410 |
complement:true squashDuplicates:false |
|
3411 |
||
3412 |
- squash multiple spaces |
|
3413 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
|
3414 |
complement:false squashDuplicates:true |
|
3415 |
||
3416 |
- squash multiple digits |
|
3417 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
|
3418 |
complement:false squashDuplicates:true |
|
3419 |
" |
|
3420 |
! |
|
3421 |
||
602 | 3422 |
copyUpTo:element |
3423 |
"return a new collection consisting of the receiver elements |
|
8874 | 3424 |
up-to (but excluding) the first occurrence of element; |
602 | 3425 |
or to the end, if element is not included" |
3426 |
||
3427 |
|idx| |
|
3428 |
||
3429 |
idx := self indexOf:element. |
|
3430 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3431 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
3432 |
^ self copyFrom:1 to:(idx-1) |
|
3433 |
||
3434 |
" |
|
3435 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
3436 |
'1234567890' copyUpTo:$5 |
|
3437 |
'1234567890' copyUpTo:$a |
|
3438 |
'1234567890' copyUpTo:$1 |
|
3439 |
'123456789' copyUpTo:$0 |
|
3440 |
" |
|
3441 |
! |
|
3442 |
||
1 | 3443 |
copyWith:newElement |
233 | 3444 |
"return a new collection containing the receivers elements |
8874 | 3445 |
and the single new element, newElement. |
233 | 3446 |
This is different from concatentation, which expects another collection |
3447 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3448 |
|
3449 |
|newCollection mySize newSize| |
|
3450 |
||
3451 |
mySize := self size. |
|
3452 |
newSize := mySize + 1. |
|
252 | 3453 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3454 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3455 |
newCollection at:newSize put:newElement. |
|
3456 |
^newCollection |
|
44 | 3457 |
|
95 | 3458 |
" |
3459 |
#(1 2 3 4 5) copyWith:$a |
|
3460 |
'abcdefg' copyWith:$h |
|
3461 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3462 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3463 |
" |
|
1 | 3464 |
! |
3465 |
||
11855 | 3466 |
copyWithFirst:newFirstElement |
3467 |
"return a new collection containing the receivers elements |
|
3468 |
and the single new element, newElement up front. |
|
3469 |
This is different from concatentation, which expects two collections |
|
3470 |
as argument, but equivalent to copy-and-addFirst." |
|
3471 |
||
3472 |
|newCollection mySize newSize| |
|
3473 |
||
3474 |
mySize := self size. |
|
3475 |
newSize := mySize + 1. |
|
3476 |
newCollection := self copyEmptyAndGrow:newSize. |
|
3477 |
newCollection replaceFrom:2 to:mySize+1 with:self startingAt:1. |
|
3478 |
newCollection at:1 put:newFirstElement. |
|
3479 |
^newCollection |
|
3480 |
||
3481 |
" |
|
3482 |
#(1 2 3 4 5) copyWithFirst:$a |
|
3483 |
'abcdefg' copyWithFirst:$h |
|
3484 |
'abcdefg' copyWithFirst:'123' -- will fail: string cannot be stored into string |
|
3485 |
'abcdefg' copyWithFirst:1 -- will fail: integer cannot be stored into string |
|
3486 |
" |
|
3487 |
! |
|
3488 |
||
61 | 3489 |
copyWithout:elementToSkip |
3490 |
"return a new collection consisting of a copy of the receiver, with |
|
3491 |
ALL elements equal to elementToSkip are left out. |
|
3492 |
No error is reported, if elementToSkip is not in the collection." |
|
3493 |
||
293 | 3494 |
|n "{ Class: SmallInteger }" |
3495 |
sz "{ Class: SmallInteger }" |
|
3496 |
srcIndex "{ Class: SmallInteger }" |
|
3497 |
dstIndex "{ Class: SmallInteger }" |
|
3498 |
skipIndex "{ Class: SmallInteger }" |
|
3499 |
copy l| |
|
61 | 3500 |
|
8874 | 3501 |
"the code below may look like overkill, |
61 | 3502 |
however, for big collections its better to move data |
3503 |
around in big chunks" |
|
3504 |
||
3505 |
n := self occurrencesOf:elementToSkip. |
|
3506 |
n == 0 ifTrue:[^ self copy]. |
|
3507 |
||
3508 |
sz := self size. |
|
252 | 3509 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3510 |
|
3511 |
srcIndex := 1. |
|
3512 |
dstIndex := 1. |
|
3513 |
||
3514 |
n timesRepeat:[ |
|
8874 | 3515 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3516 |
l := skipIndex - srcIndex. |
|
3517 |
l ~~ 0 ifTrue:[ |
|
3518 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3519 |
with:self startingAt:srcIndex. |
|
3520 |
dstIndex := dstIndex + l |
|
3521 |
]. |
|
3522 |
srcIndex := skipIndex + 1 |
|
61 | 3523 |
]. |
3524 |
l := sz - srcIndex. |
|
3525 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3526 |
with:self startingAt:srcIndex. |
61 | 3527 |
^ copy |
3528 |
||
95 | 3529 |
" |
3530 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3531 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3532 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3533 |
'abcdefghi' copyWithout:$h |
3534 |
'abcdefg' copyWithout:$h |
|
95 | 3535 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3536 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3537 |
#(90 80 70 60 50) copyWithout:70 |
|
3538 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3539 |
" |
|
61 | 3540 |
! |
3541 |
||
602 | 3542 |
copyWithoutFirst:elementToSkip |
3543 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3544 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3545 |
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
|
3546 |
Do not confuse this with copyButFirst:" |
602 | 3547 |
|
3548 |
|copy skipIndex sz| |
|
3549 |
||
3550 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3551 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3552 |
||
3553 |
sz := self size - 1. |
|
3554 |
copy := self copyEmptyAndGrow:sz. |
|
3555 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3556 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3557 |
^ copy |
|
3558 |
||
3559 |
" |
|
3560 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3561 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3562 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3563 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3564 |
" |
|
3565 |
! |
|
3566 |
||
8673 | 3567 |
copyWithoutIdentical:elementToSkip |
3568 |
"return a new collection consisting of a copy of the receiver, with |
|
3569 |
ALL elements identical to elementToSkip are left out. |
|
3570 |
No error is reported, if elementToSkip is not in the collection." |
|
3571 |
||
3572 |
|n "{ Class: SmallInteger }" |
|
3573 |
sz "{ Class: SmallInteger }" |
|
3574 |
srcIndex "{ Class: SmallInteger }" |
|
3575 |
dstIndex "{ Class: SmallInteger }" |
|
3576 |
skipIndex "{ Class: SmallInteger }" |
|
3577 |
copy l| |
|
3578 |
||
8874 | 3579 |
"the code below may look like overkill, |
8673 | 3580 |
however, for big collections its better to move data |
3581 |
around in big chunks" |
|
3582 |
||
3583 |
n := self count:[:el | el == elementToSkip]. |
|
3584 |
n == 0 ifTrue:[^ self copy]. |
|
3585 |
||
3586 |
sz := self size. |
|
3587 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3588 |
||
3589 |
srcIndex := 1. |
|
3590 |
dstIndex := 1. |
|
3591 |
||
3592 |
n timesRepeat:[ |
|
8874 | 3593 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3594 |
l := skipIndex - srcIndex. |
|
3595 |
l ~~ 0 ifTrue:[ |
|
3596 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3597 |
with:self startingAt:srcIndex. |
|
3598 |
dstIndex := dstIndex + l |
|
3599 |
]. |
|
3600 |
srcIndex := skipIndex + 1 |
|
8673 | 3601 |
]. |
3602 |
l := sz - srcIndex. |
|
3603 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3604 |
with:self startingAt:srcIndex. |
8673 | 3605 |
^ copy |
3606 |
||
3607 |
" |
|
8874 | 3608 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3609 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3610 |
" |
3611 |
! |
|
3612 |
||
61 | 3613 |
copyWithoutIndex:omitIndex |
3614 |
"return a new collection consisting of receivers elements |
|
3615 |
without the argument stored at omitIndex" |
|
3616 |
||
3617 |
|copy sz| |
|
3618 |
||
202 | 3619 |
sz := self size - 1. |
252 | 3620 |
copy := self copyEmptyAndGrow:sz. |
293 | 3621 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3622 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3623 |
^ copy |
3624 |
||
95 | 3625 |
" |
3626 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3627 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3628 |
" |
|
1 | 3629 |
! |
3630 |
||
202 | 3631 |
copyWithoutLast:count |
3632 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3633 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3634 |
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
|
3635 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3636 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3637 |
|
9250 | 3638 |
<resource:#obsolete> |
3639 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3640 |
^ self copyTo:(self size - count) |
202 | 3641 |
|
3642 |
" |
|
8874 | 3643 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3644 |
'1234567890' copyWithoutLast:4 |
|
202 | 3645 |
" |
5231 | 3646 |
! |
3647 |
||
3648 |
restAfter:anElement |
|
8366 | 3649 |
"return a new collection consisting of the receivers elements after |
5231 | 3650 |
(but excluding) anElement. |
8366 | 3651 |
If anElement is not in the receiver, the returned collection |
3652 |
will be empty. |
|
3653 |
See also #upTo:." |
|
5231 | 3654 |
|
3655 |
|pos| |
|
3656 |
||
3657 |
pos := self indexOf:anElement. |
|
8366 | 3658 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3659 |
||
5231 | 3660 |
^ self copyFrom:(pos + 1) |
3661 |
||
3662 |
" |
|
8874 | 3663 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3664 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3665 |
'hello world' upTo:Character space |
|
3666 |
'hello world' restAfter:Character space |
|
3667 |
'1234.5678' upTo:$. |
|
3668 |
'1234.5678' restAfter:$. |
|
8366 | 3669 |
" |
5231 | 3670 |
! |
3671 |
||
3672 |
upTo:anElement |
|
3673 |
"return a new collection consisting of the receivers elements upTo |
|
3674 |
(but excluding) anElement. |
|
3675 |
If anElement is not in the receiver, the returned collection |
|
3676 |
will consist of all elements of the receiver. |
|
9307 | 3677 |
See also #restAfter: , #copyFrom:index." |
5231 | 3678 |
|
3679 |
|pos| |
|
3680 |
||
3681 |
pos := self indexOf:anElement. |
|
3682 |
pos == 0 ifTrue:[^ self copy]. |
|
3683 |
||
3684 |
^ self copyFrom:1 to:(pos - 1) |
|
3685 |
||
3686 |
" |
|
8874 | 3687 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3688 |
'hello world' upTo:Character space |
|
3689 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3690 |
'1234.5678' upTo:$. |
|
3691 |
'1234' upTo:$. |
|
5231 | 3692 |
|
3693 |
raises an error: |
|
3694 |
||
8874 | 3695 |
(Dictionary new |
9307 | 3696 |
at:#foo put:'foo'; |
3697 |
at:#bar put:'bar'; |
|
3698 |
at:#baz put:'baz'; |
|
3699 |
yourself) upTo:#bar |
|
8874 | 3700 |
|
5231 | 3701 |
" |
3702 |
||
3703 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3704 |
! |
|
3705 |
||
3706 |
upTo:anElement count:count |
|
3707 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3708 |
(but excluding) count found anElements, i.e. the first found count-1 |
3709 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3710 |
procedure is done from the end of the collection backwards. |
3711 |
If anElement is not in the receiver, the returned collection |
|
3712 |
will consist of all elements of the receiver" |
|
3713 |
||
3714 |
|startPos endPos pos found| |
|
3715 |
||
3716 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3717 |
^ self upTo:anElement |
5231 | 3718 |
]. |
3719 |
||
3720 |
startPos := 1. |
|
3721 |
endPos := self size. |
|
3722 |
found := 0. |
|
3723 |
||
3724 |
count > 1 ifTrue:[ |
|
8874 | 3725 |
pos := 0. |
3726 |
[pos < self size and: [found < count]] |
|
3727 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3728 |
found == count ifTrue:[ |
|
3729 |
endPos := pos - 1 |
|
3730 |
] |
|
3731 |
]. |
|
5231 | 3732 |
|
3733 |
count < 0 ifTrue:[ |
|
8874 | 3734 |
pos := self size + 1. |
3735 |
[pos > 1 and: [found < count abs]] |
|
3736 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3737 |
found == count abs ifTrue:[ |
|
3738 |
startPos := pos + 1 |
|
3739 |
] |
|
3740 |
]. |
|
5231 | 3741 |
|
3742 |
^ self copyFrom: startPos to: endPos |
|
3743 |
||
3744 |
" |
|
8874 | 3745 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3746 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3747 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3748 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3749 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3750 |
" |
|
5563 | 3751 |
! |
3752 |
||
3753 |
upToAny:aCollectionOfObjects |
|
3754 |
"return a new collection consisting of the receivers elements upTo |
|
3755 |
(but excluding) any in aCollectionOfObjects. |
|
3756 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
3757 |
will consist of all elements of the receiver. |
|
3758 |
See also #upTo:" |
|
3759 |
||
3760 |
|pos| |
|
3761 |
||
3762 |
pos := self indexOfAny:aCollectionOfObjects. |
|
3763 |
pos == 0 ifTrue:[^ self copy]. |
|
3764 |
||
3765 |
^ self copyFrom:1 to:(pos - 1) |
|
3766 |
||
3767 |
" |
|
8874 | 3768 |
'hello world' upToAny:#($/ $:) |
3769 |
'hello:world' upToAny:#($/ $:) |
|
3770 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 3771 |
" |
602 | 3772 |
! ! |
3773 |
||
3774 |
!SequenceableCollection methodsFor:'enumerating'! |
|
3775 |
||
3776 |
collect:aBlock |
|
3777 |
"evaluate the argument, aBlock for every element in the collection |
|
3778 |
and return a collection of the results" |
|
3779 |
||
3780 |
|newCollection |
|
3781 |
sz "{ Class:SmallInteger }"| |
|
3782 |
||
3783 |
sz := self size. |
|
3784 |
newCollection := self copyEmptyAndGrow:sz. |
|
3785 |
1 to:sz do:[:index | |
|
8874 | 3786 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 3787 |
]. |
3788 |
^ newCollection |
|
3789 |
||
3790 |
" |
|
8874 | 3791 |
#(one two three four five six) collect:[:element | element asUppercase] |
3792 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
3793 |
(1 to:9) collect:[:element | element * element] |
|
602 | 3794 |
" |
202 | 3795 |
! |
3796 |
||
11711 | 3797 |
collect:aBlock as:aClass |
11870 | 3798 |
"like collect, but use an instance of aClass to collect the results. |
3799 |
Also avoids the need for an extra intermediate collection which is created with |
|
3800 |
the standard coding: 'self asXXXX collect:[...]" |
|
11711 | 3801 |
|
3802 |
|newCollection |
|
3803 |
sz "{ Class:SmallInteger }"| |
|
3804 |
||
3805 |
sz := self size. |
|
3806 |
newCollection := (aClass new:sz) grow:sz. |
|
3807 |
1 to:sz do:[:index | |
|
3808 |
newCollection at:index put:(aBlock value:(self at:index)). |
|
3809 |
]. |
|
3810 |
^ newCollection |
|
3811 |
||
3812 |
" |
|
3813 |
#(one two three four five six) collect:[:element | element asUppercase] as:OrderedCollection |
|
3814 |
'abcdef' collect:[:char | char digitValue] as:ByteArray |
|
3815 |
" |
|
3816 |
! |
|
3817 |
||
602 | 3818 |
do:aBlock |
3819 |
"evaluate the argument, aBlock for every element in the collection." |
|
3820 |
||
3821 |
|stop "{ Class:SmallInteger }"| |
|
3822 |
||
3823 |
stop := self size. |
|
3824 |
1 to:stop do:[:index | |
|
8874 | 3825 |
aBlock value:(self at:index). |
602 | 3826 |
] |
2927 | 3827 |
|
602 | 3828 |
" |
1422 | 3829 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 3830 |
" |
3831 |
! |
|
3832 |
||
2927 | 3833 |
do:aBlock separatedBy:sepBlock |
3834 |
"evaluate the argument, aBlock for every element in the collection. |
|
3835 |
Between each element (i.e. not before the first element and not after the |
|
3836 |
last element), evaluate sepBlock. |
|
3837 |
This supports printing of collections with elements separated by some |
|
3838 |
string." |
|
3839 |
||
3840 |
|stop "{ Class:SmallInteger }"| |
|
3841 |
||
3842 |
stop := self size. |
|
3843 |
stop < 1 ifTrue:[^ self]. |
|
3844 |
aBlock value:(self at:1). |
|
3845 |
2 to:stop do:[:index | |
|
8874 | 3846 |
sepBlock value. |
3847 |
aBlock value:(self at:index) |
|
2927 | 3848 |
]. |
3849 |
||
3850 |
" |
|
3851 |
#(one two three four five six) |
|
8874 | 3852 |
do:[:each | Transcript show:each] |
3853 |
separatedBy:[Transcript show:' , '] |
|
2927 | 3854 |
" |
3855 |
! |
|
3003 | 3856 |
|
3044 | 3857 |
doWithIndex:aBlock |
3858 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
3859 |
the index as second argument." |
|
3860 |
||
3861 |
self keysAndValuesDo:[:index :value | |
|
8874 | 3862 |
aBlock value:value value:index |
3044 | 3863 |
]. |
3864 |
||
3865 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
3866 |
! |
|
3867 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3868 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3869 |
"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
|
3870 |
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
|
3871 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3872 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3873 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3874 |
" |
8874 | 3875 |
#(one two three four five six) |
3876 |
from:2 |
|
3877 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3878 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3879 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3880 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3881 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
3882 |
|
602 | 3883 |
from:startIndex do:aBlock |
3884 |
"evaluate the argument, aBlock for the elements starting with the |
|
3885 |
element at startIndex to the end." |
|
3886 |
||
3887 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 3888 |
|
95 | 3889 |
" |
8874 | 3890 |
#(one two three four five six) |
3891 |
from:3 |
|
3892 |
do:[:element | Transcript showCR:element] |
|
602 | 3893 |
" |
3894 |
! |
|
3895 |
||
7490 | 3896 |
from:startIndex keysAndValuesDo:aBlock |
3897 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
3898 |
element at startIndex to the end." |
|
3899 |
||
3900 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
3901 |
||
3902 |
" |
|
8874 | 3903 |
#(one two three four five six) |
3904 |
from:3 |
|
3905 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 3906 |
" |
3907 |
! |
|
3908 |
||
7337 | 3909 |
from:index1 to:index2 by:stepArg do:aBlock |
3910 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
3911 |
||
3912 |
|start "{ Class:SmallInteger }" |
|
3913 |
stop "{ Class:SmallInteger }" |
|
3914 |
step "{ Class:SmallInteger }" | |
|
3915 |
||
3916 |
start := index1. "/ these assignments force type checking... |
|
3917 |
stop := index2. "/ and guarantee inline loop code below. |
|
3918 |
step := stepArg. |
|
3919 |
||
3920 |
start to:stop by:step do:[:index | |
|
8874 | 3921 |
aBlock value:(self at:index). |
7337 | 3922 |
] |
3923 |
||
3924 |
" |
|
8874 | 3925 |
#(one two three four five six seven eight nine ten) |
3926 |
from:2 |
|
3927 |
to:10 |
|
3928 |
by:2 |
|
3929 |
do:[:element | Transcript showCR:element] |
|
7337 | 3930 |
" |
3931 |
! |
|
3932 |
||
602 | 3933 |
from:start to:stop collect:aBlock |
3934 |
"evaluate the argument, aBlock for the elements indexed by start |
|
3935 |
to stop in the collection and return a collection of the results" |
|
3936 |
||
3937 |
|newCollection sz |
|
3938 |
idx "{ Class:SmallInteger }"| |
|
3939 |
||
3940 |
sz := stop - start + 1. |
|
3941 |
newCollection := self copyEmptyAndGrow:sz. |
|
3942 |
idx := 1. |
|
3943 |
start to:stop do:[:index | |
|
8874 | 3944 |
newCollection at:idx put:(aBlock value:(self at:index)). |
3945 |
idx := idx + 1 |
|
602 | 3946 |
]. |
3947 |
^ newCollection |
|
3948 |
||
3949 |
" |
|
8874 | 3950 |
#(one two three four five six) |
3951 |
from:2 |
|
3952 |
to:4 |
|
3953 |
collect:[:element | element asUppercase] |
|
602 | 3954 |
" |
3955 |
! |
|
3956 |
||
3957 |
from:index1 to:index2 do:aBlock |
|
3958 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3959 |
index2 in the collection" |
|
3960 |
||
3961 |
|start "{ Class:SmallInteger }" |
|
3962 |
stop "{ Class:SmallInteger }" | |
|
3963 |
||
7337 | 3964 |
start := index1. "/ these assignments force type checking... |
3965 |
stop := index2. "/ and guarantee inline loop code below. |
|
3966 |
||
602 | 3967 |
start to:stop do:[:index | |
8874 | 3968 |
aBlock value:(self at:index). |
602 | 3969 |
] |
3970 |
||
3971 |
" |
|
8874 | 3972 |
#(one two three four five six) |
3973 |
from:3 |
|
3974 |
to:5 |
|
3975 |
do:[:element | Transcript showCR:element] |
|
602 | 3976 |
" |
3977 |
! |
|
3978 |
||
6868 | 3979 |
from:index1 to:index2 keysAndValuesDo:aBlock |
3980 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
3981 |
index2 in the collection; pass both index and value." |
|
3982 |
||
3983 |
|start "{ Class:SmallInteger }" |
|
3984 |
stop "{ Class:SmallInteger }" | |
|
3985 |
||
7337 | 3986 |
start := index1. "/ these assignments force type checking... |
3987 |
stop := index2. "/ and guarantee inline loop code below. |
|
3988 |
||
6868 | 3989 |
start to:stop do:[:index | |
8874 | 3990 |
aBlock value:index value:(self at:index). |
6868 | 3991 |
] |
3992 |
||
3993 |
" |
|
8874 | 3994 |
#(one two three four five six) |
3995 |
from:3 |
|
3996 |
to:5 |
|
3997 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 3998 |
" |
3999 |
! |
|
4000 |
||
602 | 4001 |
from:index1 to:index2 reverseDo:aBlock |
4002 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4003 |
index2 in the collection. Step in reverse order" |
|
4004 |
||
4005 |
|start "{ Class:SmallInteger }" |
|
4006 |
stop "{ Class:SmallInteger }" | |
|
4007 |
||
7337 | 4008 |
start := index1. "/ these assignments force type checking... |
4009 |
stop := index2. "/ and guarantee inline loop code below. |
|
4010 |
||
602 | 4011 |
stop to:start by:-1 do:[:index | |
8874 | 4012 |
aBlock value:(self at:index). |
602 | 4013 |
] |
4014 |
||
4015 |
" |
|
8874 | 4016 |
#(one two three four five six) |
4017 |
from:3 |
|
4018 |
to:5 |
|
4019 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 4020 |
" |
61 | 4021 |
! |
4022 |
||
6027 | 4023 |
from:startIndex to:stopIndex select:aBlock |
4024 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
4025 |
and return a collection of those elements for which the block return true." |
|
4026 |
||
4027 |
|element newColl species needCopy |
|
4028 |
n "{ Class:SmallInteger }"| |
|
4029 |
||
4030 |
n := stopIndex - startIndex + 1. |
|
4031 |
species := self species. |
|
4032 |
species growIsCheap ifFalse:[ |
|
8874 | 4033 |
newColl := OrderedCollection new:n. |
4034 |
needCopy := true |
|
6027 | 4035 |
] ifTrue:[ |
8874 | 4036 |
newColl := self copyEmpty:n. |
4037 |
needCopy := false |
|
6027 | 4038 |
]. |
4039 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4040 |
element := self at:index. |
4041 |
(aBlock value:element) ifTrue:[ |
|
4042 |
newColl add:element |
|
4043 |
]. |
|
6027 | 4044 |
]. |
4045 |
needCopy ifTrue:[ |
|
8874 | 4046 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 4047 |
]. |
4048 |
^ newColl |
|
4049 |
||
4050 |
" |
|
8874 | 4051 |
#(faba one two three four five six) |
4052 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 4053 |
" |
4054 |
! |
|
4055 |
||
10150 | 4056 |
inGroupsOf:n collect:anNArgBlock |
4057 |
"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
|
4058 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4059 |
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
|
4060 |
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
|
4061 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4062 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4063 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4064 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4065 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4066 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4067 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4068 |
|
10150 | 4069 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
4070 |
"/ 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
|
4071 |
n == 2 ifTrue:[ |
10150 | 4072 |
1 to:stop by:n do:[:index | |
4073 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4074 |
newCollection at:dstIdx put:rslt. |
|
4075 |
dstIdx := dstIdx + 1. |
|
4076 |
]. |
|
4077 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4078 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4079 |
n == 3 ifTrue:[ |
10150 | 4080 |
1 to:stop by:n do:[:index | |
4081 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4082 |
newCollection at:dstIdx put:rslt. |
|
4083 |
dstIdx := dstIdx + 1. |
|
4084 |
]. |
|
4085 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4086 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4087 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4088 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4089 |
1 to:stop by:n do:[:index | |
10150 | 4090 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4091 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
4092 |
newCollection at:dstIdx put:rslt. |
|
4093 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4094 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4095 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4096 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4097 |
"for groups of 2, this is the same as: |
8874 | 4098 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4099 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 4100 |
|
4101 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4102 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4103 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4104 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4105 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
4106 |
" |
|
4107 |
||
4108 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
4109 |
! |
|
4110 |
||
4111 |
inGroupsOf:n do:anNArgBlock |
|
4112 |
"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
|
4113 |
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
|
4114 |
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
|
4115 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4116 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4117 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4118 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4119 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4120 |
|
10150 | 4121 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
4122 |
"/ 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
|
4123 |
n == 2 ifTrue:[ |
10150 | 4124 |
1 to:stop by:n do:[:index | |
4125 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4126 |
]. |
|
4127 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4128 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4129 |
n == 3 ifTrue:[ |
10150 | 4130 |
1 to:stop by:n do:[:index | |
4131 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4132 |
]. |
|
4133 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4134 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4135 |
n == 4 ifTrue:[ |
10150 | 4136 |
1 to:stop by:n do:[:index | |
4137 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
4138 |
]. |
|
4139 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4140 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4141 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4142 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4143 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4144 |
1 to:stop by:n do:[:index | |
10150 | 4145 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4146 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4147 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4148 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4149 |
"for groups of 2, this is the same as: |
8874 | 4150 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4151 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4152 |
|
4153 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4154 |
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
|
4155 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4156 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4157 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
4158 |
" |
|
4159 |
||
4160 |
"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
|
4161 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4162 |
|
8853 | 4163 |
keysAndValuesCollect:aTwoArgBlock |
4164 |
"evaluate the argument, aBlock for every element in the collection, |
|
4165 |
passing both index and element as arguments. |
|
4166 |
Collect the returned values and return them." |
|
4167 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4168 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4169 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4170 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4171 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4172 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4173 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 4174 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4175 |
|
8853 | 4176 |
^ newCollection |
4177 |
||
4178 |
" |
|
8874 | 4179 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4180 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4181 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 4182 |
|
4183 |
||
4184 |
|d| |
|
4185 |
||
4186 |
d := Dictionary new. |
|
4187 |
d at:'a' put:'A'. |
|
4188 |
d at:'b' put:'B'. |
|
4189 |
d at:'c' put:'C'. |
|
4190 |
d at:'d' put:'D'. |
|
4191 |
d at:'e' put:'E'. |
|
8874 | 4192 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4193 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4194 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4195 |
ifFalse:[element asLowercase]] |
8853 | 4196 |
" |
4197 |
! |
|
4198 |
||
602 | 4199 |
keysAndValuesDo:aTwoArgBlock |
4200 |
"evaluate the argument, aBlock for every element in the collection, |
|
4201 |
passing both index and element as arguments." |
|
4202 |
||
4203 |
|stop "{ Class:SmallInteger }"| |
|
4204 |
||
4205 |
stop := self size. |
|
4206 |
1 to:stop do:[:index | |
|
8874 | 4207 |
aTwoArgBlock value:index value:(self at:index). |
602 | 4208 |
] |
4209 |
" |
|
8874 | 4210 |
#(one two three four five six) |
4211 |
keysAndValuesDo:[:key :element | |
|
4212 |
Transcript show:key; space; showCR:element |
|
4213 |
] |
|
602 | 4214 |
" |
4215 |
! |
|
4216 |
||
1359 | 4217 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 4218 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 4219 |
in the collection, passing both index and element as arguments." |
4220 |
||
4221 |
|stop "{ Class:SmallInteger }"| |
|
4222 |
||
4223 |
stop := self size. |
|
4224 |
stop to:1 by:-1 do:[:index | |
|
8874 | 4225 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 4226 |
] |
4227 |
||
4228 |
" |
|
8874 | 4229 |
#(one two three four five six) |
4230 |
keysAndValuesReverseDo:[:key :element | |
|
4231 |
Transcript show:key; space; showCR:element |
|
4232 |
] |
|
1359 | 4233 |
" |
4234 |
||
4235 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
4236 |
! |
|
4237 |
||
602 | 4238 |
nonNilElementsDo:aBlock |
4239 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
4240 |
||
4241 |
|stop "{ Class:SmallInteger }" |
|
4242 |
element| |
|
4243 |
||
4244 |
stop := self size. |
|
4245 |
1 to:stop do:[:index | |
|
8874 | 4246 |
(element := self at:index) notNil ifTrue:[ |
4247 |
aBlock value:element. |
|
4248 |
] |
|
602 | 4249 |
] |
4250 |
" |
|
1422 | 4251 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 4252 |
" |
4253 |
! |
|
4254 |
||
10150 | 4255 |
pairWiseCollect:aTwoArgBlock |
4256 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 4257 |
The block is called with 2 arguments for each 2 elements in the receiver. |
4258 |
An error will be reported, if the number of elements in the receiver |
|
4259 |
is not a multiple of 2. |
|
4260 |
Collect the results and return a new collection containing those." |
|
4261 |
||
10150 | 4262 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 4263 |
|
95 | 4264 |
" |
8874 | 4265 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4266 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 4267 |
|
4268 |
||
4269 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4270 |
pairWiseCollect:[:x :y | x@y] |
4271 |
" |
|
4272 |
||
4273 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
4274 |
! |
|
4275 |
||
4276 |
pairWiseDo:aTwoArgBlock |
|
10385 | 4277 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
4278 |
An error will be reported, if the number of elements in the receiver |
|
602 | 4279 |
is not a multiple of 2." |
4280 |
||
10150 | 4281 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4282 |
|
602 | 4283 |
" |
8874 | 4284 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4285 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4286 |
|
4287 |
||
4288 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4289 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
4290 |
" |
|
4291 |
||
10385 | 4292 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 4293 |
! |
4294 |
||
602 | 4295 |
reverseDo:aBlock |
4296 |
"evaluate the argument, aBlock for every element in the collection |
|
4297 |
in reverse order" |
|
4298 |
||
4299 |
|sz "{ Class:SmallInteger }"| |
|
4300 |
||
4301 |
sz := self size. |
|
4302 |
sz to:1 by:-1 do:[:index | |
|
8874 | 4303 |
aBlock value:(self at:index). |
602 | 4304 |
] |
4305 |
||
4306 |
" |
|
1422 | 4307 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 4308 |
" |
4309 |
! |
|
4310 |
||
4311 |
select:aBlock |
|
4312 |
"evaluate the argument, aBlock for every element in the collection |
|
4313 |
and return a collection of all elements for which the block return |
|
4314 |
true" |
|
4315 |
||
4316 |
|element newColl species needCopy |
|
4317 |
sz "{ Class:SmallInteger }"| |
|
4318 |
||
4319 |
sz := self size. |
|
4320 |
species := self species. |
|
4321 |
species growIsCheap ifFalse:[ |
|
8874 | 4322 |
newColl := OrderedCollection new:sz. |
4323 |
needCopy := true |
|
602 | 4324 |
] ifTrue:[ |
8874 | 4325 |
newColl := self copyEmpty:sz. |
4326 |
needCopy := false |
|
602 | 4327 |
]. |
4328 |
1 to:sz do:[:index | |
|
8874 | 4329 |
element := self at:index. |
4330 |
(aBlock value:element) ifTrue:[ |
|
4331 |
newColl add:element |
|
4332 |
]. |
|
602 | 4333 |
]. |
4334 |
needCopy ifTrue:[ |
|
8874 | 4335 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 4336 |
]. |
61 | 4337 |
^ newColl |
4338 |
||
95 | 4339 |
" |
8874 | 4340 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
4341 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
4342 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
4343 |
(1 to:9) select:[:element | element odd] |
|
4344 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 4345 |
" |
186 | 4346 |
! |
4347 |
||
602 | 4348 |
with:aSequenceableCollection do:aTwoArgBlock |
4349 |
"evaluate the argument, aBlock for successive elements from |
|
4350 |
each the receiver and the argument, aSequenceableCollection. |
|
4351 |
The second argument, aBlock must be a two-argument block. |
|
4352 |
The collection argument must implement access via a numeric key." |
|
4353 |
||
4354 |
|stop "{ Class: SmallInteger }" | |
|
4355 |
||
4356 |
stop := self size. |
|
4357 |
1 to:stop do:[:index | |
|
8874 | 4358 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 4359 |
] |
186 | 4360 |
" |
8874 | 4361 |
#(one two three four five six) |
4362 |
with:(1 to:10) |
|
4363 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 4364 |
" |
9454 | 4365 |
! |
4366 |
||
4367 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
4368 |
"evaluate the argument, aBlock for successive elements from |
|
4369 |
each the receiver and the argument, aSequenceableCollection. |
|
4370 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4371 |
the index as arguments. |
|
4372 |
The collection argument must implement access via a numeric key." |
|
4373 |
||
4374 |
|stop "{ Class: SmallInteger }" | |
|
4375 |
||
4376 |
stop := self size. |
|
4377 |
1 to:stop do:[:index | |
|
4378 |
aThreeArgBlock |
|
4379 |
value:(self at:index) |
|
4380 |
value:(aSequenceableCollection at:index) |
|
4381 |
value:index. |
|
4382 |
] |
|
4383 |
" |
|
4384 |
#(one two three four five six) |
|
4385 |
with:(1 to:10) |
|
4386 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4387 |
" |
|
4388 |
||
4389 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 4390 |
! ! |
4391 |
||
4154 | 4392 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 4393 |
|
602 | 4394 |
atAllPut:anObject |
4395 |
"replace all elements of the collection by the argument, anObject. |
|
4396 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4397 |
therefore the change may affect all others referencing the receiver." |
|
4398 |
||
4399 |
self from:1 to:(self size) put:anObject |
|
4400 |
||
4401 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4402 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4403 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4404 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4405 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4406 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4407 |
" |
602 | 4408 |
(Array new:10) atAllPut:1 |
4409 |
(String new:10) atAllPut:$a |
|
4410 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4411 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4412 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 4413 |
! |
4414 |
||
1 | 4415 |
from:index1 to:index2 put:anObject |
4416 |
"replace the elements from index1 to index2 of the collection |
|
44 | 4417 |
by the argument, anObject. |
4418 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4419 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4420 |
|
4421 |
|index "{ Class: SmallInteger }" |
|
4422 |
end "{ Class: SmallInteger }"| |
|
4423 |
||
4424 |
index := index1. |
|
4425 |
end := index2. |
|
4426 |
[index <= end] whileTrue:[ |
|
8874 | 4427 |
self at:index put:anObject. |
4428 |
index := index + 1 |
|
1 | 4429 |
] |
44 | 4430 |
|
95 | 4431 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4432 |
args: index1 : <integer> |
8874 | 4433 |
index2 : <integer> |
4434 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4435 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4436 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4437 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4438 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4439 |
" |
95 | 4440 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
4441 |
'abcdefghijkl' from:3 to:6 put:$X |
|
4442 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4443 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4444 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 4445 |
! |
4446 |
||
3003 | 4447 |
replaceAll:oldObject by:newObject |
4448 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
4449 |
Pleace do no longer use this one; |
|
4450 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
4451 |
||
5872 | 4452 |
<resource:#obsolete> |
4453 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4454 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 4455 |
^ self replaceAll:oldObject with:newObject |
4456 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4457 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4458 |
args: oldObject : <object> |
8874 | 4459 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4460 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4461 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4462 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4463 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4464 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 4465 |
! |
4466 |
||
4467 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
4468 |
"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
|
4469 |
Please do no longer use this one; |
3003 | 4470 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
4471 |
||
5872 | 4472 |
<resource:#obsolete> |
4473 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4474 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 4475 |
^ 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
|
4476 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4477 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4478 |
args: oldObject : <object> |
8874 | 4479 |
newObject : <object> |
4480 |
startIndex : <integer> |
|
4481 |
stopIndex : <integer> |
|
4482 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4483 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4484 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4485 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4486 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 4487 |
! |
4488 |
||
2927 | 4489 |
replaceAll:oldObject with:newObject |
44 | 4490 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4491 |
|
44 | 4492 |
Notice: This operation modifies the receiver, NOT a copy; |
4493 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4494 |
|
8874 | 4495 |
^ self |
4496 |
replaceAll:oldObject |
|
4497 |
with:newObject |
|
4498 |
from:1 |
|
4499 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4500 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4501 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4502 |
args: oldObject : <object> |
8874 | 4503 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4504 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4505 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4506 |
" |
362 | 4507 |
|
4508 |
" |
|
8874 | 4509 |
'123123abc123' replaceAll:$1 with:$* |
4510 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4511 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4512 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4513 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4514 |
! |
293 | 4515 |
|
2927 | 4516 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4517 |
"replace all oldObjects found between startIndex and endIndex, |
4518 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4519 |
|
362 | 4520 |
Notice: This operation modifies the receiver, NOT a copy; |
4521 |
therefore the change may affect all others referencing the receiver." |
|
4522 |
||
4523 |
|start "{ Class: SmallInteger }" |
|
4524 |
stop "{ Class: SmallInteger }"| |
|
4525 |
||
4526 |
start := startIndex. |
|
4527 |
stop := stopIndex. |
|
4528 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4529 |
(self at:index) = oldObject ifTrue:[ |
4530 |
self at:index put:newObject |
|
4531 |
] |
|
1 | 4532 |
] |
44 | 4533 |
|
95 | 4534 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4535 |
args: oldObject : <object> |
8874 | 4536 |
newObject : <object> |
4537 |
startIndex : <integer> |
|
4538 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4539 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4540 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4541 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4542 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4543 |
" |
2980 | 4544 |
'123123abc123' replaceAll:$1 with:$* |
4545 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4546 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4547 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4548 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4549 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4550 |
! |
4551 |
||
7615 | 4552 |
replaceAllForWhich:aConditionBlock with:newObject |
4553 |
"replace all elements for which aConditionBlock returns true |
|
4554 |
between startIndex and endIndex, by newObject in the receiver. |
|
4555 |
||
4556 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4557 |
therefore the change may affect all others referencing the receiver." |
|
4558 |
||
8874 | 4559 |
self |
4560 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4561 |
from:1 to:(self size) |
|
7615 | 4562 |
|
4563 |
" |
|
4564 |
args: aConditionBlock : <block> |
|
8874 | 4565 |
newObject : <object> |
7615 | 4566 |
|
4567 |
returns: self |
|
4568 |
" |
|
4569 |
! |
|
4570 |
||
4571 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4572 |
"replace all elements for which aConditionBlock returns true |
|
4573 |
between startIndex and endIndex, by newObject in the receiver. |
|
4574 |
||
4575 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4576 |
therefore the change may affect all others referencing the receiver." |
|
4577 |
||
4578 |
|start "{ Class: SmallInteger }" |
|
4579 |
stop "{ Class: SmallInteger }"| |
|
4580 |
||
4581 |
start := startIndex. |
|
4582 |
stop := stopIndex. |
|
4583 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4584 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4585 |
self at:index put:newObject |
|
4586 |
] |
|
7615 | 4587 |
] |
4588 |
||
4589 |
" |
|
4590 |
args: aConditionBlock : <block> |
|
8874 | 4591 |
newObject : <object> |
4592 |
startIndex : <integer> |
|
4593 |
stopIndex : <integer> |
|
7615 | 4594 |
|
4595 |
returns: self |
|
4596 |
" |
|
4597 |
! |
|
4598 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4599 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4600 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4601 |
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
|
4602 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4603 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4604 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4605 |
|
8874 | 4606 |
^ self |
4607 |
replaceAllIdentical:oldObject |
|
4608 |
with:newObject |
|
4609 |
from:1 |
|
4610 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4611 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4612 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4613 |
args: oldObject : <object> |
8874 | 4614 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4615 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4616 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4617 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4618 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4619 |
" |
8874 | 4620 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4621 |
#(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
|
4622 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4623 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4624 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4625 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4626 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4627 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4628 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4629 |
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
|
4630 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4631 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4632 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4633 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4634 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4635 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4636 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4637 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4638 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4639 |
startIndex to:stopIndex do:[:index | |
8874 | 4640 |
(self at:index) == oldObject ifTrue:[ |
4641 |
self at:index put:newObject |
|
4642 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4643 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4644 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4645 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4646 |
args: oldObject : <object> |
8874 | 4647 |
newObject : <object> |
4648 |
startIndex : <integer> |
|
4649 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4650 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4651 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4652 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4653 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4654 |
" |
8874 | 4655 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4656 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4657 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4658 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4659 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4660 |
|
3003 | 4661 |
replaceAny:aCollection by:newObject |
4662 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4663 |
Pleace do no longer use this one; |
3003 | 4664 |
instead use #replaceAny:with: for naming consistence." |
4665 |
||
5872 | 4666 |
<resource:#obsolete> |
4667 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4668 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4669 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4670 |
|
4671 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4672 |
args: aCollection : <colleciton of <object> > |
8874 | 4673 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4674 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4675 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4676 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4677 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4678 |
" |
3003 | 4679 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4680 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4681 |
" |
|
4682 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4683 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4684 |
! |
4685 |
||
3003 | 4686 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4687 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4688 |
Pleace do no longer use this one; |
3003 | 4689 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4690 |
|
5872 | 4691 |
<resource:#obsolete> |
4692 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4693 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 4694 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 4695 |
|
4696 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4697 |
args: aCollection : <colleciton of <object> > |
8874 | 4698 |
newObject : <object> |
4699 |
startIndex : <integer> |
|
4700 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4701 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4702 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4703 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4704 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4705 |
" |
8874 | 4706 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4707 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 4708 |
" |
4709 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4710 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 4711 |
! |
4712 |
||
4713 |
replaceAny:aCollection with:newObject |
|
4714 |
"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
|
4715 |
|
2927 | 4716 |
Notice: This operation modifies the receiver, NOT a copy; |
4717 |
therefore the change may affect all others referencing the receiver." |
|
4718 |
||
8874 | 4719 |
^ self |
10439 | 4720 |
replaceAny:aCollection |
4721 |
with:newObject |
|
4722 |
from:1 |
|
4723 |
to:self size |
|
4724 |
||
4725 |
" |
|
4726 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
4727 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
4728 |
" |
|
4729 |
||
4730 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 4731 |
! |
4732 |
||
2927 | 4733 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 4734 |
"replace all elements within a range, |
4735 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4736 |
|
362 | 4737 |
Notice: This operation modifies the receiver, NOT a copy; |
4738 |
therefore the change may affect all others referencing the receiver." |
|
4739 |
||
4740 |
|start "{ Class: SmallInteger }" |
|
4741 |
stop "{ Class: SmallInteger }"| |
|
4742 |
||
4743 |
start := startIndex. |
|
4744 |
stop := stopIndex. |
|
4745 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4746 |
(aCollection includes:(self at:index)) ifTrue:[ |
4747 |
self at:index put:newObject |
|
4748 |
] |
|
95 | 4749 |
] |
4750 |
||
4751 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4752 |
args: aCollection : <colleciton of <object> > |
8874 | 4753 |
newObject : <object> |
4754 |
startIndex : <integer> |
|
4755 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4756 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4757 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4758 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4759 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4760 |
" |
8874 | 4761 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
4762 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 4763 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4764 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4765 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 4766 |
! |
4767 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4768 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4769 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4770 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4771 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4772 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4773 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4774 |
|
8874 | 4775 |
^ self |
4776 |
replaceFrom:startIndex |
|
4777 |
to:(startIndex + numberOfElements - 1) |
|
4778 |
with:replacementCollection |
|
4779 |
startingAt:1 |
|
4780 |
||
4781 |
" |
|
4782 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4783 |
#($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
|
4784 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4785 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4786 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4787 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4788 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4789 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4790 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4791 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4792 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4793 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4794 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4795 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4796 |
^ self |
8874 | 4797 |
replaceFrom:startIndex |
4798 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
4799 |
with:replacementCollection |
|
4800 |
startingAt:repStartIndex |
|
4801 |
||
4802 |
" |
|
4803 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
4804 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
4805 |
#($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
|
4806 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4807 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4808 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4809 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
4810 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4811 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4812 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 4813 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4814 |
|
44 | 4815 |
Notice: This operation modifies the receiver, NOT a copy; |
4816 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4817 |
|
8874 | 4818 |
^ self |
4819 |
replaceFrom:startIndex |
|
4820 |
to:stopIndex |
|
4821 |
with:replacementCollection |
|
4822 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4823 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4824 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4825 |
args: startIndex : <integer> |
8874 | 4826 |
stopIndex : <integer> |
4827 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4828 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4829 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4830 |
" |
61 | 4831 |
|
95 | 4832 |
" |
4833 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
4834 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
4835 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4836 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4837 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 4838 |
! |
4839 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4840 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4841 |
"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
|
4842 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 4843 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4844 |
|
44 | 4845 |
Notice: This operation modifies the receiver, NOT a copy; |
4846 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4847 |
|
4848 |
|srcIndex "{ Class: SmallInteger }" |
|
4849 |
dstIndex "{ Class: SmallInteger }" |
|
4850 |
end "{ Class: SmallInteger }" | |
|
4851 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4852 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 4853 |
" must do reverse copy " |
4854 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
4855 |
dstIndex := stopIndex. |
|
4856 |
end := startIndex. |
|
4857 |
[dstIndex >= end] whileTrue:[ |
|
4858 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
4859 |
srcIndex := srcIndex - 1. |
|
4860 |
dstIndex := dstIndex - 1 |
|
4861 |
]. |
|
4862 |
^ self |
|
1 | 4863 |
]. |
4864 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4865 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4866 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4867 |
end := stopIndex. |
1 | 4868 |
[dstIndex <= end] whileTrue:[ |
8874 | 4869 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
4870 |
srcIndex := srcIndex + 1. |
|
4871 |
dstIndex := dstIndex + 1 |
|
1 | 4872 |
] |
61 | 4873 |
|
95 | 4874 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4875 |
args: startIndex : <integer> |
8874 | 4876 |
stopIndex : <integer> |
4877 |
replacementCollection : <colleciton of <object> > |
|
4878 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4879 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4880 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4881 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4882 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4883 |
" |
8874 | 4884 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
4885 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
4886 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 4887 |
|
4888 |
|c| |
|
4889 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4890 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 4891 |
|
4892 |
|c| |
|
4893 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 4894 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 4895 |
" |
3234 | 4896 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4897 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 4898 |
! |
4899 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4900 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4901 |
"replace elements in the receiver starting at startIndex, |
602 | 4902 |
with elements taken from replacementCollection starting at 1 |
4903 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4904 |
|
602 | 4905 |
Notice: This operation modifies the receiver, NOT a copy; |
4906 |
therefore the change may affect all others referencing the receiver." |
|
4907 |
||
8874 | 4908 |
^ self |
4909 |
replaceFrom:startIndex |
|
4910 |
to:(startIndex + replacementCollection size - 1) |
|
4911 |
with:replacementCollection |
|
4912 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4913 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4914 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4915 |
args: startIndex : <integer> |
8874 | 4916 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4917 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4918 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4919 |
" |
602 | 4920 |
|
4921 |
" |
|
4922 |
'1234567890' replaceFrom:5 with:'abc' |
|
4923 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
4924 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4925 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4926 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 4927 |
! |
4928 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4929 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4930 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4931 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 4932 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4933 |
|
602 | 4934 |
Notice: This operation modifies the receiver, NOT a copy; |
4935 |
therefore the change may affect all others referencing the receiver." |
|
4936 |
||
8874 | 4937 |
^ self |
4938 |
replaceFrom:startIndex |
|
4939 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
4940 |
with:replacementCollection |
|
4941 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4942 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4943 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4944 |
args: startIndex : <integer> |
8874 | 4945 |
replacementCollection : <colleciton of <object> > |
4946 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4947 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4948 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4949 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4950 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4951 |
" |
8874 | 4952 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 4953 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
4954 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4955 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4956 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 4957 |
! |
4958 |
||
61 | 4959 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
4960 |
"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
|
4961 |
|
61 | 4962 |
Notice: This operation modifies the destination collection, NOT a copy; |
4963 |
therefore the change may affect all others referencing this object." |
|
4964 |
||
8874 | 4965 |
destColl |
4966 |
replaceFrom:destStartIndex |
|
4967 |
to:destEndIndex |
|
4968 |
with:self |
|
4969 |
startingAt:sourceStart |
|
61 | 4970 |
|
4971 |
" |
|
4972 |
|s| |
|
4973 |
s := 'abcdefghijklmnop'. |
|
4974 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
4975 |
s'123defghijklmnop' |
|
4976 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4977 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4978 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 4979 |
! ! |
4980 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4981 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4982 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4983 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4984 |
"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
|
4985 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4986 |
Elements on the left are filled with padElement. |
8874 | 4987 |
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
|
4988 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4989 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4990 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4991 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4992 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4993 |
(len < size) ifTrue:[ |
8874 | 4994 |
s := self species new:size withAll:padElement. |
4995 |
s replaceFrom:(size - len + 1) with:self. |
|
4996 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4997 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4998 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
4999 |
" |
8874 | 5000 |
'foo' leftPaddedTo:10 with:$. |
5001 |
'fooBar' leftPaddedTo:5 with:$. |
|
5002 |
123 printString leftPaddedTo:10 with:$. |
|
5003 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
5004 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
5005 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
5006 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
5007 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
5008 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
5009 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5010 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5011 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5012 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5013 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5014 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5015 |
plus pad elements up to length. |
8874 | 5016 |
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
|
5017 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5018 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5019 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5020 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5021 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5022 |
len < newSize ifTrue:[ |
8874 | 5023 |
s := self species new:newSize withAll:padElement. |
5024 |
s replaceFrom:1 to:len with:self. |
|
5025 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5026 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5027 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5028 |
" |
8874 | 5029 |
'foo' paddedTo:10 with:$. |
5030 |
123 printString paddedTo:10 with:$* |
|
5031 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
5032 |
(Float pi printString) paddedTo:15 with:$- |
|
5033 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
5034 |
#[1 2 3 4] paddedTo:6 with:0 |
|
5035 |
#[1 2 3 4] paddedTo:10 with:99 |
|
5036 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5037 |
" |
7116 | 5038 |
! |
5039 |
||
5040 |
paddedToMultipleOf:sizeModulu with:padElement |
|
5041 |
|mySize mod numPad| |
|
5042 |
||
5043 |
mySize := self size. |
|
5044 |
mod := (mySize \\ sizeModulu). |
|
5045 |
mod == 0 ifTrue:[^ self]. |
|
5046 |
||
5047 |
numPad := sizeModulu - mod. |
|
5048 |
^ self , (self species new:numPad withAll:padElement). |
|
5049 |
||
5050 |
" |
|
8874 | 5051 |
#[] paddedToMultipleOf:3 with:0 |
5052 |
#[1] paddedToMultipleOf:3 with:0 |
|
5053 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
5054 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 5055 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
5056 |
||
5057 |
#() paddedToMultipleOf:3 with:nil |
|
5058 |
#(1) paddedToMultipleOf:3 with:nil |
|
5059 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
5060 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
5061 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
5062 |
||
5063 |
'' paddedToMultipleOf:3 with:Character space |
|
5064 |
'a' paddedToMultipleOf:3 with:Character space |
|
5065 |
'ab' paddedToMultipleOf:3 with:Character space |
|
5066 |
'abc' paddedToMultipleOf:3 with:Character space |
|
5067 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
5068 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5069 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5070 |
|
7260 | 5071 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 5072 |
|
5358 | 5073 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 5074 |
"Private!! |
5358 | 5075 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
5076 |
||
5077 |
| i1 i2 val1 val2 out | |
|
5078 |
||
5079 |
i1 := first. |
|
5080 |
i2 := middle + 1. |
|
5081 |
val1 := self at: i1. |
|
5082 |
val2 := self at: i2. |
|
5083 |
out := first - 1. "will be pre-incremented" |
|
5084 |
||
5085 |
"select 'lower' half of the elements based on comparator" |
|
5086 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 5087 |
(aBlock value: val1 value: val2) |
5088 |
ifTrue: [ |
|
5089 |
dst at: (out := out + 1) put: val1. |
|
5090 |
val1 := self at: (i1 := i1 + 1)] |
|
5091 |
ifFalse: [ |
|
5092 |
dst at: (out := out + 1) put: val2. |
|
5093 |
i2 := i2 + 1. |
|
5094 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 5095 |
|
5096 |
"copy the remaining elements" |
|
5097 |
i1 <= middle |
|
8874 | 5098 |
ifTrue: [ |
5099 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
5100 |
ifFalse: [ |
|
5101 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 5102 |
|
5103 |
! |
|
5104 |
||
5105 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
5106 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
5107 |
||
5108 |
| middle | |
|
5109 |
||
5110 |
first = last ifTrue: [^ self]. |
|
5111 |
middle := (first + last) // 2. |
|
5112 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
5113 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
5114 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
5115 |
||
5116 |
||
5117 |
! |
|
5118 |
||
2069 | 5119 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5120 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5121 |
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
|
5122 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5123 |
|
8874 | 5124 |
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
|
5125 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5126 |
|
5127 |
|begin "{ Class: SmallInteger }" |
|
5128 |
end "{ Class: SmallInteger }" |
|
5129 |
b "{ Class: SmallInteger }" |
|
5130 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5131 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5132 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5133 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5134 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5135 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5136 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5137 |
[stack notEmpty] whileTrue:[ |
8874 | 5138 |
end := stack removeLast. |
5139 |
begin := stack removeLast. |
|
5140 |
||
5141 |
b := begin. |
|
5142 |
e := end. |
|
5143 |
middleElement := self at:((b + e) // 2). |
|
5144 |
||
5145 |
[b < e] whileTrue:[ |
|
5146 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5147 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5148 |
||
5149 |
(b <= e) ifTrue:[ |
|
5150 |
(b == e) ifFalse:[ |
|
5151 |
temp1 := self at:b. temp2 := self at:e. |
|
5152 |
self at:b put:temp2. self at:e put:temp1 |
|
5153 |
]. |
|
5154 |
b := b + 1. |
|
5155 |
e := e - 1 |
|
5156 |
] |
|
5157 |
]. |
|
5158 |
(begin < e) ifTrue:[ |
|
5159 |
stack add:begin. |
|
5160 |
stack add:e. |
|
5161 |
]. |
|
5162 |
(b < end) ifTrue:[ |
|
5163 |
stack add:b. |
|
5164 |
stack add:end. |
|
5165 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5166 |
] |
2069 | 5167 |
! |
5168 |
||
5169 |
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
|
5170 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5171 |
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
|
5172 |
with degenerated collections." |
2069 | 5173 |
|
5174 |
|begin "{ Class: SmallInteger }" |
|
5175 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5176 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5177 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5178 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5179 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5180 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5181 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5182 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5183 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5184 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5185 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5186 |
[stack notEmpty] whileTrue:[ |
8874 | 5187 |
end := stack removeLast. |
5188 |
begin := stack removeLast. |
|
5189 |
||
5190 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5191 |
elB := self at:begin. |
|
5192 |
elE := self at:end. |
|
5193 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5194 |
temp := elB. elB := elE. elE := temp. |
|
5195 |
self at:begin put:elB. |
|
5196 |
self at:end put:elE. |
|
5197 |
]. |
|
5198 |
||
5199 |
nToSort > 2 ifTrue:[ |
|
5200 |
m := (begin + end) // 2. |
|
5201 |
elM := self at:m. |
|
5202 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5203 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5204 |
temp := self at:end. |
|
5205 |
self at:end put:(self at:m). |
|
5206 |
self at:m put:temp. |
|
5207 |
elM := elE |
|
5208 |
] |
|
5209 |
] ifFalse:[ |
|
5210 |
temp := self at:begin. |
|
5211 |
self at:begin put:(self at:m). |
|
5212 |
self at:m put:temp. |
|
5213 |
elM := elB |
|
5214 |
]. |
|
5215 |
nToSort > 3 ifTrue:[ |
|
5216 |
bRun := begin. |
|
5217 |
eRun := end. |
|
5218 |
[ |
|
5219 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5220 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5221 |
bRun <= eRun |
|
5222 |
] whileTrue:[ |
|
5223 |
temp := self at:bRun. |
|
5224 |
self at:bRun put:(self at:eRun). |
|
5225 |
self at:eRun put:temp. |
|
5226 |
]. |
|
5227 |
stack add:begin. |
|
5228 |
stack add:eRun. |
|
5229 |
||
5230 |
stack add:bRun. |
|
5231 |
stack add:end. |
|
5232 |
] |
|
5233 |
] |
|
5234 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5235 |
] |
2069 | 5236 |
! |
5237 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5238 |
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
|
5239 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5240 |
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
|
5241 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5242 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5243 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5244 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5245 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5246 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5247 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5248 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5249 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5250 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5251 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5252 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5253 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5254 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5255 |
[stack notEmpty] whileTrue:[ |
8874 | 5256 |
end := stack removeLast. |
5257 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5258 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5259 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 5260 |
elB := self at:begin. |
5261 |
elE := self at:end. |
|
5262 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
5263 |
temp := elB. elB := elE. elE := temp. |
|
5264 |
self at:begin put:elB. |
|
5265 |
self at:end put:elE. |
|
5266 |
]. |
|
5267 |
||
5268 |
nToSort > 2 ifTrue:[ |
|
5269 |
m := (begin + end) // 2. |
|
5270 |
elM := self at:m. |
|
5271 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
5272 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
5273 |
temp := self at:end. |
|
5274 |
self at:end put:(self at:m). |
|
5275 |
self at:m put:temp. |
|
5276 |
elM := elE |
|
5277 |
] |
|
5278 |
] ifFalse:[ |
|
5279 |
temp := self at:begin. |
|
5280 |
self at:begin put:(self at:m). |
|
5281 |
self at:m put:temp. |
|
5282 |
elM := elB |
|
5283 |
]. |
|
5284 |
nToSort > 3 ifTrue:[ |
|
5285 |
bRun := begin. |
|
5286 |
eRun := end. |
|
5287 |
[ |
|
5288 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
5289 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
5290 |
bRun <= eRun |
|
5291 |
] whileTrue:[ |
|
5292 |
temp := self at:bRun. |
|
5293 |
self at:bRun put:(self at:eRun). |
|
5294 |
self at:eRun put:temp. |
|
5295 |
]. |
|
5296 |
stack add:begin. |
|
5297 |
stack add:eRun. |
|
5298 |
||
5299 |
stack add:bRun. |
|
5300 |
stack add:end. |
|
5301 |
] |
|
5302 |
] |
|
5303 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5304 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5305 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5306 |
|
2069 | 5307 |
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
|
5308 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5309 |
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
|
5310 |
with degenerated collections." |
2069 | 5311 |
|
5312 |
|begin "{ Class: SmallInteger }" |
|
5313 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5314 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5315 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5316 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5317 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5318 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5319 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5320 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5321 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5322 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5323 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5324 |
[stack notEmpty] whileTrue:[ |
8874 | 5325 |
end := stack removeLast. |
5326 |
begin := stack removeLast. |
|
5327 |
||
5328 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5329 |
||
5330 |
elB := self at:begin. |
|
5331 |
elE := self at:end. |
|
5332 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5333 |
temp := elB. elB := elE. elE := temp. |
|
5334 |
self at:begin put:elB. |
|
5335 |
self at:end put:elE. |
|
5336 |
||
5337 |
temp := aCollection at:end. |
|
5338 |
aCollection at:end put:(aCollection at:begin). |
|
5339 |
aCollection at:begin put:temp. |
|
5340 |
]. |
|
5341 |
||
5342 |
nToSort > 2 ifTrue:[ |
|
5343 |
m := (begin + end) // 2. |
|
5344 |
elM := self at:m. |
|
5345 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5346 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5347 |
temp := self at:end. |
|
5348 |
self at:end put:(self at:m). |
|
5349 |
self at:m put:temp. |
|
5350 |
||
5351 |
temp := aCollection at:end. |
|
5352 |
aCollection at:end put:(aCollection at:m). |
|
5353 |
aCollection at:m put:temp. |
|
5354 |
||
5355 |
elM := elE |
|
5356 |
] |
|
5357 |
] ifFalse:[ |
|
5358 |
temp := self at:begin. |
|
5359 |
self at:begin put:(self at:m). |
|
5360 |
self at:m put:temp. |
|
5361 |
||
5362 |
temp := aCollection at:begin. |
|
5363 |
aCollection at:begin put:(aCollection at:m). |
|
5364 |
aCollection at:m put:temp. |
|
5365 |
elM := elB |
|
5366 |
]. |
|
5367 |
nToSort > 3 ifTrue:[ |
|
5368 |
bRun := begin. |
|
5369 |
eRun := end. |
|
5370 |
[ |
|
5371 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5372 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5373 |
bRun <= eRun |
|
5374 |
] whileTrue:[ |
|
5375 |
temp := self at:bRun. |
|
5376 |
self at:bRun put:(self at:eRun). |
|
5377 |
self at:eRun put:temp. |
|
5378 |
||
5379 |
temp := aCollection at:bRun. |
|
5380 |
aCollection at:bRun put:(aCollection at:eRun). |
|
5381 |
aCollection at:eRun put:temp. |
|
5382 |
]. |
|
5383 |
stack add:begin. |
|
5384 |
stack add:eRun. |
|
5385 |
||
5386 |
stack add:bRun. |
|
5387 |
stack add:end. |
|
5388 |
] |
|
5389 |
] |
|
5390 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5391 |
] |
2069 | 5392 |
! |
5393 |
||
5394 |
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
|
5395 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5396 |
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
|
5397 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5398 |
|
8874 | 5399 |
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
|
5400 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5401 |
|
5402 |
|begin "{ Class: SmallInteger }" |
|
5403 |
end "{ Class: SmallInteger }" |
|
5404 |
b "{ Class: SmallInteger }" |
|
5405 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5406 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5407 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5408 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5409 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5410 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5411 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5412 |
[stack notEmpty] whileTrue:[ |
8874 | 5413 |
end := stack removeLast. |
5414 |
begin := stack removeLast. |
|
5415 |
||
5416 |
b := begin. |
|
5417 |
e := end. |
|
5418 |
middleElement := self at:((b + e) // 2). |
|
5419 |
||
5420 |
[b < e] whileTrue:[ |
|
5421 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5422 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5423 |
||
5424 |
(b <= e) ifTrue:[ |
|
5425 |
(b == e) ifFalse:[ |
|
5426 |
temp1 := self at:b. temp2 := self at:e. |
|
5427 |
self at:b put:temp2. self at:e put:temp1. |
|
5428 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5429 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5430 |
]. |
|
5431 |
b := b + 1. |
|
5432 |
e := e - 1 |
|
5433 |
] |
|
5434 |
]. |
|
5435 |
(begin < e) ifTrue:[ |
|
5436 |
stack add:begin. |
|
5437 |
stack add:e. |
|
5438 |
]. |
|
5439 |
(b < end) ifTrue:[ |
|
5440 |
stack add:b. |
|
5441 |
stack add:end. |
|
5442 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5443 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5444 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5445 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5446 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5447 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5448 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5449 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5450 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5451 |
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
|
5452 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5453 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5454 |
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
|
5455 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5456 |
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
|
5457 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5458 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5459 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5460 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5461 |
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
|
5462 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5463 |
"/ 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
|
5464 |
^ 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
|
5465 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5466 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5467 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5468 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5469 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5470 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5471 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5472 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5473 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5474 |
"/ 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
|
5475 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5476 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5477 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5478 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5479 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5480 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5481 |
"/ "/ 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
|
5482 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5483 |
"/ 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
|
5484 |
"/ 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
|
5485 |
"/ 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
|
5486 |
"/ aCollection notNil ifTrue:[ |
8874 | 5487 |
"/ 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
|
5488 |
"/ 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
|
5489 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5490 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5491 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5492 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5493 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5494 |
"/ 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
|
5495 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5496 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5497 |
"/ [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
|
5498 |
"/ [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
|
5499 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5500 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5501 |
"/ (b == e) ifFalse:[ |
8874 | 5502 |
"/ 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
|
5503 |
"/ 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
|
5504 |
"/ aCollection notNil ifTrue:[ |
8874 | 5505 |
"/ 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
|
5506 |
"/ 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
|
5507 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5508 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5509 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5510 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5511 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5512 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5513 |
"/ (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
|
5514 |
"/ (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
|
5515 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5516 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5517 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5518 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5519 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5520 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5521 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5522 |
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
|
5523 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5524 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5525 |
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
|
5526 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5527 |
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
|
5528 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5529 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5530 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5531 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5532 |
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
|
5533 |
"/ 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
|
5534 |
^ 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
|
5535 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5536 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5537 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5538 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5539 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5540 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5541 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5542 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5543 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5544 |
"/ 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
|
5545 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5546 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5547 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5548 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5549 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5550 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5551 |
"/ "/ 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
|
5552 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5553 |
"/ 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
|
5554 |
"/ 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
|
5555 |
"/ 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
|
5556 |
"/ aCollection notNil ifTrue:[ |
8874 | 5557 |
"/ 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
|
5558 |
"/ 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
|
5559 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5560 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5561 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5562 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5563 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5564 |
"/ 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
|
5565 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5566 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5567 |
"/ [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
|
5568 |
"/ [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
|
5569 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5570 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5571 |
"/ (b == e) ifFalse:[ |
8874 | 5572 |
"/ 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
|
5573 |
"/ 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
|
5574 |
"/ aCollection notNil ifTrue:[ |
8874 | 5575 |
"/ 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
|
5576 |
"/ 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
|
5577 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5578 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5579 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5580 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5581 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5582 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5583 |
"/ (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
|
5584 |
"/ (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
|
5585 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5586 |
"/ "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
|
5587 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5588 |
! ! |
5589 |
||
602 | 5590 |
!SequenceableCollection methodsFor:'queries'! |
5591 |
||
5592 |
firstIndex |
|
5593 |
"return the first elements index" |
|
5594 |
||
5595 |
^ 1 |
|
5596 |
! |
|
5597 |
||
5598 |
isSequenceable |
|
5599 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5600 |
||
5601 |
^ true |
|
1 | 5602 |
! |
5603 |
||
602 | 5604 |
isSequenceableCollection |
5605 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5606 |
This method is a historic leftover and will be removed." |
|
5607 |
||
5872 | 5608 |
<resource:#obsolete> |
5609 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5610 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5611 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5612 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5613 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5614 |
! |
5615 |
||
5358 | 5616 |
isSorted |
5617 |
"return true. if my elements are sorted (already)" |
|
5618 |
||
5619 |
|lastEl el| |
|
5620 |
||
5621 |
lastEl := self first. |
|
5622 |
2 to:self size do:[:i | |
|
8874 | 5623 |
el := self at:i. |
5624 |
el >= lastEl ifFalse: [^ false]. |
|
5625 |
lastEl := el |
|
5358 | 5626 |
]. |
5627 |
^ true |
|
5628 |
||
5629 |
" |
|
8874 | 5630 |
#(1 2 3 5 10 100) isSorted |
5631 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5632 |
" |
5633 |
! |
|
5634 |
||
5635 |
isSortedBy:aBlock |
|
5636 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5637 |
||
5638 |
|lastEl el| |
|
5639 |
||
5640 |
lastEl := self first. |
|
5641 |
2 to:self size do:[:i | |
|
8874 | 5642 |
el := self at:i. |
5643 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5644 |
lastEl := el |
|
5358 | 5645 |
]. |
5646 |
^ true |
|
5647 |
||
5648 |
" |
|
8874 | 5649 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5650 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5651 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5652 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5653 |
" |
5654 |
||
5655 |
! |
|
5656 |
||
602 | 5657 |
keys |
5658 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5659 |
||
5660 |
|sz| |
|
5661 |
||
1 | 5662 |
sz := self size. |
602 | 5663 |
sz == 0 ifTrue:[ |
8874 | 5664 |
^ #() |
61 | 5665 |
]. |
602 | 5666 |
^ 1 to:sz |
1 | 5667 |
! |
5668 |
||
602 | 5669 |
lastIndex |
5670 |
"return the last elements index" |
|
5671 |
||
5672 |
^ self size |
|
61 | 5673 |
! |
5674 |
||
602 | 5675 |
size |
5676 |
"return the number of elements in the collection. |
|
5677 |
concrete implementations must define this" |
|
5678 |
||
5679 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5680 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5681 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5682 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5683 |
"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
|
5684 |
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
|
5685 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5686 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5687 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5688 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5689 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5690 |
! ! |
5691 |
||
5692 |
!SequenceableCollection methodsFor:'searching'! |
|
5693 |
||
3887 | 5694 |
detect:aBlock startingAt:startIndex |
5695 |
"find the first element, for which evaluation of the argument, aBlock |
|
5696 |
return true, starting the search with startIndex. |
|
5697 |
If none does so, report an error" |
|
5698 |
||
5699 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
5700 |
||
5701 |
" |
|
8874 | 5702 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
5703 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 5704 |
" |
5705 |
||
5706 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
5707 |
! |
|
5708 |
||
5709 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
5710 |
"find the first element, for which evaluation of the argument, aBlock |
|
5711 |
return true, starting the search with startIndex. |
|
5717 | 5712 |
If none does so, return the evaluation of exceptionBlock" |
5713 |
||
5714 |
|stop| |
|
3887 | 5715 |
|
5716 |
stop := self size. |
|
5717 | 5717 |
self from:startIndex to:stop do:[:el | |
8874 | 5718 |
(aBlock value:el) ifTrue:[ |
5719 |
^ el |
|
5720 |
]. |
|
3887 | 5721 |
]. |
5722 |
^ exceptionBlock value |
|
5723 |
||
5724 |
" |
|
8874 | 5725 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
5726 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 5727 |
" |
5728 |
||
5729 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
5730 |
! |
|
5731 |
||
11705 | 5732 |
findFirst:aBlock ifNone:exceptionalValue |
10453 | 5733 |
"find the index of the first element, for which evaluation of the argument, aBlock |
11705 | 5734 |
returns true; return its index or the value from exceptionalValue if none detected. |
3887 | 5735 |
This is much like #detect, however, here an INDEX is returned, |
5736 |
while #detect returns the element." |
|
602 | 5737 |
|
5738 |
|stop "{ Class: SmallInteger }" | |
|
5739 |
||
32 | 5740 |
stop := self size. |
602 | 5741 |
1 to:stop do:[:index | |
10453 | 5742 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 5743 |
]. |
11705 | 5744 |
^ exceptionalValue value |
5745 |
||
5746 |
" |
|
5747 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
5748 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
5749 |
" |
|
1 | 5750 |
! |
5751 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5752 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5753 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 5754 |
returns true; return its index or 0 if none detected. |
5755 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
5756 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5757 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5758 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5759 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5760 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5761 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5762 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5763 |
start to:stop do:[:index | |
8874 | 5764 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5765 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5766 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5767 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5768 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5769 |
#(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
|
5770 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5771 |
" |
3887 | 5772 |
|
5773 |
"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
|
5774 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5775 |
|
602 | 5776 |
findLast:aBlock |
5777 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 5778 |
returns true. |
5779 |
Return its index or 0 if none detected." |
|
602 | 5780 |
|
5781 |
|start "{ Class: SmallInteger }"| |
|
5782 |
||
5783 |
start := self size. |
|
5784 |
start to:1 by:-1 do:[:index | |
|
8874 | 5785 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 5786 |
]. |
5787 |
^ 0 |
|
5788 |
||
5789 |
" |
|
602 | 5790 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
5791 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 5792 |
" |
5793 |
! |
|
5794 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5795 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5796 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 5797 |
returns true. Start the search at startIndex. |
5798 |
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
|
5799 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5800 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5801 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5802 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5803 |
start to:1 by:-1 do:[:index | |
8874 | 5804 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5805 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5806 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5807 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5808 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5809 |
#(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
|
5810 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5811 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5812 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
5813 |
|
8193 | 5814 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
5815 |
"find the last element, for which evaluation of the argument, aBlock |
|
5816 |
returns true. Start the search at startIndex. |
|
5817 |
End the search at endIndex or when an element is found. |
|
5818 |
Return its index or 0 if none detected." |
|
5819 |
||
5820 |
|start "{ Class: SmallInteger }"| |
|
5821 |
||
5822 |
start := startIndex. |
|
5823 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 5824 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 5825 |
]. |
5826 |
^ 0 |
|
5827 |
||
5828 |
" |
|
5829 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
5830 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
5831 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
5832 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
5833 |
" |
|
5834 |
! |
|
5835 |
||
159 | 5836 |
indexOfAny:aCollection |
5837 |
"search the collection for an element in aCollection. |
|
5838 |
if found, return the index otherwise return 0. |
|
8874 | 5839 |
The comparison is done using = |
159 | 5840 |
(i.e. equality test - not identity test). |
5841 |
||
5842 |
Notice, that for big collections, the runtime of this search |
|
5843 |
grows proportional to size(receiver) * size(aCollection). |
|
5844 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5845 |
||
5846 |
^ self indexOfAny:aCollection startingAt:1 |
|
5847 |
||
5848 |
" |
|
5849 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
5850 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
5851 |
" |
|
5852 |
! |
|
5853 |
||
5854 |
indexOfAny:aCollection startingAt:start |
|
5855 |
"search the collection for an element in aCollection, |
|
5856 |
starting the search at index start; |
|
5857 |
if found, return the index otherwise return 0. |
|
8874 | 5858 |
The comparison is done using = |
159 | 5859 |
(i.e. equality test - not identity test). |
5860 |
||
5861 |
Notice, that for big collections, the runtime of this search |
|
5862 |
grows proportional to size(receiver) * size(aCollection). |
|
5863 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
5864 |
||
5865 |
||
5866 |
|startIndex "{ Class: SmallInteger }" |
|
5867 |
stop "{ Class: SmallInteger }" | |
|
5868 |
||
5869 |
startIndex := start. |
|
5870 |
stop := self size. |
|
5871 |
startIndex to:stop do:[:index | |
|
8874 | 5872 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 5873 |
]. |
5874 |
^ 0 |
|
5875 |
" |
|
8874 | 5876 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
5877 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 5878 |
" |
1 | 5879 |
! |
5880 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5881 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5882 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5883 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5884 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 5885 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5886 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5887 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5888 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5889 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5890 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5891 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5892 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5893 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5894 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5895 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5896 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5897 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5898 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5899 |
" |
8874 | 5900 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
5901 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
5902 |
#(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
|
5903 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5904 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
5905 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5906 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5907 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5908 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5909 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5910 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5911 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5912 |
#(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
|
5913 |
#(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
|
5914 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5915 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5916 |
|
8874 | 5917 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5918 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5919 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5920 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5921 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5922 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5923 |
#(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
|
5924 |
#(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
|
5925 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5926 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5927 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5928 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5929 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5930 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5931 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5932 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5933 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
5934 |
|
368 | 5935 |
indexOfSubCollection:aCollection |
9234 | 5936 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 5937 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5938 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 5939 |
|
5940 |
" |
|
8874 | 5941 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
5942 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
5943 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
5944 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
5945 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
5946 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
5947 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
5948 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
368 | 5949 |
" |
5950 |
! |
|
5951 |
||
602 | 5952 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
5953 |
"find a subcollection. If found, return the index; |
|
5954 |
if not found, return the result of evaluating exceptionBlock." |
|
5955 |
||
5956 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
5957 |
! |
|
5958 |
||
368 | 5959 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 5960 |
"find a subcollection starting at index. |
368 | 5961 |
If found, return the index; if not found, return 0." |
5962 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
5963 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 5964 |
! |
5965 |
||
5966 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
5967 |
"find a subcollection, starting at index. If found, return the index; |
|
5968 |
if not found, return the result of evaluating exceptionBlock. |
|
5969 |
This is a q&d hack - not very efficient" |
|
5970 |
||
8874 | 5971 |
|same first |
368 | 5972 |
mySize "{Class: SmallInteger }" |
5973 |
checkIndex "{Class: SmallInteger }" |
|
5974 |
cmpIndex "{Class: SmallInteger }" |
|
5975 |
sz "{Class: SmallInteger }"| |
|
5976 |
||
5977 |
mySize := self size. |
|
5978 |
sz := aCollection size. |
|
5979 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
5980 |
first := aCollection at:1. |
|
5981 |
||
8874 | 5982 |
checkIndex := startIndex - 1. |
368 | 5983 |
[true] whileTrue:[ |
8874 | 5984 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
5985 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
5986 |
||
5987 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
5988 |
^ 0 |
|
5989 |
]. |
|
5990 |
same := true. |
|
5991 |
cmpIndex := 1. |
|
5992 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
5993 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
5994 |
same := false |
|
5995 |
] ifTrue:[ |
|
5996 |
cmpIndex := cmpIndex + 1 |
|
5997 |
] |
|
5998 |
]. |
|
5999 |
same ifTrue:[^ checkIndex]. |
|
6000 |
] |
|
6001 |
||
6002 |
" |
|
6003 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
6004 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
6005 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
6006 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
6007 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
6008 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 6009 |
" |
602 | 6010 |
! |
6011 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6012 |
lastIndexOfSubCollection:aCollection |
8874 | 6013 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6014 |
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
|
6015 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6016 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6017 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6018 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6019 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6020 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6021 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6022 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6023 |
"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
|
6024 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6025 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6026 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6027 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6028 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6029 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6030 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6031 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6032 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6033 |
"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
|
6034 |
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
|
6035 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6036 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6037 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6038 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6039 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6040 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6041 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6042 |
"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
|
6043 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6044 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6045 |
|
8874 | 6046 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6047 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6048 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6049 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6050 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6051 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6052 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6053 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6054 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6055 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6056 |
|
8874 | 6057 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6058 |
checkIndex > mySize ifTrue:[ |
8874 | 6059 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6060 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6061 |
[true] whileTrue:[ |
8874 | 6062 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
6063 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6064 |
||
6065 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
6066 |
^ 0 |
|
6067 |
]. |
|
6068 |
same := true. |
|
6069 |
cmpIndex := 1. |
|
6070 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6071 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
6072 |
same := false |
|
6073 |
] ifTrue:[ |
|
6074 |
cmpIndex := cmpIndex + 1 |
|
6075 |
] |
|
6076 |
]. |
|
6077 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
6078 |
] |
|
6079 |
||
6080 |
" |
|
6081 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
6082 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
6083 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
6084 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
6085 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
6086 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
6087 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
6088 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
6089 |
#(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
|
6090 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6091 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6092 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6093 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6094 |
|
7264 | 6095 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6096 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6097 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6098 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6099 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6100 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6101 |
|
10194 | 6102 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6103 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6104 |
" |
8874 | 6105 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6106 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6107 |
returns: true - if found |
10194 | 6108 |
false - if not found |
8874 | 6109 |
" |
6110 |
||
6111 |
" |
|
6112 |
#(10 20 30 40 50 60 70) includes:99 |
|
6113 |
#(10 20 30 40 50 60 70) includes:40 |
|
6114 |
#(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
|
6115 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6116 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6117 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6118 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6119 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6120 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6121 |
"search the collection for anElement; |
602 | 6122 |
if found, return the index otherwise return 0. |
8874 | 6123 |
The comparison is done using = |
602 | 6124 |
(i.e. equality test - not identity test)." |
6125 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6126 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6127 |
|
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 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6130 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6131 |
returns: elementIndex - if found |
8874 | 6132 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6133 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6134 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6135 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6136 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6137 |
#(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
|
6138 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6139 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6140 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6143 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6144 |
"search the collection for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6145 |
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
|
6146 |
exceptionBlock. |
8874 | 6147 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6148 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6149 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6150 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6151 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6152 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6153 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6154 |
^ index |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6157 |
args: anElement : <object> |
8874 | 6158 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6159 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6160 |
returns: elementIndex - if found |
8874 | 6161 |
exceptionBlock value - if not found |
6162 |
" |
|
6163 |
||
6164 |
" |
|
6165 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
6166 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
6167 |
#(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
|
6168 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6169 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6170 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6171 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6172 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6173 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 6174 |
"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
|
6175 |
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
|
6176 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6177 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6178 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6179 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6180 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6181 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6182 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 6183 |
self at:idx put:replacement. |
6184 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6185 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6186 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6187 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6188 |
" |
8874 | 6189 |
args: elementToFind : <object> |
6190 |
replacement : <integer> |
|
6191 |
start : <integer> |
|
6192 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6193 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6194 |
returns: elementIndex - if found (and replaced) |
8874 | 6195 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6196 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6197 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6198 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6199 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6200 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6201 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6202 |
(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
|
6203 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6204 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6205 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6206 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6207 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6208 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6209 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6210 |
"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
|
6211 |
if found, return the index otherwise return 0. |
8874 | 6212 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6213 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6214 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6215 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6216 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6217 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6218 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6219 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6220 |
startIndex to:stop do:[:index | |
8874 | 6221 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6222 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6223 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6224 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6225 |
" |
8874 | 6226 |
args: anElement : <object> |
6227 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6228 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6229 |
returns: elementIndex - if found |
8874 | 6230 |
0 - if not found |
6231 |
" |
|
6232 |
||
6233 |
" |
|
6234 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
6235 |
#(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
|
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 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6239 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6240 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6241 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6242 |
"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
|
6243 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6244 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6245 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6246 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6247 |
|
602 | 6248 |
|startIndex "{ Class: SmallInteger }" |
6249 |
stopIndex "{ Class: SmallInteger }" | |
|
6250 |
||
6251 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6252 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6253 |
startIndex to:stopIndex do:[:index | |
8874 | 6254 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6257 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6258 |
" |
8874 | 6259 |
args: anElement : <object> |
6260 |
start : <integer> |
|
6261 |
stop : <integer> |
|
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 |
returns: elementIndex - if found |
8874 | 6264 |
exceptionBlock value - if not found |
6265 |
" |
|
6266 |
||
6267 |
" |
|
6268 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
6269 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
6270 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
6271 |
#(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
|
6272 |
" |
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 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6275 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6276 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6277 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6278 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6279 |
"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
|
6280 |
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
|
6281 |
exceptionBlock. |
8874 | 6282 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6283 |
(i.e. equality test - not identity test)." |
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 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6286 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6287 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6288 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6289 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6290 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6291 |
" |
8874 | 6292 |
args: anElement : <object> |
6293 |
start : <integer> |
|
6294 |
exceptionBlock : <valueImplementor> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6297 |
exceptionBlock value - if not found |
6298 |
" |
|
6299 |
||
6300 |
" |
|
6301 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
6302 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
6303 |
#(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
|
6304 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6305 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6306 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6307 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6308 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6309 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6310 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6311 |
if found, return the index otherwise return 0. |
8874 | 6312 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6313 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6314 |
|
8874 | 6315 |
^ self lastIndexOf:anElement startingAt:self size |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6318 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6319 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6320 |
returns: elementIndex - if found |
8874 | 6321 |
0 - if not found |
6322 |
" |
|
6323 |
||
6324 |
" |
|
6325 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
6326 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
6327 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
6328 |
#(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
|
6329 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6330 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6331 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6332 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6333 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6334 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6335 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6336 |
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
|
6337 |
exceptionBlock. |
8874 | 6338 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6339 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6340 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6341 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6342 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6343 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6344 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6345 |
^ index |
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 |
" |
8874 | 6348 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
6349 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
6350 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
6351 |
#(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
|
6352 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6353 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6354 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6355 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6356 |
"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
|
6357 |
if found, return the index otherwise return 0. |
8874 | 6358 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6359 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6360 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6361 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6362 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6363 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6364 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6365 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 6366 |
]. |
6367 |
^ 0 |
|
6368 |
||
6369 |
" |
|
8874 | 6370 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
6371 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
6372 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
6373 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
6374 |
#(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
|
6375 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6376 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6377 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6378 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6379 |
"search the collection backwards for anElement starting the search at |
6380 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6381 |
otherwise return the value of the exceptionBlock. |
8874 | 6382 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6383 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6384 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6385 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6386 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6387 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6388 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6389 |
^ index |
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 |
" |
8874 | 6392 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
6393 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6394 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6395 |
#(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
|
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 |
|
4788 | 6399 |
lastIndexOfAny:aCollection |
6400 |
"search the collection backwards for any in aCollection; |
|
6401 |
if found, return the index, otherwise return 0. |
|
8874 | 6402 |
The comparison is done using = |
4788 | 6403 |
(i.e. equality test - not identity test)." |
6404 |
||
8874 | 6405 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 6406 |
|
6407 |
" |
|
6408 |
args: aCollection : <collection> |
|
6409 |
||
6410 |
returns: elementIndex - if found |
|
8874 | 6411 |
0 - if not found |
6412 |
" |
|
6413 |
||
6414 |
" |
|
6415 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 6416 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 6417 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
6418 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 6419 |
" |
6420 |
||
6421 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
6422 |
! |
|
6423 |
||
6424 |
lastIndexOfAny:aCollection startingAt:start |
|
6425 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
6426 |
if found, return the index, otherwise return 0. |
|
8874 | 6427 |
The comparison is done using = |
4788 | 6428 |
(i.e. equality test - not identity test)." |
6429 |
||
6430 |
|startIndex "{ Class: SmallInteger }"| |
|
6431 |
||
6432 |
startIndex := start. |
|
6433 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 6434 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 6435 |
]. |
6436 |
^ 0 |
|
6437 |
||
6438 |
" |
|
8874 | 6439 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
6440 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 6441 |
" |
6442 |
! |
|
6443 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6444 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6445 |
"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
|
6446 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6447 |
if found, return the index otherwise return nil. |
8874 | 6448 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6449 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6450 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6451 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6452 |
although this is somewhat strange (the indexOf-search |
6453 |
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
|
6454 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6455 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6456 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6457 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6458 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6459 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6460 |
startIndex to:stop do:[:index | |
8874 | 6461 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6462 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6463 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6464 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6465 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6466 |
args: anElement : <object> |
8874 | 6467 |
start : <integer> |
6468 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6469 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6470 |
returns: elementIndex - if found |
8874 | 6471 |
nil - if not found |
6472 |
" |
|
6473 |
||
6474 |
" |
|
6475 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
6476 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
6477 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 6478 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6479 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6480 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 6481 |
! |
6482 |
||
6483 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6484 |
"search the collection for anElement, starting the search at index start |
|
6485 |
and stopping at stop; |
|
6486 |
if found, return the index otherwise return the value of the |
|
6487 |
exceptionBlock. |
|
8874 | 6488 |
The comparison is done using = |
602 | 6489 |
(i.e. equality test - not identity test)." |
6490 |
||
6491 |
|index| |
|
6492 |
||
6493 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6494 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 6495 |
^ index |
6496 |
||
6497 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6498 |
args: anElement : <object> |
8874 | 6499 |
start : <integer> |
6500 |
stop : <integer> |
|
6501 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6502 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6503 |
returns: elementIndex - if found |
8874 | 6504 |
valueImplementor value - if not found |
6505 |
" |
|
6506 |
||
6507 |
" |
|
6508 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6509 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6510 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6511 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6512 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6513 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 6514 |
! |
6515 |
||
6516 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6517 |
"search the collection for anElement, starting the search at index start; |
|
6518 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6519 |
If found (within the range), return the index, otherwise return nil. |
8874 | 6520 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6521 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6522 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6523 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6524 |
although this is somewhat strange (the indexOf-search |
6525 |
methods return 0), this is done for VW compatibility." |
|
2275 | 6526 |
|
6527 |
|startIndex "{ Class: SmallInteger }" |
|
6528 |
stopIndex "{ Class: SmallInteger }" | |
|
6529 |
||
6530 |
startIndex := startSearchIndex. |
|
6531 |
stopIndex := endSearchIndex max:1. |
|
6532 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 6533 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6534 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6535 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6536 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6537 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6538 |
args: anElement : <object> |
8874 | 6539 |
start : <integer> |
6540 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6541 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6542 |
returns: elementIndex - if found |
8874 | 6543 |
nil - if not found |
6544 |
" |
|
6545 |
||
6546 |
" |
|
6547 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6548 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6549 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6550 |
" |
6551 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6552 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6553 |
! |
6554 |
||
6555 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6556 |
"search the collection for anElement, starting the search at index start |
|
6557 |
and stopping at stop, doing a reverse search; |
|
6558 |
if found, return the index otherwise return the value of the |
|
6559 |
exceptionBlock. |
|
8874 | 6560 |
The comparison is done using = |
2275 | 6561 |
(i.e. equality test - not identity test)." |
6562 |
||
6563 |
|index| |
|
6564 |
||
6565 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6566 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6567 |
^ index |
|
6568 |
||
6569 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6570 |
args: anElement : <object> |
8874 | 6571 |
start : <integer> |
6572 |
stop : <integer> |
|
6573 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6574 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6575 |
returns: elementIndex - if found |
8874 | 6576 |
valueImplementor value - if not found |
6577 |
" |
|
6578 |
||
6579 |
" |
|
6580 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6581 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6582 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6583 |
" |
6584 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6585 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6586 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6587 |
|
7264 | 6588 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6589 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6590 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6591 |
"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
|
6592 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6593 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6594 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6595 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6596 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6597 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6598 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6599 |
returns: elementIndex - if found |
8874 | 6600 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6601 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6602 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6603 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6604 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6605 |
#(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
|
6606 |
#(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
|
6607 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6608 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6609 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6610 |
#(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
|
6611 |
#(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
|
6612 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6613 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6614 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6615 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6616 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6617 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6618 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6619 |
"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
|
6620 |
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
|
6621 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6622 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6623 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6624 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6625 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6626 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6627 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6628 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6629 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6630 |
args: anElement : <object> |
8874 | 6631 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6632 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6633 |
returns: elementIndex - if found |
8874 | 6634 |
exceptionBlock value - if not found |
6635 |
" |
|
6636 |
||
6637 |
" |
|
6638 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6639 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6640 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6641 |
#(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
|
6642 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6643 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6644 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6645 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6646 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6647 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6648 |
"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
|
6649 |
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
|
6650 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6651 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6652 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6653 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6654 |
args: anElement : <object> |
8874 | 6655 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6656 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6657 |
returns: elementIndex - if found |
8874 | 6658 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6659 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6660 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6661 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6662 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6663 |
"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
|
6664 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6665 |
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
|
6666 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6667 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6668 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6669 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6670 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6671 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6672 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6673 |
startIndex to:stop do:[:index | |
8874 | 6674 |
el := self at:index. |
6675 |
anElement == el ifTrue:[^ index]. |
|
6676 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6677 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6678 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6679 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6680 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6681 |
args: anElement : <object> |
8874 | 6682 |
anotherElement : <object> |
6683 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6684 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6685 |
returns: elementIndex - if found |
8874 | 6686 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6687 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6688 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6689 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6690 |
#(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
|
6691 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6692 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6693 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6694 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6695 |
"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
|
6696 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6697 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6698 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6699 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6700 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6701 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6702 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6703 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6704 |
startIndex to:stop do:[:index | |
8874 | 6705 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6706 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6707 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6708 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6709 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6710 |
args: anElement : <object> |
8874 | 6711 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6712 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6713 |
returns: elementIndex - if found |
8874 | 6714 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6715 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6716 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6717 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6718 |
#(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
|
6719 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 6720 |
#(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
|
6721 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6722 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6723 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6724 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6725 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6726 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6727 |
"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
|
6728 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6729 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6730 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6731 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6732 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6733 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6734 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6735 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6736 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6737 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6738 |
startIndex to:stopIndex do:[:index | |
8874 | 6739 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6740 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6741 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6742 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6743 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6744 |
args: anElement : <object> |
8874 | 6745 |
start : <integer> |
6746 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6747 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6748 |
returns: elementIndex - if found |
8874 | 6749 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6750 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6751 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6752 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6753 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6754 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6755 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6756 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6757 |
"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
|
6758 |
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
|
6759 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6760 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6761 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6762 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6763 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6764 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6765 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6766 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6767 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6768 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6769 |
args: anElement : <object> |
8874 | 6770 |
start : <integer> |
6771 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6772 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6773 |
returns: elementIndex - if found |
8874 | 6774 |
exceptionBlock value - if not found |
6775 |
" |
|
6776 |
||
6777 |
" |
|
6778 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
6779 |
#(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
|
6780 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6781 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6782 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6783 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6784 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6785 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6786 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6787 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6788 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6789 |
|
10194 | 6790 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6791 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6792 |
" |
8874 | 6793 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6794 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6795 |
returns: true - if found |
10194 | 6796 |
false - if not found |
8874 | 6797 |
" |
6798 |
||
6799 |
" |
|
6800 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
6801 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
6802 |
#(10 20 30 40 50 60 70) includes:40 |
|
6803 |
#(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
|
6804 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6805 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6806 |
|
8874 | 6807 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
6808 |
#(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
|
6809 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6810 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6811 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6812 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6813 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6814 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6815 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6816 |
if found, return the index otherwise return 0. |
8874 | 6817 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6818 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6819 |
|
8874 | 6820 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6821 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6822 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6823 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6824 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6825 |
returns: elementIndex - if found |
8874 | 6826 |
0 - if not found |
6827 |
" |
|
6828 |
||
6829 |
" |
|
6830 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
6831 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
6832 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
6833 |
#(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
|
6834 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6835 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6836 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6837 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6838 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6839 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6840 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6841 |
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
|
6842 |
exceptionBlock. |
8874 | 6843 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6844 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6845 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6846 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6847 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6848 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6849 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6850 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6851 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6852 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6853 |
args: anElement : <object> |
8874 | 6854 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6855 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6856 |
returns: elementIndex - if found |
8874 | 6857 |
exceptionBlock value - if not found |
6858 |
" |
|
6859 |
||
6860 |
" |
|
6861 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
6862 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
6863 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
6864 |
#(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
|
6865 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6866 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6867 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6868 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6869 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6870 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6871 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6872 |
"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
|
6873 |
if found, return the index otherwise return 0. |
8874 | 6874 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6875 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6876 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6877 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6878 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6879 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6880 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6881 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6882 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6883 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6884 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6885 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6886 |
args: anElement : <object> |
8874 | 6887 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6888 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6889 |
returns: elementIndex - if found |
8874 | 6890 |
0 - if not found |
6891 |
" |
|
6892 |
||
6893 |
" |
|
6894 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
6895 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
6896 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
6897 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
6898 |
#(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
|
6899 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6900 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6901 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6902 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6903 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6904 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6905 |
"search the collection backwards for anElement starting the search at |
6906 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6907 |
otherwise return the value of the exceptionBlock. |
8874 | 6908 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6909 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6910 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6911 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6912 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6913 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6914 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6915 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6916 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6917 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6918 |
args: anElement : <object> |
8874 | 6919 |
start : <integer> |
6920 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6921 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6922 |
returns: elementIndex - if found |
8874 | 6923 |
exceptionBlock value - if not found |
6924 |
" |
|
6925 |
||
6926 |
" |
|
6927 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
6928 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6929 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6930 |
#(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
|
6931 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6932 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6933 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6934 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 6935 |
! ! |
6936 |
||
6937 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
6938 |
||
6958 | 6939 |
detectFirstInOrder:sortBlock |
6940 |
"find the first element of the collection sorted with sortBlock" |
|
6941 |
||
6942 |
|selectedForNow start| |
|
6943 |
||
6944 |
start := true. |
|
6945 |
self do:[:element| |
|
8874 | 6946 |
start ifTrue:[ |
6947 |
start := false. |
|
6948 |
selectedForNow := element. |
|
6949 |
] ifFalse:[ |
|
6950 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
6951 |
selectedForNow := element. |
|
6952 |
]. |
|
6953 |
]. |
|
6958 | 6954 |
]. |
6955 |
||
6956 |
^ selectedForNow |
|
6957 |
||
6958 |
" |
|
8874 | 6959 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
6960 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
6961 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
6962 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 6963 |
" |
6964 |
! |
|
6965 |
||
5358 | 6966 |
mergeSort |
8874 | 6967 |
"sort the collection using a mergeSort algorithm. |
6968 |
The elements are compared using'<' |
|
5358 | 6969 |
i.e. they should offer a magnitude-like protocol. |
6970 |
||
6971 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 6972 |
the best possible for all situations |
5358 | 6973 |
See also #quickSort and #randomizedSort for other sort algorithms |
6974 |
with different worst- and average case behavior)" |
|
6975 |
||
6976 |
|stop| |
|
6977 |
||
6978 |
stop := self size. |
|
6979 |
(stop > 1) ifTrue:[ |
|
8874 | 6980 |
self mergeSort:[:a :b | a < b] from:1 to:stop |
5358 | 6981 |
] |
6982 |
||
6983 |
" |
|
6984 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
6985 |
||
6986 |
|data| |
|
6987 |
data := Random new next:100000. |
|
6988 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
6989 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 6990 |
data reverse. |
5358 | 6991 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
6992 |
||
6993 |
data := Random new next:100000. |
|
6994 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
6995 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 6996 |
data reverse. |
5358 | 6997 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6998 |
||
6999 |
data := Random new next:100000. |
|
7000 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
7001 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 7002 |
data reverse. |
5358 | 7003 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
7004 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7005 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7006 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7007 |
mergeSort:sortBlock |
8874 | 7008 |
"sort the collection using a mergeSort algorithm. |
7009 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7010 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7011 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7012 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 7013 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7014 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7015 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7016 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7017 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7018 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7019 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7020 |
(stop > 1) ifTrue:[ |
8874 | 7021 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7022 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7023 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7024 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7025 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7026 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7027 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7028 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7029 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7030 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7031 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7032 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7033 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7034 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7035 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7036 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 7037 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7038 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7039 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7040 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7041 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7042 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 7043 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7044 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7045 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7046 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7047 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7048 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 7049 |
"Sort the given range of indices using the mergesort algorithm. |
7050 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 7051 |
as many comparisons as heapsort or quicksort." |
7052 |
||
8874 | 7053 |
"Details: recursively split the range to be sorted into two halves, |
7054 |
mergesort each half, then merge the two halves together. |
|
7055 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
7056 |
and forth between the receiver and this copy. |
|
7057 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 7058 |
resulting in the receiver being completely sorted." |
7059 |
||
7060 |
| temp | |
|
7061 |
||
7062 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
7063 |
startIndex = stopIndex ifTrue: [^ self]. |
|
7064 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 7065 |
ifFalse: [self error: 'bad start index']. |
5358 | 7066 |
stopIndex <= self size |
8874 | 7067 |
ifFalse: [self error: 'bad stop index']. |
5358 | 7068 |
temp := self clone. |
7069 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
7070 |
||
7071 |
" |
|
8874 | 7072 |
#(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
|
7073 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7074 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7075 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7076 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7077 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7078 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7079 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7080 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7081 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7082 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7083 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7084 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7085 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7086 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7087 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 7088 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7089 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7090 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7091 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7092 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7093 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7094 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7095 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7096 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7097 |
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
|
7098 |
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
|
7099 |
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
|
7100 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7101 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7102 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7103 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7104 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7105 |
(stop > 1) ifTrue:[ |
8874 | 7106 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7107 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7108 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7109 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7110 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 7111 |
|
7112 |
|data| |
|
7113 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7114 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7115 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 7116 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7117 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7118 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7119 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7120 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7121 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7122 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7123 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7124 |
"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
|
7125 |
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
|
7126 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7127 |
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
|
7128 |
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
|
7129 |
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
|
7130 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7131 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7132 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7133 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7134 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7135 |
(stop > 1) ifTrue:[ |
8874 | 7136 |
sortBlock numArgs == 3 ifTrue:[ |
7137 |
"/ TODO: pass a collating policy to aBlock |
|
7138 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
7139 |
] ifFalse:[ |
|
7140 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
7141 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7142 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7143 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7144 |
" |
8874 | 7145 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7146 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7147 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7148 |
#(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
|
7149 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7150 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7151 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7152 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7153 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7154 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7155 |
"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
|
7156 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7157 |
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
|
7158 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7159 |
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
|
7160 |
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
|
7161 |
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
|
7162 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7163 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7164 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7165 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7166 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7167 |
(stop > 1) ifTrue:[ |
8874 | 7168 |
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
|
7169 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7170 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7171 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7172 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7173 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7174 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7175 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7176 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7177 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7178 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7179 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7180 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7181 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7182 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7183 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7184 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7185 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7186 |
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
|
7187 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7188 |
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
|
7189 |
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
|
7190 |
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
|
7191 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7192 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7193 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7194 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7195 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7196 |
(stop > 1) ifTrue:[ |
8874 | 7197 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7198 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7199 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7200 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 7201 |
! |
7202 |
||
11272 | 7203 |
randomShuffle |
7204 |
"random shuffle my elements in place. |
|
7205 |
This is a destructive algorithm. |
|
7206 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
7207 |
See 'The Art of Computer Programming'." |
|
7208 |
||
7209 |
self size to:2 by:-1 do:[:endIndex | |
|
7210 |
|rndIndex t| |
|
7211 |
||
7212 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
7213 |
"/ exchange |
|
7214 |
t := self at:rndIndex. |
|
7215 |
self at:rndIndex put:(self at:endIndex). |
|
7216 |
self at:endIndex put:t. |
|
7217 |
]. |
|
7218 |
||
7219 |
" |
|
7220 |
(1 to:10) asOrderedCollection randomShuffle |
|
7221 |
||
7222 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7223 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7224 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7225 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7226 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7227 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7228 |
" |
|
7229 |
||
11273 | 7230 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
7231 |
||
7232 |
|a score| |
|
7233 |
||
7234 |
a := 'abcd'. |
|
7235 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
7236 |
||
7237 |
1000000 times do:[ |
|
7238 |
|sorted| |
|
7239 |
sorted := a copy randomShuffle. |
|
7240 |
score at:sorted put:(score at:sorted)+1. |
|
7241 |
]. |
|
7242 |
||
7243 |
score keys asSortedCollection do:[:k | |
|
7244 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
7245 |
]. |
|
7246 |
" |
|
7247 |
||
7248 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
11272 | 7249 |
! |
7250 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7251 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7252 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7253 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7254 |
|
8874 | 7255 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7256 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7257 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7258 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7259 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7260 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7261 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7262 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7263 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7264 |
"/ 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
|
7265 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7266 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7267 |
"/ 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
|
7268 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7269 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7270 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7271 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7272 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7273 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7274 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7275 |
|
8874 | 7276 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7277 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7278 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7279 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7280 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7281 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7282 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7283 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7284 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7285 |
"/ 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
|
7286 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7287 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7288 |
"/ 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
|
7289 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7290 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7291 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7292 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7293 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7294 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7295 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7296 |
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
|
7297 |
|
8874 | 7298 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7299 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7300 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7301 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7302 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7303 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7304 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7305 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7306 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7307 |
"/ 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
|
7308 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7309 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7310 |
"/ 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
|
7311 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7312 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7313 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7314 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7315 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7316 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7317 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7318 |
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
|
7319 |
|
8874 | 7320 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7321 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7322 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7323 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7324 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7325 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7326 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7327 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7328 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7329 |
"/ 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
|
7330 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7331 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7332 |
"/ 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
|
7333 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7334 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7335 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7336 |
|
602 | 7337 |
reverse |
7335 | 7338 |
"destructively reverse the order of the elements inplace. |
11264 | 7339 |
WARNING: this is a destructive operation, which modifies the receiver. |
7340 |
Please use reversed (with a d) for a functional version." |
|
602 | 7341 |
|
7342 |
|lowIndex "{ Class: SmallInteger }" |
|
7343 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7344 |
t1 t2| |
602 | 7345 |
|
7346 |
hiIndex := self size. |
|
7347 |
lowIndex := 1. |
|
7348 |
[lowIndex < hiIndex] whileTrue:[ |
|
11264 | 7349 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
7350 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
7351 |
||
7352 |
lowIndex := lowIndex + 1. |
|
7353 |
hiIndex := hiIndex - 1 |
|
602 | 7354 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7355 |
|
69 | 7356 |
" |
602 | 7357 |
#(4 5 6 7 7) reverse |
7358 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 7359 |
" |
1 | 7360 |
! |
7361 |
||
5004 | 7362 |
reversed |
7363 |
"return a copy with elements in reverse order" |
|
7364 |
||
7365 |
^ self copy reverse |
|
7366 |
||
7367 |
" |
|
8874 | 7368 |
#(4 5 6 7 7) reversed |
5004 | 7369 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
7370 |
" |
|
7371 |
! |
|
7372 |
||
1 | 7373 |
sort |
7374 |
"sort the collection inplace. The elements are compared using |
|
2069 | 7375 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7376 |
|
159 | 7377 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7378 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7379 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7380 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 7381 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7382 |
self quickSort |
1 | 7383 |
|
95 | 7384 |
" |
7385 |
#(1 16 7 98 3 19 4 0) sort |
|
7386 |
||
7387 |
|data| |
|
7388 |
data := Random new next:100000. |
|
7389 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7390 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7391 |
data reverse. |
95 | 7392 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7393 |
" |
|
2069 | 7394 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7395 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 7396 |
! |
7397 |
||
7398 |
sort:sortBlock |
|
7399 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7400 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7401 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7402 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7403 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7404 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7405 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7406 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7407 |
self quickSort:sortBlock |
1 | 7408 |
|
95 | 7409 |
" |
8874 | 7410 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7411 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7412 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7413 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 7414 |
" |
2069 | 7415 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7416 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 7417 |
! |
7418 |
||
7419 |
sort:sortBlock with:aCollection |
|
7420 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 7421 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7422 |
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
|
7423 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7424 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7425 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7426 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7427 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7428 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7429 |
self quickSort:sortBlock with:aCollection |
1 | 7430 |
|
69 | 7431 |
" |
7432 |
|c1 c2| |
|
1 | 7433 |
c1 := #(1 16 7 9). |
7434 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
7435 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 7436 |
c1 printNL. |
7437 |
c2 printNL |
|
69 | 7438 |
" |
2069 | 7439 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7440 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 7441 |
! |
7442 |
||
11154 | 7443 |
sortBySelector:aSelector |
7444 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
7445 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
7446 |
||
7447 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
7448 |
||
7449 |
" |
|
7450 |
replace all uses of sort as in: |
|
7451 |
... sort:[:a :b | a foo < b foo] |
|
7452 |
by: |
|
7453 |
... sortBySelector:#foo |
|
7454 |
||
7455 |
find these by searching for code matching (code-search in the browsers method list): |
|
7456 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
7457 |
" |
|
7458 |
! |
|
7459 |
||
602 | 7460 |
sortWith:aCollection |
2069 | 7461 |
"sort the receiver collection inplace, using '<' to compare elements. |
7462 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7463 |
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
|
7464 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7465 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7466 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7467 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7468 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 7469 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7470 |
self quickSortWith:aCollection |
424 | 7471 |
! |
7472 |
||
11279 | 7473 |
sortedBy:aBlock |
9072 | 7474 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
7475 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
7476 |
||
7477 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
11279 | 7478 |
|
7479 |
"Modified: / 22-10-2008 / 21:25:07 / cg" |
|
9072 | 7480 |
! |
7481 |
||
602 | 7482 |
topologicalSort:sortBlock |
7483 |
"sort the collection inplace using a sloooow sort algorithm. |
|
7484 |
This algorithm has O-square runtime behavior and should be used only |
|
7485 |
in special situations. |
|
7486 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
7487 |
a>c (for example, to sort classes by inheritance) |
|
7488 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7489 |
In all other situations, use #sort; which implements the quicksort algorithm. |
10812 | 7490 |
|
7491 |
If there are cycles, this algorithm may loop endless!! |
|
7492 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
77 | 7493 |
" |
602 | 7494 |
|
7495 |
|smallestIndex "{ Class: SmallInteger }" |
|
7496 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7497 |
index2 "{ Class: SmallInteger }" |
602 | 7498 |
smallest thisOne| |
7499 |
||
7500 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
7501 |
||
7502 |
end := self size. |
|
862 | 7503 |
1 to:end-1 do:[:index | |
10812 | 7504 |
smallest := self at:index. |
7505 |
smallestIndex := index. |
|
7506 |
||
7507 |
index2 := index + 1. |
|
7508 |
[ |
|
7509 |
thisOne := self at:index2. |
|
7510 |
(index2 ~~ smallestIndex |
|
7511 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
7512 |
smallestIndex := index2. |
|
7513 |
smallest := thisOne. |
|
7514 |
index2 := index + 1. |
|
7515 |
] ifFalse:[ |
|
7516 |
index2 := index2 + 1. |
|
7517 |
]. |
|
7518 |
] doWhile:[index2 <= end]. |
|
862 | 7519 |
|
7520 |
"/ (index + 1) to:end do:[:index2 | |
|
7521 |
"/ thisOne := self at:index2. |
|
7522 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
7523 |
"/ smallestIndex := index2. |
|
7524 |
"/ smallest := thisOne |
|
7525 |
"/ ]. |
|
7526 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7527 |
|
10812 | 7528 |
(smallestIndex ~~ index) ifTrue:[ |
7529 |
thisOne := self at:index. |
|
7530 |
self at:index put:smallest. |
|
7531 |
self at:smallestIndex put:thisOne |
|
7532 |
]. |
|
1 | 7533 |
] |
95 | 7534 |
|
77 | 7535 |
" |
8874 | 7536 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
7537 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
10812 | 7538 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
8874 | 7539 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
7540 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 7541 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7542 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7543 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 7544 |
! ! |
602 | 7545 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7546 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7547 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7548 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7549 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7550 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 7551 |
however, having a common protocol with indexed collections |
7552 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7553 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7554 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7555 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7556 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7557 |
" |
8874 | 7558 |
#(1 2 3) includesKey:4 |
7559 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7560 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7561 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7562 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7563 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7564 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7565 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7566 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7567 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7568 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7569 |
|
2069 | 7570 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7571 |
|
7572 |
version |
|
11870 | 7573 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.274 2009-08-23 09:21:08 cg Exp $' |
628 | 7574 |
! ! |
6820 | 7575 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7576 |
SequenceableCollection initialize! |