author | Claus Gittinger <cg@exept.de> |
Fri, 23 Sep 2011 14:03:55 +0200 | |
changeset 13719 | 33c58d279282 |
parent 13697 | 6e9de7dc94bc |
child 13792 | 5d34db2e46bf |
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 |
||
5188 | 204 |
!SequenceableCollection class methodsFor:'Signal constants'! |
205 |
||
206 |
missingClassInLiteralArrayErrorSignal |
|
207 |
^ MissingClassInLiteralArrayErrorSignal |
|
208 |
||
209 |
"Created: / 18.5.1999 / 14:50:04 / cg" |
|
210 |
! ! |
|
211 |
||
7304 | 212 |
!SequenceableCollection class methodsFor:'instance creation-multiDimensional'! |
7255 | 213 |
|
214 |
_at:nIndices |
|
215 |
"this is a synthetic selector, generated by the compiler, |
|
216 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 217 |
I.e. |
218 |
Array[n] |
|
7255 | 219 |
generates |
8874 | 220 |
Array _at: n |
7255 | 221 |
" |
222 |
||
223 |
^ self new:nIndices |
|
224 |
! |
|
225 |
||
226 |
_at:dim1 at:dim2 |
|
227 |
"this is a synthetic selector, generated by the compiler, |
|
228 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 229 |
I.e. |
230 |
Array[n,m] |
|
7255 | 231 |
generates |
8874 | 232 |
Array _at:n at:m |
7255 | 233 |
" |
234 |
||
235 |
|data| |
|
236 |
||
237 |
data := self withSize:(dim1 * dim2). |
|
238 |
^ MultiDimensionalArrayAccessor |
|
8874 | 239 |
collection:data |
240 |
dimensions:(Array with:dim1 with:dim2) |
|
7255 | 241 |
! |
242 |
||
243 |
_at:dim1 at:dim2 at:dim3 |
|
244 |
"this is a synthetic selector, generated by the compiler, |
|
245 |
if a construct of the form expr[idx...] is parsed. |
|
8874 | 246 |
I.e. |
247 |
Array[n,m,o] |
|
7255 | 248 |
generates |
8874 | 249 |
Array _at:n at:m at:o |
7255 | 250 |
" |
251 |
||
252 |
|data| |
|
253 |
||
254 |
data := self withSize:(dim1 * dim2 * dim3). |
|
8874 | 255 |
^ MultiDimensionalArrayAccessor |
256 |
collection:data |
|
257 |
dimensions:(Array with:dim1 with:dim2 with:dim3) |
|
7255 | 258 |
! ! |
259 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
260 |
!SequenceableCollection class methodsFor:'instance creation-streaming'! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
261 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
262 |
streamContents:blockWithArg |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
263 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
264 |
evaluate blockWithArg, passing that stream, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
265 |
extract and return the streams contents." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
266 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
267 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
268 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
269 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
270 |
blockWithArg value:stream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
271 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
272 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
273 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
274 |
|rslt| |
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 |
rslt := String streamContents:[:s | s nextPutAll:'hello'; space; nextPutAll:'world'] |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
277 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
278 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
279 |
"Modified: / 29-03-2007 / 15:20:20 / cg" |
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 |
|
10590 | 282 |
streamContents:blockWithArg limitedTo:limit |
283 |
"create a limited write-stream on an instance of the receiver-class, |
|
284 |
evaluate blockWithArg, passing that stream, |
|
285 |
extract and return the streams contents (possibly truncated)." |
|
286 |
||
287 |
|stream| |
|
288 |
||
289 |
stream := self writeStream. |
|
290 |
stream writeLimit:limit. |
|
291 |
blockWithArg value:stream. |
|
292 |
^ stream contents |
|
293 |
||
294 |
"Created: / 04-06-2007 / 17:22:38 / cg" |
|
295 |
! |
|
296 |
||
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
297 |
streamContentsEnumerating:aReceiver using:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
298 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
299 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
300 |
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
|
301 |
enumerator somewhere, and you want this as a collection." |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
302 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
303 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
304 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
305 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
306 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
307 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
308 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
309 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
310 |
String streamContentsEnumerating:'hello' using:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
311 |
Array streamContentsEnumerating:Method using:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
312 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
313 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
314 |
"Created: / 29-03-2007 / 15:21:12 / cg" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
315 |
! |
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 |
streamContentsOf:aReceiver enumerator:enumeratorSelector |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
318 |
"create a write-stream on an instance of the receiver-class, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
319 |
enumerate on aReceiver using enumeratorSelector as a do-block-selector, |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
320 |
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
|
321 |
enumerator somewhere, and you want this as a collection." |
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 |
|stream| |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
324 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
325 |
stream := self writeStream. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
326 |
aReceiver perform:enumeratorSelector with:[:each |stream nextPut:each]. |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
327 |
^ stream contents |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
328 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
329 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
330 |
String streamContentsOf:'hello' enumerator:#reverseDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
331 |
Array streamContentsOf:Method enumerator:#allInstancesDo: |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
332 |
" |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
333 |
|
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
334 |
"Created: / 29-03-2007 / 15:05:30 / cg" |
13202 | 335 |
! |
336 |
||
337 |
writeStream |
|
338 |
"create a write-stream on an instance of the receiver-class" |
|
339 |
||
340 |
^ self writeStreamClass on:(self new:50). |
|
341 |
||
342 |
" |
|
343 |
OrderedCollection writeStream |
|
344 |
" |
|
345 |
||
346 |
"Modified: / 09-01-2011 / 10:37:35 / cg" |
|
347 |
! |
|
348 |
||
349 |
writeStreamClass |
|
350 |
"the type of stream used in writeStream" |
|
351 |
||
352 |
^ WriteStream |
|
353 |
||
354 |
" |
|
355 |
OrderedCollection writeStreamClass |
|
356 |
" |
|
357 |
||
358 |
"Created: / 09-01-2011 / 10:37:15 / cg" |
|
359 |
! |
|
360 |
||
361 |
writeStreamWithInitialSize:l |
|
362 |
"create a write-stream on an instance of the receiver-class" |
|
363 |
||
364 |
^ self writeStreamClass on:(self new:l). |
|
365 |
||
366 |
" |
|
367 |
OrderedCollection writeStream |
|
368 |
" |
|
369 |
||
370 |
"Created: / 09-01-2011 / 10:36:28 / cg" |
|
10479
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
371 |
! ! |
fcbd291cce60
+streamContentsEnumerating:using:
Claus Gittinger <cg@exept.de>
parents:
10476
diff
changeset
|
372 |
|
8893 | 373 |
!SequenceableCollection class methodsFor:'queries'! |
374 |
||
375 |
isAbstract |
|
11219 | 376 |
"Return if this class is an abstract class. |
377 |
True is returned for SequenceableCollection here; false for subclasses. |
|
378 |
Abstract subclasses must redefine again." |
|
379 |
||
8893 | 380 |
^ self == SequenceableCollection |
381 |
! ! |
|
382 |
||
13697 | 383 |
|
7261 | 384 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 385 |
|
7796 | 386 |
allButFirst |
8874 | 387 |
"Return a copy of the receiver containing all but the first element. |
7796 | 388 |
Raise an error if there are not enough elements." |
389 |
||
390 |
^ self copyFrom:2 |
|
8571 | 391 |
|
392 |
" |
|
393 |
'1234567890' allButFirst |
|
394 |
" |
|
395 |
! |
|
396 |
||
397 |
allButFirst:n |
|
8874 | 398 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 399 |
Raise an error if there are not enough elements." |
400 |
||
401 |
^ self copyFrom:n+1 |
|
402 |
||
403 |
" |
|
404 |
'1234567890' allButFirst:5 |
|
405 |
" |
|
7796 | 406 |
! |
407 |
||
6149 | 408 |
allButLast |
8874 | 409 |
"Return a copy of the receiver containing all but the last element. |
7796 | 410 |
Raise an error if there are not enough elements." |
6183 | 411 |
|
412 |
^ self allButLast:1 |
|
413 |
||
8571 | 414 |
" |
415 |
'1234567890' allButLast |
|
416 |
" |
|
417 |
||
6183 | 418 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
419 |
! |
|
420 |
||
421 |
allButLast:n |
|
8874 | 422 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 423 |
Raise an error if there are not enough elements." |
6183 | 424 |
|
425 |
^ self copyFrom: 1 to: self size - n |
|
426 |
||
8571 | 427 |
" |
428 |
'1234567890' allButLast:5 |
|
429 |
" |
|
430 |
||
6183 | 431 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 432 |
! |
433 |
||
8874 | 434 |
atPin: index |
4414 | 435 |
"Return the index'th element of me if possible. |
436 |
Return the first or last element if index is out of bounds." |
|
437 |
||
438 |
index < 1 ifTrue:[^ self first]. |
|
439 |
index > self size ifTrue:[^ self last]. |
|
440 |
^ self at:index |
|
441 |
||
442 |
" |
|
8874 | 443 |
#(1 2 3) atPin:4 |
444 |
#(1 2 3) atPin:0 |
|
445 |
#(1 2 3) atPin:1 |
|
446 |
#(1 2 3) atPin:3 |
|
447 |
" |
|
448 |
! |
|
449 |
||
450 |
atRandom |
|
4459 | 451 |
"Return any random element from the receiver" |
452 |
||
8573 | 453 |
^ self atRandom:Random |
454 |
||
455 |
" |
|
8874 | 456 |
#(1 2 3) atRandom |
457 |
" |
|
458 |
! |
|
459 |
||
460 |
atRandom:aRandomGenerator |
|
8573 | 461 |
"Return any random element from the receiver" |
462 |
||
4459 | 463 |
|max idx| |
464 |
||
465 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 466 |
self emptyCollectionError |
4459 | 467 |
]. |
8573 | 468 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 469 |
^ self at:idx |
470 |
||
471 |
" |
|
8874 | 472 |
#(1 2 3) atRandom:(Random new) |
473 |
" |
|
474 |
! |
|
475 |
||
476 |
atWrap:index |
|
4975 | 477 |
"Return the index'th element of me if possible. |
478 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
479 |
||
480 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
481 |
||
482 |
" |
|
8874 | 483 |
#(1 2 3) atWrap:1 |
484 |
#(1 2 3) atWrap:2 |
|
485 |
#(1 2 3) atWrap:3 |
|
486 |
#(1 2 3) atWrap:4 |
|
487 |
#(1 2 3) atWrap:5 |
|
488 |
#(1 2 3) atWrap:6 |
|
489 |
#(1 2 3) atWrap:7 |
|
490 |
#(1 2 3) atWrap:0 |
|
491 |
#(1 2 3) atWrap:-1 |
|
4975 | 492 |
" |
5372 | 493 |
! |
494 |
||
495 |
beginsWith:aCollection |
|
6183 | 496 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 497 |
|
498 |
^ self startsWith:aCollection |
|
6183 | 499 |
|
500 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 501 |
! |
502 |
||
8611 | 503 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 504 |
"squeak uses a different naming here - sigh. |
505 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
506 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
507 |
this method first takes a slice (from:to:) and then collects over that. |
|
508 |
I.e. it is equivalent to: |
|
8874 | 509 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 510 |
|
511 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
512 |
! |
|
513 |
||
7012 | 514 |
copyAfter: anElement |
8874 | 515 |
"Answer a copy of the receiver from after the first occurrence |
516 |
of anElement up to the end. If no such element exists, answer |
|
517 |
an empty copy." |
|
7012 | 518 |
|idx| |
519 |
||
520 |
idx := self indexOf:anElement. |
|
521 |
idx == 0 ifTrue:[idx := self size]. |
|
522 |
^ self copyFrom:idx + 1 |
|
523 |
||
524 |
" |
|
8874 | 525 |
'hello world' copyAfter:$l |
526 |
'123456123456' copyAfter:$2 |
|
527 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
528 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
529 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 530 |
" |
531 |
! |
|
532 |
||
6982 | 533 |
copyAfterLast:element |
8874 | 534 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 535 |
of element to the end; uses = for comparison" |
536 |
||
537 |
|idx| |
|
538 |
||
539 |
idx := self lastIndexOf:element. |
|
540 |
idx == 0 ifTrue:[idx := self size]. |
|
541 |
^ self copyFrom:idx + 1 |
|
542 |
||
543 |
" |
|
8874 | 544 |
'hello world' copyAfterLast:$l |
545 |
'123456123456' copyAfterLast:$2 |
|
546 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
547 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
548 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 549 |
" |
550 |
! |
|
551 |
||
6980 | 552 |
copyUpToLast:element |
8874 | 553 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 554 |
of element; uses = for comparison" |
555 |
||
556 |
|idx| |
|
557 |
||
558 |
idx := self lastIndexOf:element. |
|
559 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
560 |
^ self copyTo:idx - 1 |
|
561 |
||
562 |
" |
|
8874 | 563 |
'hello world' copyUpToLast:$l |
564 |
'123456123456' copyUpToLast:$2 |
|
565 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
566 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
567 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 568 |
" |
569 |
! |
|
570 |
||
8276 | 571 |
copyWithoutAll:elementsToSkip |
572 |
"return a new collection consisting of a copy of the receiver, with |
|
573 |
ALL elements equal to any in elementsToSkip are left out. |
|
574 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
575 |
||
576 |
|n "{ Class: SmallInteger }" |
|
577 |
sz "{ Class: SmallInteger }" |
|
578 |
srcIndex "{ Class: SmallInteger }" |
|
579 |
dstIndex "{ Class: SmallInteger }" |
|
580 |
skipIndex "{ Class: SmallInteger }" |
|
581 |
copy l| |
|
582 |
||
8874 | 583 |
"the code below may look like overkill, |
8276 | 584 |
however, for big collections its better to move data |
585 |
around in big chunks" |
|
586 |
||
587 |
n := self occurrencesOfAny:elementsToSkip. |
|
588 |
n == 0 ifTrue:[^ self copy]. |
|
589 |
||
590 |
sz := self size. |
|
591 |
copy := self copyEmptyAndGrow:(sz - n). |
|
592 |
||
593 |
srcIndex := 1. |
|
594 |
dstIndex := 1. |
|
595 |
||
596 |
n timesRepeat:[ |
|
8874 | 597 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
598 |
l := skipIndex - srcIndex. |
|
599 |
l ~~ 0 ifTrue:[ |
|
600 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
601 |
with:self startingAt:srcIndex. |
|
602 |
dstIndex := dstIndex + l |
|
603 |
]. |
|
604 |
srcIndex := skipIndex + 1 |
|
8276 | 605 |
]. |
606 |
l := sz - srcIndex. |
|
607 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 608 |
with:self startingAt:srcIndex. |
8276 | 609 |
^ copy |
610 |
||
611 |
" |
|
612 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 613 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 614 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
615 |
" |
|
616 |
! |
|
617 |
||
7304 | 618 |
copyWithoutFirst |
619 |
^ self copyFrom:2 |
|
620 |
! |
|
621 |
||
12051 | 622 |
shuffled |
623 |
"return a randomly shuffled copy of the receiver" |
|
624 |
||
625 |
^ self shuffledBy:Random |
|
626 |
||
627 |
" |
|
628 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
629 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
|
630 |
" |
|
631 |
! |
|
632 |
||
633 |
shuffledBy:aRandom |
|
634 |
"return a randomly shuffled copy of the receiver, using the given random generator" |
|
635 |
||
636 |
|copy| |
|
637 |
||
638 |
copy := self shallowCopy. |
|
639 |
copy size to: 1 by: -1 do:[:i | |
|
640 |
copy swap: i with: ((1 to: i) atRandom: aRandom) |
|
641 |
]. |
|
642 |
^ copy |
|
643 |
||
644 |
" |
|
645 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
646 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
|
647 |
" |
|
648 |
! |
|
649 |
||
11279 | 650 |
sortBy:aBlock |
651 |
"Sort inplace - destructive" |
|
652 |
||
653 |
self sort:aBlock |
|
654 |
||
655 |
"Created: / 22-10-2008 / 21:25:35 / cg" |
|
4414 | 656 |
! ! |
657 |
||
7261 | 658 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 659 |
|
660 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
661 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
662 |
! ! |
|
663 |
||
13697 | 664 |
|
1 | 665 |
!SequenceableCollection methodsFor:'accessing'! |
666 |
||
1366 | 667 |
after:anObject |
668 |
"return the element, after anObject. |
|
5231 | 669 |
If anObject is not in the receiver, report an error; |
670 |
if anObject is the last in the receiver, return nil. |
|
1366 | 671 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
672 |
||
673 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
674 |
||
675 |
" |
|
8874 | 676 |
#(4 3 2 1) asOrderedCollection after:3. |
677 |
#(4 3 2 1) asOrderedCollection after:5 |
|
678 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 679 |
" |
680 |
||
681 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
682 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
683 |
! |
|
684 |
||
685 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 686 |
"return the element, after anObject. |
5231 | 687 |
If anObject is the last in the receiver, return nil. |
688 |
If there is no such element (anObject is not found), |
|
1366 | 689 |
return the value from exceptionBlock." |
690 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
691 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
692 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
693 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
694 |
idx ~~ 0 ifTrue:[ |
8874 | 695 |
idx == self size ifTrue:[^ nil]. |
696 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
697 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
698 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
699 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
700 |
" |
8874 | 701 |
#(4 3 2 1) after:3 ifAbsent:nil. |
702 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
703 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
704 |
(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
|
705 |
" |
1366 | 706 |
! |
707 |
||
6967 | 708 |
anElement |
8874 | 709 |
"return any element from the collection, |
6967 | 710 |
report an error if there is none" |
711 |
||
712 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
713 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
714 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
715 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
716 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
717 |
" |
6967 | 718 |
! |
719 |
||
32 | 720 |
at:index ifAbsent:exceptionBlock |
8874 | 721 |
"return the element at index if valid. |
722 |
If the index is invalid, return the result of evaluating |
|
302 | 723 |
the exceptionblock. |
8874 | 724 |
NOTICE: |
725 |
in ST-80, this message is only defined for Dictionaries, |
|
726 |
however, having a common protocol with indexed collections |
|
727 |
often simplifies things." |
|
32 | 728 |
|
8579 | 729 |
(index between:1 and:self size) ifFalse:[ |
8874 | 730 |
^ exceptionBlock value |
32 | 731 |
]. |
732 |
^ self at:index |
|
733 |
||
95 | 734 |
" |
735 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 736 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
737 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 738 |
|
95 | 739 |
(Dictionary with:(#foo -> #bar) |
8874 | 740 |
with:(#frob -> #baz)) |
741 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 742 |
" |
362 | 743 |
! |
744 |
||
13003 | 745 |
atAllIndices:indexCollection |
746 |
"return the elements at each index from indexCollection." |
|
747 |
||
748 |
^ indexCollection collect:[:eachIdx | self at:eachIdx]. |
|
749 |
||
750 |
" |
|
751 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:#( 8 5 12 12 15) |
|
752 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:( 5 to: 10) |
|
753 |
" |
|
754 |
||
755 |
"Created: / 08-08-2010 / 01:15:06 / cg" |
|
756 |
! |
|
757 |
||
758 |
atIndex:index |
|
759 |
"return an element at a given index. This allows for seqentialCollections |
|
760 |
and orderedDictionaries to be both accessed via a numeric index." |
|
761 |
||
762 |
^ self at:index |
|
763 |
||
764 |
"Created: / 08-08-2010 / 00:50:10 / cg" |
|
765 |
! |
|
766 |
||
767 |
atIndex:index put:newValue |
|
768 |
"return an element at a given index. This allows for seqentialCollections |
|
769 |
and orderedDictionaries to be both accessed via a numeric index." |
|
770 |
||
771 |
^ self at:index put:newValue |
|
772 |
||
773 |
"Created: / 08-08-2010 / 00:50:27 / cg" |
|
774 |
! |
|
775 |
||
1366 | 776 |
before:anObject |
777 |
"return the element before the argument, anObject. |
|
5231 | 778 |
If anObject is not in the receiver, report an error; |
779 |
if anObject is the first in the receiver, return nil. |
|
1366 | 780 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
781 |
||
782 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
783 |
||
784 |
" |
|
8874 | 785 |
#(4 3 2 1) asOrderedCollection before:3. |
786 |
#(4 3 2 1) asOrderedCollection before:4 |
|
787 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 788 |
" |
789 |
||
790 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
791 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
792 |
! |
|
793 |
||
794 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 795 |
"return the element, before anObject. |
5231 | 796 |
If anObject is the first in the receiver, return nil. |
797 |
If there is no such element (anObject is not found), |
|
1366 | 798 |
return the value from exceptionBlock." |
799 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
800 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
801 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
802 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
803 |
idx ~~ 0 ifTrue:[ |
8874 | 804 |
idx == 1 ifTrue:[^ nil]. |
805 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
806 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
807 |
^ exceptionBlock value |
1366 | 808 |
|
5231 | 809 |
" |
8874 | 810 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
811 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
812 |
#(4 3 2 1) asOrderedCollection before:5. |
|
813 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
814 |
#(4 3 2 1) asOrderedCollection before:4. |
|
815 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
816 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 817 |
" |
818 |
||
1366 | 819 |
! |
820 |
||
602 | 821 |
first |
8874 | 822 |
"return the first element; |
5231 | 823 |
report an error, if the collection is empty." |
602 | 824 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
825 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 826 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
827 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
828 |
" |
8874 | 829 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
830 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
831 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
832 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
833 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 834 |
! |
835 |
||
5887 | 836 |
first:n |
837 |
"return the n first elements of the collection. |
|
838 |
Raises an error if there are not enough elements in the receiver." |
|
839 |
||
840 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
841 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
842 |
||
843 |
^ self copyFirst:n |
|
844 |
||
845 |
" |
|
8874 | 846 |
#(1 2 3 4 5) first:3 |
847 |
#(1 2 3 4 5) first:6 |
|
848 |
#(1 2 3 4 5) asSet first:3 |
|
849 |
'hello world' first:5 |
|
5887 | 850 |
" |
851 |
! |
|
852 |
||
11878 | 853 |
indexOfNth:n occurrenceOf:what |
854 |
"return the index of the nTh occurence of a value, or 0 if there are not that many" |
|
855 |
||
856 |
|idx cnt| |
|
857 |
||
858 |
n > self size ifTrue:[^ 0]. |
|
859 |
cnt := 0. |
|
860 |
idx := 0. |
|
861 |
[ |
|
862 |
idx := self indexOf:what startingAt:idx+1. |
|
863 |
idx == 0 ifTrue:[^ 0]. |
|
864 |
cnt := cnt + 1. |
|
865 |
cnt = n ifTrue:[ ^ idx]. |
|
866 |
] loop. |
|
867 |
||
868 |
" |
|
869 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:1 |
|
870 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:9 |
|
871 |
" |
|
872 |
! |
|
873 |
||
362 | 874 |
keyAtEqualValue:value |
875 |
"return the index of a value. |
|
876 |
This is normally not used (use indexOf:), but makes the |
|
877 |
protocol more compatible with dictionaries." |
|
878 |
||
879 |
^ self indexOf:value |
|
880 |
! |
|
881 |
||
882 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
883 |
"return the index of a value. |
|
884 |
This is normally not used (use indexOf:), but makes the |
|
885 |
protocol more compatible with dictionaries." |
|
886 |
||
887 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 888 |
! |
889 |
||
890 |
keyAtValue:value |
|
891 |
"return the index of a value. |
|
892 |
This is normally not used (use indexOf:), but makes the |
|
893 |
protocol more compatible with dictionaries." |
|
894 |
||
895 |
^ self identityIndexOf:value |
|
896 |
! |
|
897 |
||
898 |
keyAtValue:value ifAbsent:exceptionBlock |
|
899 |
"return the index of a value. |
|
900 |
This is normally not used (use indexOf:), but makes the |
|
901 |
protocol more compatible with dictionaries." |
|
902 |
||
903 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
904 |
! |
|
905 |
||
906 |
last |
|
5231 | 907 |
"return the last element; |
908 |
report an error, if the collection is empty." |
|
602 | 909 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
910 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
911 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
912 |
(sz := self size) == 0 ifFalse:[ |
8874 | 913 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
914 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
915 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
916 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
917 |
|
5231 | 918 |
" |
919 |
#(1 2 3 4 5) last |
|
920 |
#() last |
|
921 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
922 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
923 |
|
5887 | 924 |
last:n |
925 |
"return the n last elements of the collection. |
|
926 |
Raises an error if there are not enough elements in the receiver." |
|
927 |
||
928 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
929 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
930 |
||
931 |
^ self copyLast:n |
|
932 |
||
933 |
" |
|
8874 | 934 |
#(1 2 3 4 5) last:3 |
935 |
#(1 2 3 4 5) last:6 |
|
936 |
#(1 2 3 4 5) asSet last:3 |
|
937 |
'hello world' last:5 |
|
5887 | 938 |
" |
939 |
! |
|
940 |
||
4986 | 941 |
nth:n |
5231 | 942 |
"return the nth element; |
943 |
report an error, if the collections size is smaller than n." |
|
4986 | 944 |
|
945 |
^ self at:n |
|
946 |
||
947 |
" |
|
8874 | 948 |
args: |
4986 | 949 |
returns: nth element <object> |
950 |
" |
|
951 |
||
5231 | 952 |
" |
8874 | 953 |
#(10 20 30 40 50) nth:4 |
5231 | 954 |
#(10 20 30 40 50) nth:6 |
955 |
#() nth:1 |
|
956 |
" |
|
957 |
||
4986 | 958 |
! |
959 |
||
11915 | 960 |
order |
961 |
"the order is the set of keys " |
|
962 |
||
963 |
^ 1 to:self size |
|
964 |
! |
|
965 |
||
4855 | 966 |
second |
5231 | 967 |
"return the second element; |
968 |
report an error, if the collections size is smaller than 2." |
|
4855 | 969 |
|
970 |
^ self at:2 |
|
971 |
||
972 |
" |
|
8874 | 973 |
args: |
4855 | 974 |
returns: secondElement <object> |
975 |
" |
|
976 |
||
977 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
978 |
! |
|
979 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
980 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
981 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
982 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
983 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
984 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
985 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
986 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
987 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
988 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
989 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 990 |
! ! |
991 |
||
602 | 992 |
!SequenceableCollection methodsFor:'adding & removing'! |
993 |
||
994 |
add:anObject |
|
995 |
"append the argument, anObject to the collection. |
|
996 |
Return the argument, anObject. |
|
997 |
||
998 |
Notice, that this is modifies the receiver NOT a copy. |
|
999 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1000 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1001 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1002 |
||
1003 |
|newSize| |
|
1004 |
||
1005 |
newSize := self size + 1. |
|
1006 |
self grow:newSize. |
|
1007 |
self at:newSize put:anObject. |
|
1008 |
^ anObject |
|
1009 |
||
1010 |
" |
|
1011 |
|a| |
|
1012 |
a := #(1 2 3 4 5 6 7 8). |
|
1013 |
a add:'hello'. |
|
1014 |
a |
|
1015 |
" |
|
1016 |
" |
|
1017 |
|c| |
|
1018 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1019 |
c add:'hello'. |
|
1020 |
c |
|
1021 |
" |
|
1022 |
! |
|
1023 |
||
1024 |
add:anElement beforeIndex:index |
|
1025 |
"insert the first argument, anObject into the collection before slot index. |
|
1026 |
Return the receiver (sigh - ST-80 compatibility). |
|
1027 |
||
1028 |
Notice, that this is modifies the receiver NOT a copy. |
|
1029 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1030 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1031 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1032 |
||
1033 |
|newSize| |
|
1034 |
||
1035 |
newSize := self size + 1. |
|
1036 |
self grow:newSize. |
|
1037 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
1038 |
self at:index put:anElement |
|
1039 |
||
1040 |
" |
|
1041 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
1042 |
" |
|
202 | 1043 |
! |
1044 |
||
602 | 1045 |
addFirst:anObject |
1046 |
"prepend the argument, anObject to the collection. |
|
1047 |
Return the argument, anObject. |
|
1048 |
||
1049 |
Notice, that this is modifies the receiver NOT a copy. |
|
1050 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1051 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1052 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1053 |
||
1054 |
|newSize| |
|
1055 |
||
1056 |
newSize := self size + 1. |
|
1057 |
self grow:newSize. |
|
1058 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
1059 |
self at:1 put:anObject. |
|
1060 |
^ anObject |
|
1061 |
||
1062 |
" |
|
8874 | 1063 |
|a| |
1064 |
a:= #(1 2 3 4 5 6 7 8). |
|
1065 |
a addFirst:'hello'. |
|
1066 |
a |
|
602 | 1067 |
" |
1068 |
" |
|
1069 |
|c| |
|
1070 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1071 |
c addFirst:'hello'. |
|
1072 |
c |
|
1073 |
" |
|
69 | 1074 |
! |
1075 |
||
602 | 1076 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1077 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1078 |
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
|
1079 |
Use equality compare (=) to search for an occurrence. |
602 | 1080 |
|
1081 |
Notice, that this is modifies the receiver NOT a copy. |
|
1082 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1083 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1084 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1085 |
||
8874 | 1086 |
|any |
602 | 1087 |
dstIndex "{ Class: SmallInteger }" |
1088 |
sz "{ Class: SmallInteger }"| |
|
1089 |
||
1090 |
dstIndex := 1. |
|
1091 |
any := false. |
|
1092 |
sz := self size. |
|
1093 |
1 to:sz do:[:srcIndex | |
|
8874 | 1094 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1095 |
any := true |
|
1096 |
] ifFalse:[ |
|
1097 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1098 |
self at:dstIndex put:(self at:srcIndex) |
|
1099 |
]. |
|
1100 |
dstIndex := dstIndex + 1 |
|
1101 |
] |
|
602 | 1102 |
]. |
1103 |
any ifTrue:[ |
|
8874 | 1104 |
self grow:dstIndex - 1. |
1105 |
^ anElement |
|
602 | 1106 |
]. |
1107 |
^ aBlock value |
|
1108 |
||
1109 |
" |
|
1422 | 1110 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1111 |
#(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
|
1112 |
#(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
|
1113 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1114 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1115 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1116 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1117 |
! |
1118 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1119 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1120 |
"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
|
1121 |
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
|
1122 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1123 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1124 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1125 |
"/ 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
|
1126 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1127 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1128 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1129 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1130 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1131 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1132 |
[runIndex <= self size] whileTrue:[ |
8874 | 1133 |
element := self at:runIndex. |
1134 |
(aBlock value:element) ifTrue:[ |
|
1135 |
removed add:element. |
|
1136 |
self removeAtIndex:runIndex |
|
1137 |
] ifFalse:[ |
|
1138 |
runIndex := runIndex + 1 |
|
1139 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1140 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1141 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1142 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1143 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1144 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1145 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1146 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1147 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1148 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1149 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1150 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1151 |
"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
|
1152 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1153 |
|
602 | 1154 |
removeAtIndex:anIndex |
1155 |
"remove the element stored at anIndex. Return the removed object. |
|
1156 |
||
1157 |
Notice, that this is modifies the receiver NOT a copy. |
|
1158 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1159 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1160 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1161 |
||
1162 |
|element| |
|
1163 |
||
1164 |
element := self at:anIndex. |
|
1165 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1166 |
^ element |
|
1167 |
||
1168 |
" |
|
1169 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1170 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1171 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1172 |
" |
|
13 | 1173 |
! |
1174 |
||
602 | 1175 |
removeFirst |
1176 |
"remove the first element of the receiver and return it. |
|
1177 |
||
1178 |
Notice, that this is modifies the receiver NOT a copy. |
|
1179 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1180 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1181 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1182 |
||
1183 |
^ self removeAtIndex:1 |
|
1184 |
||
1185 |
" |
|
1186 |
|a| |
|
1187 |
a := #(1 2 3 4 5 6). |
|
1188 |
a removeFirst. |
|
8874 | 1189 |
a |
602 | 1190 |
" |
13 | 1191 |
! |
1192 |
||
2294 | 1193 |
removeFromIndex:startIndex |
1194 |
"remove the elements stored at indexes from startIndex to the end. |
|
1195 |
Return the receiver. |
|
1196 |
Returning the receiver is a historic leftover - it may at one |
|
1197 |
time return a collection of the removed elements. |
|
1198 |
||
8874 | 1199 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1200 |
therefore any other users of the receiver will also see this change. |
1201 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1202 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1203 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1204 |
||
1205 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1206 |
||
1207 |
" |
|
8874 | 1208 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1209 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1210 |
" |
|
1211 |
||
1212 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1213 |
! |
|
1214 |
||
602 | 1215 |
removeFromIndex:startIndex toIndex:endIndex |
1216 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1217 |
Return the receiver. |
|
1218 |
Returning the receiver is a historic leftover - it may at one |
|
1219 |
time return a collection of the removed elements. |
|
1220 |
||
8874 | 1221 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1222 |
therefore any other users of the receiver will also see this change. |
1223 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1224 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1225 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1226 |
||
1227 |
|size newSize| |
|
1228 |
||
1229 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1230 |
self grow:(startIndex - 1) |
602 | 1231 |
] ifFalse:[ |
8874 | 1232 |
newSize := size - endIndex + startIndex - 1. |
1233 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1234 |
self grow:newSize |
|
602 | 1235 |
] |
1236 |
||
1237 |
"/ |newSize| |
|
1238 |
"/ |
|
1239 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1240 |
"/ newSize <= 0 ifTrue:[ |
|
1241 |
"/ self grow:0 |
|
1242 |
"/ ] ifFalse:[ |
|
1243 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1244 |
"/ self grow:newSize |
|
1245 |
"/ ] |
|
1246 |
||
1247 |
" |
|
8874 | 1248 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1249 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1250 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1251 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1252 |
" |
1253 |
! |
|
1254 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1255 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1256 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1257 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1258 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1259 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1260 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1261 |
Also note, that it may be a slow operation for some collections, |
8874 | 1262 |
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
|
1263 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1264 |
|
2363 | 1265 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1266 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1267 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1268 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1269 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1270 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1271 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1272 |
1 to:sz do:[:srcIndex | |
8874 | 1273 |
el := self at:srcIndex. |
1274 |
(anElement == el) ifTrue:[ |
|
1275 |
any := true |
|
1276 |
] ifFalse:[ |
|
1277 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1278 |
self at:dstIndex put:el |
|
1279 |
]. |
|
1280 |
dstIndex := dstIndex + 1 |
|
1281 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1282 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1283 |
any ifTrue:[ |
8874 | 1284 |
self grow:dstIndex - 1. |
1285 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1286 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1287 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1288 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1289 |
" |
8874 | 1290 |
#(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
|
1291 |
#(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
|
1292 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1293 |
|
2363 | 1294 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1295 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1296 |
|
602 | 1297 |
removeIndex:index |
1298 |
"remove the argument stored at index. Return the receiver. |
|
1299 |
||
1300 |
Notice, that this is modifies the receiver NOT a copy. |
|
1301 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1302 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1303 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1304 |
||
1305 |
self removeFromIndex:index toIndex:index |
|
1306 |
||
1307 |
" |
|
1308 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1309 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1310 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1311 |
" |
|
77 | 1312 |
! |
1313 |
||
602 | 1314 |
removeLast |
1315 |
"remove the last element of the receiver and return it. |
|
1316 |
||
1317 |
Notice, that this is modifies the receiver NOT a copy. |
|
1318 |
Also note, that it may be a slow operation for some collections, |
|
1319 |
due to the grow:-message, which is inefficient for fixed size |
|
1320 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1321 |
||
8874 | 1322 |
^ self removeAtIndex:(self size) |
602 | 1323 |
|
1324 |
" |
|
1325 |
|a| |
|
1326 |
a := #(1 2 3 4 5 6). |
|
1327 |
a removeLast. |
|
8874 | 1328 |
a |
602 | 1329 |
" |
1330 |
! ! |
|
1331 |
||
11407 | 1332 |
!SequenceableCollection methodsFor:'combinatoric'! |
1333 |
||
1334 |
combinationsDo: aBlock |
|
11463 | 1335 |
"Repeatly evaluate aBlock with all combinations of elements from the receivers elements. |
1336 |
The receivers elements must be collections of the individuals to be taken for the combinations" |
|
11407 | 1337 |
|
1338 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1339 |
||
1340 |
" |
|
1341 |
(Array |
|
11410 | 1342 |
with:($a to:$d)) |
1343 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1344 |
" |
|
1345 |
" |
|
1346 |
(Array |
|
11407 | 1347 |
with:($a to:$d) |
1348 |
with:(1 to: 4)) |
|
1349 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1350 |
" |
|
1351 |
" |
|
1352 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1353 |
" |
|
11408 | 1354 |
"all combinations of two letters are generated with: |
1355 |
(Array |
|
1356 |
with:($a to:$z) |
|
1357 |
with:($a to:$z)) |
|
1358 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1359 |
" |
|
11432 | 1360 |
" |
1361 |
(Array |
|
1362 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1363 |
with:#(A)) |
|
1364 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1365 |
" |
|
11407 | 1366 |
! |
1367 |
||
1368 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1369 |
"a helper for combinationsDo:" |
|
1370 |
||
11431 | 1371 |
|loopedElement| |
1372 |
||
1373 |
loopedElement := self at:anInteger. |
|
1374 |
||
11407 | 1375 |
anInteger == self size ifTrue:[ |
11431 | 1376 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1377 |
^ self. |
11431 | 1378 |
]. |
1379 |
||
1380 |
loopedElement do:[:el | |
|
1381 |
|newPrefix| |
|
1382 |
||
1383 |
newPrefix := (prefix copyWith:el). |
|
1384 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1385 |
]. |
1386 |
||
1387 |
" |
|
1388 |
(Array |
|
1389 |
with:($a to:$d) |
|
1390 |
with:(1 to: 4)) |
|
1391 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1392 |
" |
|
11431 | 1393 |
" |
1394 |
(Array |
|
1395 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1396 |
with:#(A)) |
|
1397 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1398 |
" |
|
11407 | 1399 |
! |
1400 |
||
11462 | 1401 |
lexicographicPermutationsDo:aBlock |
1402 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1403 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1404 |
permutations in lexicographical order (i.e. sorted by size). |
|
11932 | 1405 |
|
11462 | 1406 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1407 |
the identical same collection (but with different contents) every time. |
1408 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1409 |
of permuted copies. The user of this function must be careful in the block, |
|
1410 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1411 |
|
1412 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1413 |
||
1414 |
" |
|
1415 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1416 |
" |
|
1417 |
! |
|
1418 |
||
1419 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1420 |
"a helper for lexicographicPermutationsDo:" |
|
1421 |
||
1422 |
|mySize values indices| |
|
1423 |
||
1424 |
mySize := self size. |
|
1425 |
anInteger > mySize ifTrue: [^ self]. |
|
1426 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1427 |
values := self copyFrom:anInteger to:mySize. |
|
1428 |
indices := (anInteger to:mySize) asArray. |
|
1429 |
values sortWith:indices. |
|
1430 |
indices do:[:i | |
|
1431 |
self swap: anInteger with: i. |
|
1432 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1433 |
self swap: anInteger with: i |
|
1434 |
] |
|
1435 |
||
1436 |
" |
|
1437 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1438 |
" |
|
1439 |
! |
|
1440 |
||
1441 |
permutationsDo:aBlock |
|
1442 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1443 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1444 |
|
11462 | 1445 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1446 |
the identical same collection (but with different contents) every time. |
1447 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1448 |
of permuted copies. The user of this function must be careful in the block, |
|
1449 |
to copy the argument, if it has to be stored somewhere." |
|
11407 | 1450 |
|
1451 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1452 |
||
1453 |
" |
|
11462 | 1454 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1455 |
" |
1456 |
||
1457 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1458 |
! |
|
1459 |
||
1460 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1461 |
"a helper for permutationsDo:" |
11407 | 1462 |
|
1463 |
|mySize| |
|
1464 |
||
1465 |
mySize := self size. |
|
11462 | 1466 |
anInteger > mySize ifTrue: [^ self]. |
1467 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1468 |
anInteger to:mySize do:[:i | |
1469 |
self swap: anInteger with: i. |
|
11462 | 1470 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1471 |
self swap: anInteger with: i |
1472 |
] |
|
1473 |
||
1474 |
" |
|
1475 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1476 |
|
1477 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1478 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1479 |
" |
1480 |
||
1481 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1482 |
! |
1483 |
||
1484 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1485 |
"a helper for permutationsDo:" |
|
1486 |
||
1487 |
|mySize| |
|
1488 |
||
1489 |
mySize := self size. |
|
1490 |
anInteger > mySize ifTrue: [^ self]. |
|
1491 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1492 |
anInteger to:mySize do:[:i | |
|
1493 |
self swap: anInteger with:i. |
|
1494 |
anotherCollection swap: anInteger with:i. |
|
1495 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1496 |
self swap:anInteger with: i. |
|
1497 |
anotherCollection swap:anInteger with:i. |
|
1498 |
] |
|
1499 |
||
1500 |
" |
|
1501 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1502 |
||
1503 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1504 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1505 |
" |
|
1506 |
||
1507 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1508 |
! |
|
1509 |
||
1510 |
permutationsWith:anotherCollection do:aBlock |
|
1511 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1512 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
11932 | 1513 |
|
11462 | 1514 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
11932 | 1515 |
the identical same collection (but with different contents) every time. |
1516 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
|
1517 |
of permuted copies. The user of this function must be careful in the block, |
|
1518 |
to copy the argument, if it has to be stored somewhere." |
|
11462 | 1519 |
|
1520 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1521 |
||
1522 |
" |
|
1523 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1524 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1525 |
" |
|
1526 |
||
1527 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1528 |
! ! |
1529 |
||
602 | 1530 |
!SequenceableCollection methodsFor:'comparing'! |
1531 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1532 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1533 |
"compare two sequenceable collections" |
8874 | 1534 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1535 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1536 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1537 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1538 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1539 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1540 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1541 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1542 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1543 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1544 |
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
|
1545 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1546 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1547 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1548 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1549 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1550 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1551 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1552 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1553 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1554 |
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
|
1555 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1556 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1557 |
(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
|
1558 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1559 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1560 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1561 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1562 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1563 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1564 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1565 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1566 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1567 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1568 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1569 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1570 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1571 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1572 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1573 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1574 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1575 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1576 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1577 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1578 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1579 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1580 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1581 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1582 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1583 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1584 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1585 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1586 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1587 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1588 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1589 |
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
|
1590 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1591 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1592 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1593 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1594 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1595 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1596 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1597 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1598 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1599 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1600 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1601 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1602 |
"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
|
1603 |
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
|
1604 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1605 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1606 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1607 |
|
602 | 1608 |
= aCollection |
1609 |
"return true if the receiver and aCollection represent collections |
|
12290 | 1610 |
with equal contents, and if they are of the same species." |
602 | 1611 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1612 |
| stop "{ Class: SmallInteger }" | |
602 | 1613 |
|
1614 |
(aCollection == self) ifTrue:[^true]. |
|
1615 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1616 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1617 |
|
1618 |
stop := self size. |
|
1619 |
stop == (aCollection size) ifFalse:[^false]. |
|
1620 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1621 |
1 to:stop do:[:index| |
9366 | 1622 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1623 |
]. |
1624 |
^ true |
|
1625 |
||
1626 |
" |
|
8874 | 1627 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1628 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1629 |
#($1 $2 $3 $4 $5) = '12345' |
|
1630 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1631 |
" |
1632 |
! |
|
1633 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1634 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1635 |
"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
|
1636 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1637 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1638 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1639 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1640 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1641 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1642 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1643 |
"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
|
1644 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1645 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1646 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1647 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1648 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1649 |
|
8874 | 1650 |
commonPrefixWith:aCollection |
6507 | 1651 |
"return the common prefix of myself and the argument, aCollection. |
1652 |
If there is none, an empty collection is returned." |
|
1653 |
||
1654 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1655 |
||
1656 |
" |
|
8874 | 1657 |
'hello' commonPrefixWith:'hello' |
1658 |
'hello' commonPrefixWith:'hElLo' |
|
1659 |
'hello' commonPrefixWith:'hello world' |
|
1660 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1661 |
||
1662 |
'hello world' commonPrefixWith:'hello' |
|
1663 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1664 |
||
1665 |
'abcd' commonPrefixWith:'bcde' |
|
1666 |
||
1667 |
'abcd' commonPrefixWith:'abab' |
|
1668 |
'abcd' commonPrefixWith:'ab' |
|
1669 |
'abcd' commonPrefixWith:'ababab' |
|
1670 |
'abcd' commonPrefixWith:'abcdef' |
|
1671 |
||
1672 |
'abab' commonPrefixWith:'abcd' |
|
1673 |
'ab' commonPrefixWith:'abcd' |
|
1674 |
'ababab' commonPrefixWith:'abcd' |
|
1675 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1676 |
" |
1677 |
! |
|
1678 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1679 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1680 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1681 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1682 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1683 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1684 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1685 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1686 |
1 to:matchLen do:[:idx | |
8874 | 1687 |
|elHere elThere same| |
1688 |
||
1689 |
elHere := self at:idx. |
|
1690 |
elThere := aCollection at:idx. |
|
1691 |
||
1692 |
ignoreCase ifTrue:[ |
|
1693 |
same := elHere sameAs:elThere |
|
1694 |
] ifFalse:[ |
|
1695 |
same := elHere = elThere |
|
1696 |
]. |
|
1697 |
same ifFalse:[ |
|
1698 |
^ self copyTo:(idx - 1). |
|
1699 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1700 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1701 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1702 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1703 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1704 |
" |
8874 | 1705 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1706 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1707 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1708 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1709 |
||
1710 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1711 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1712 |
||
1713 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1714 |
||
1715 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1716 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1717 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1718 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1719 |
||
1720 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1721 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1722 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1723 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1724 |
" |
|
1725 |
! |
|
1726 |
||
1727 |
commonSuffixWith:aCollection |
|
6507 | 1728 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1729 |
If there is none, an empty collection is returned." |
|
1730 |
||
1731 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1732 |
||
1733 |
" |
|
8874 | 1734 |
'hello' commonSuffixWith:'hello' |
1735 |
'hello' commonSuffixWith:'hElLo' |
|
1736 |
'hello' commonSuffixWith:'hello world' |
|
1737 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1738 |
'hello2 world' commonSuffixWith:'hello world' |
|
1739 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1740 |
||
1741 |
'hello world' commonSuffixWith:'world' |
|
1742 |
'hello WoRlD' commonSuffixWith:'world' |
|
1743 |
||
1744 |
'dcba' commonSuffixWith:'edcb' |
|
1745 |
||
1746 |
'dcba' commonSuffixWith:'baba' |
|
1747 |
'dcba' commonSuffixWith:'ba' |
|
1748 |
'dcba' commonSuffixWith:'bababa' |
|
1749 |
'dcba' commonSuffixWith:'fdcba' |
|
1750 |
||
1751 |
'baba' commonSuffixWith:'dcba' |
|
1752 |
'ba' commonSuffixWith:'dcba' |
|
1753 |
'bababa' commonSuffixWith:'dcba' |
|
1754 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1755 |
" |
1756 |
! |
|
1757 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1758 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1759 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1760 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1761 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1762 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1763 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1764 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1765 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1766 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1767 |
1 to:matchLen do:[:idx | |
8874 | 1768 |
|elHere elThere same| |
1769 |
||
1770 |
elHere := self at:(l1 - idx + 1). |
|
1771 |
elThere := aCollection at:(l2 - idx + 1). |
|
1772 |
||
1773 |
ignoreCase ifTrue:[ |
|
1774 |
same := elHere sameAs:elThere |
|
1775 |
] ifFalse:[ |
|
1776 |
same := elHere = elThere |
|
1777 |
]. |
|
1778 |
same ifFalse:[ |
|
1779 |
^ self copyFrom:(l1 - idx + 2). |
|
1780 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1781 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1782 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1783 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1784 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1785 |
" |
8874 | 1786 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1787 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1788 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1789 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1790 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1791 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1792 |
||
1793 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1794 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1795 |
||
1796 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1797 |
||
1798 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1799 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1800 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1801 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1802 |
||
1803 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1804 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1805 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1806 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1807 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1808 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1809 |
|
5813 | 1810 |
deepSameContentsAs:aCollection |
1811 |
"return true, if the receiver and the arg have the same contents |
|
1812 |
in both the named instance vars and any indexed instVars. |
|
1813 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1814 |
||
1815 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1816 |
unused space" |
|
1817 |
||
1818 |
|index "{ Class: SmallInteger }" |
|
1819 |
stop "{ Class: SmallInteger }" | |
|
1820 |
||
1821 |
(aCollection == self) ifTrue:[^true]. |
|
1822 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1823 |
||
1824 |
stop := self size. |
|
1825 |
stop == (aCollection size) ifFalse:[^false]. |
|
1826 |
||
1827 |
index := 1. |
|
1828 |
[index <= stop] whileTrue:[ |
|
8874 | 1829 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1830 |
index := index + 1 |
|
5813 | 1831 |
]. |
1832 |
^ true |
|
1833 |
||
1834 |
" |
|
8874 | 1835 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1836 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1837 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1838 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1839 |
" |
1840 |
! |
|
1841 |
||
602 | 1842 |
endsWith:aCollection |
1843 |
"return true, if the receivers last elements match those |
|
1844 |
of aCollection" |
|
1845 |
||
1846 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1847 |
index2 "{ Class: SmallInteger }" |
1848 |
stop "{ Class: SmallInteger }" |
|
602 | 1849 |
sz "{ Class: SmallInteger }"| |
1850 |
||
1851 |
(aCollection == self) ifTrue:[^ true]. |
|
1852 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1853 |
||
1854 |
stop := aCollection size. |
|
77 | 1855 |
sz := self size. |
602 | 1856 |
stop > sz ifTrue:[^false]. |
1857 |
||
1858 |
index1 := sz. |
|
1859 |
index2 := stop. |
|
1860 |
[index2 > 0] whileTrue:[ |
|
8874 | 1861 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1862 |
index1 := index1 - 1. |
|
1863 |
index2 := index2 - 1 |
|
77 | 1864 |
]. |
602 | 1865 |
^ true |
1866 |
||
1867 |
" |
|
1868 |
'abcde' endsWith:#($d $e) |
|
8874 | 1869 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1870 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1871 |
" |
|
1872 |
! |
|
1873 |
||
7278 | 1874 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1875 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1876 |
|
1877 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1878 |
||
1879 |
" |
|
1880 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1881 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1882 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1883 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1884 |
" |
1885 |
! |
|
1886 |
||
11820 | 1887 |
hasEqualElements: otherCollection |
1888 |
"Answer whether the receiver's size is the same as otherCollection's |
|
12290 | 1889 |
size, and each of the receiver's elements equal the corresponding |
1890 |
element of otherCollection. |
|
1891 |
This should probably replace the current definition of #= ." |
|
1892 |
||
1893 |
^ self isSameSequenceAs:otherCollection |
|
11820 | 1894 |
! |
1895 |
||
602 | 1896 |
hash |
1897 |
"return a hash key for the receiver" |
|
1898 |
||
1899 |
"this hash is stupid - but for larger collections, the hashing |
|
1900 |
time can become much bigger than the time lost in added probing. |
|
1901 |
Time will show ..." |
|
1902 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1903 |
|mySize h| |
3653 | 1904 |
|
1905 |
mySize := self size. |
|
1906 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1907 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1908 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1909 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1910 |
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
|
1911 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1912 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1913 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1914 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1915 |
"/ 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
|
1916 |
"/ ^ ((((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
|
1917 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1918 |
" |
8874 | 1919 |
#(1 2 3 4 5 6) hash |
1920 |
(1 to:6) hash |
|
1921 |
||
1922 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1923 |
#(1 2 3 4 5) hash |
|
602 | 1924 |
" |
3653 | 1925 |
|
1926 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1927 |
! |
1928 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1929 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1930 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1931 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1932 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1933 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1934 |
^ self |
8874 | 1935 |
sameContentsAs:aCollection |
1936 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1937 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1938 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1939 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1940 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1941 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1942 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1943 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1944 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1945 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1946 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1947 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1948 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1949 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1950 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1951 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1952 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1953 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1954 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1955 |
" |
8874 | 1956 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1957 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1958 |
#($1 $2 $3 $4 $5) = '12345' |
|
1959 |
||
1960 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1961 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1962 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1963 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1964 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1965 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1966 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1967 |
|
12289
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1968 |
isSameSequenceAs:otherCollection |
8874 | 1969 |
"Answer whether the receiver's size is the same as otherCollection's size, |
12290 | 1970 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
1971 |
This should probably replace the current definition of #= ." |
|
6118 | 1972 |
|
1973 |
| size | |
|
1974 |
||
12289
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1975 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1976 |
otherCollection isSequenceable ifFalse: [^ false]. |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1977 |
|
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1978 |
1 to:size do:[:index | |
7fcbb93d365e
changed: #isSameSequenceAs:
Claus Gittinger <cg@exept.de>
parents:
12187
diff
changeset
|
1979 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false] |
6118 | 1980 |
]. |
1981 |
^ true |
|
1982 |
! |
|
1983 |
||
12938 | 1984 |
sameContentsAs:aCollection |
1985 |
"return true, if the receiver and the arg have the same contents. |
|
1986 |
||
1987 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1988 |
unused space" |
|
1989 |
||
1990 |
|index "{ Class: SmallInteger }" |
|
1991 |
stop "{ Class: SmallInteger }" | |
|
1992 |
||
1993 |
(aCollection == self) ifTrue:[^true]. |
|
1994 |
(aCollection isSequenceable) ifFalse:[ |
|
1995 |
aCollection with:self do:[:e1 :e2 | |
|
1996 |
(e1 = e2) ifFalse:[^false]. |
|
1997 |
]. |
|
1998 |
^ true |
|
1999 |
]. |
|
2000 |
||
2001 |
stop := self size. |
|
2002 |
stop == (aCollection size) ifFalse:[^false]. |
|
2003 |
||
2004 |
index := 1. |
|
2005 |
[index <= stop] whileTrue:[ |
|
2006 |
((self at:index) = (aCollection at:index)) ifFalse:[^false]. |
|
2007 |
index := index + 1 |
|
2008 |
]. |
|
2009 |
^ true |
|
2010 |
||
2011 |
" |
|
2012 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) copy |
|
2013 |
#($1 $2 $3 $4 $5) sameContentsAs: #(1 2 3 4 5) |
|
2014 |
#($1 $2 $3 $4 $5) sameContentsAs: '12345' |
|
2015 |
#($1 $2 $3 $4 $5) sameContentsAs: '54321' asSortedCollection |
|
2016 |
" |
|
2017 |
||
2018 |
"Created: / 09-07-2010 / 12:44:28 / sr" |
|
2019 |
! |
|
2020 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2021 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2022 |
"return true if the receiver and aCollection represent collections |
8874 | 2023 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2024 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2025 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2026 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2027 |
" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2028 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2029 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2030 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2031 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2032 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2033 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2034 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 2035 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2036 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2037 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2038 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2039 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2040 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2041 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2042 |
[index <= stop] whileTrue:[ |
8874 | 2043 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
2044 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2045 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2046 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2047 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2048 |
" |
8874 | 2049 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
2050 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
2051 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
2052 |
#(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
|
2053 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2054 |
#('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
|
2055 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2056 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2057 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2058 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2059 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2060 |
|
602 | 2061 |
startsWith:aCollection |
2062 |
"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
|
2063 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
2064 |
If the argument is empty, true is returned." |
602 | 2065 |
|
2066 |
|index "{ Class: SmallInteger }" |
|
2067 |
stop "{ Class: SmallInteger }" | |
|
2068 |
||
2069 |
(aCollection == self) ifTrue:[^true]. |
|
2070 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
2071 |
||
2072 |
stop := aCollection size. |
|
2073 |
stop > self size ifTrue:[^false]. |
|
2074 |
||
2075 |
index := 1. |
|
2076 |
[index <= stop] whileTrue:[ |
|
8874 | 2077 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
2078 |
index := index + 1 |
|
602 | 2079 |
]. |
2080 |
^ true |
|
2081 |
||
2082 |
" |
|
2083 |
'abcde' startsWith:#($a $b $c) |
|
2084 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
2085 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 2086 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 2087 |
" |
7279 | 2088 |
! |
2089 |
||
2090 |
startsWithAnyOf:aCollectionOfCollections |
|
2091 |
"return true, if the receiver starts with any in aCollection" |
|
2092 |
||
2093 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
2094 |
||
2095 |
" |
|
2096 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
2097 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 2098 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
2099 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 2100 |
" |
13 | 2101 |
! ! |
2102 |
||
260 | 2103 |
!SequenceableCollection methodsFor:'converting'! |
2104 |
||
11879
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2105 |
asCollectionOfSubCollectionsOfSize:pieceSize |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2106 |
"return a collection containing pieces of size pieceSite from the receiver. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2107 |
The last piece may be smaller, if the receivers size is not a multiple of pieceSize." |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2108 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2109 |
|pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2110 |
start "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2111 |
stop "{ Class:SmallInteger }" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2112 |
mySize "{ Class:SmallInteger }"| |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2113 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2114 |
pieces := OrderedCollection new. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2115 |
start := 1. stop := start + pieceSize - 1. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2116 |
mySize := self size. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2117 |
[stop <= mySize] whileTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2118 |
pieces add:(self copyFrom:start to:stop). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2119 |
start := start + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2120 |
stop := stop + pieceSize. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2121 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2122 |
(start <= mySize) ifTrue:[ |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2123 |
pieces add:(self copyFrom:start to:mySize). |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2124 |
]. |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2125 |
^ pieces |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2126 |
|
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2127 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2128 |
'123123123123123123' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2129 |
'12312312312312312312' asCollectionOfSubCollectionsOfSize:3 |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2130 |
" |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2131 |
! |
e7d20a6e6719
+asCollectionOfSubCollectionsOfSize:
Claus Gittinger <cg@exept.de>
parents:
11878
diff
changeset
|
2132 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2133 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 2134 |
"return a collection containing the subcollections (separated by anElement) |
2135 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2136 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2137 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2138 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2139 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2140 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2141 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2142 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2143 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2144 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2145 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2146 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2147 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2148 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2149 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2150 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2151 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2152 |
1 to:numberOfCols do:[:lineNr | |
8874 | 2153 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
2154 |
stopIndex == 0 ifTrue:[ |
|
2155 |
stopIndex := self size |
|
2156 |
] ifFalse: [ |
|
2157 |
stopIndex := stopIndex - 1. |
|
2158 |
]. |
|
2159 |
||
2160 |
(stopIndex < startIndex) ifTrue: [ |
|
2161 |
cols add:(myClass new:0) |
|
2162 |
] ifFalse: [ |
|
2163 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
2164 |
]. |
|
2165 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2166 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2167 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2168 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2169 |
" |
8874 | 2170 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2171 |
'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
|
2172 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2173 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2174 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2175 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2176 |
|
9234 | 2177 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
2178 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2179 |
of the receiver by anElement. |
|
2180 |
If anElement occurs multiple times in a row, |
|
2181 |
the block will be invoked with empty collections as argument. |
|
2182 |
This algorithm uses equality-compare to detect the element." |
|
2183 |
||
2184 |
|subCollection |
|
2185 |
endIndex "{ Class:SmallInteger }" |
|
2186 |
startIndex "{ Class:SmallInteger }" |
|
2187 |
stopIndex "{ Class:SmallInteger }" | |
|
2188 |
||
2189 |
startIndex := 0. |
|
2190 |
endIndex := self size. |
|
2191 |
||
2192 |
[startIndex <= endIndex] whileTrue:[ |
|
2193 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2194 |
stopIndex == 0 ifTrue:[ |
|
2195 |
stopIndex := self size |
|
2196 |
] ifFalse: [ |
|
2197 |
stopIndex := stopIndex - 1. |
|
2198 |
]. |
|
2199 |
||
2200 |
(stopIndex < startIndex) ifTrue: [ |
|
2201 |
subCollection := self species new:0 |
|
2202 |
] ifFalse: [ |
|
2203 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2204 |
]. |
|
2205 |
aBlock value:subCollection. |
|
2206 |
startIndex := stopIndex + 1 |
|
2207 |
]. |
|
2208 |
||
2209 |
" |
|
2210 |
'' |
|
2211 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2212 |
||
2213 |
'1 one' |
|
2214 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2215 |
||
2216 |
'1 one:2 two:3 three:4 four:5 five' |
|
2217 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2218 |
||
2219 |
'a::b' |
|
2220 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2221 |
||
2222 |
':' |
|
2223 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2224 |
||
2225 |
':a' |
|
2226 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2227 |
||
2228 |
'a:' |
|
2229 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2230 |
" |
|
2231 |
! |
|
2232 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2233 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2234 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2235 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2236 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2237 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2238 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2239 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2240 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2241 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2242 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2243 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2244 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2245 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2246 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2247 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2248 |
[done] whileFalse:[ |
8874 | 2249 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2250 |
stopIndex == 0 ifTrue:[ |
|
2251 |
stopIndex := self size. |
|
2252 |
done := true. |
|
2253 |
] ifFalse: [ |
|
2254 |
stopIndex := stopIndex - 1. |
|
2255 |
]. |
|
2256 |
||
2257 |
(stopIndex < startIndex) ifTrue: [ |
|
2258 |
items add:(myClass new:0) |
|
2259 |
] ifFalse: [ |
|
2260 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2261 |
]. |
|
2262 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2263 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2264 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2265 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2266 |
" |
8874 | 2267 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2268 |
#(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
|
2269 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2270 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2271 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2272 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2273 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2274 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2275 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2276 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2277 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2278 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2279 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2280 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2281 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2282 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2283 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2284 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2285 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2286 |
#(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
|
2287 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2288 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2289 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2290 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2291 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2292 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2293 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2294 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2295 |
|words |
8874 | 2296 |
start "{ Class:SmallInteger }" |
2297 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2298 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2299 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2300 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2301 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2302 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2303 |
[start <= mySize] whileTrue:[ |
8874 | 2304 |
"skip multiple separators" |
2305 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2306 |
start := start + 1 . |
|
2307 |
start > mySize ifTrue:[ |
|
2308 |
^ words |
|
2309 |
]. |
|
2310 |
]. |
|
2311 |
||
2312 |
stop := self findFirst:aBlock startingAt:start. |
|
2313 |
stop == 0 ifTrue:[ |
|
2314 |
words add:(self copyFrom:start to:mySize). |
|
2315 |
^ words |
|
2316 |
]. |
|
2317 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2318 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2319 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2320 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2321 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2322 |
" |
8874 | 2323 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2324 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2325 |
#(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
|
2326 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2327 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2328 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2329 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2330 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2331 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2332 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2333 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2334 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2335 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2336 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2337 |
|
1413 | 2338 |
asStringCollection |
2339 |
"return a new string collection containing the elements; |
|
2340 |
these ought to be strings. (i.e. String or Text instances)" |
|
2341 |
||
2342 |
|newColl sz| |
|
2343 |
||
2344 |
sz := self size. |
|
9162 | 2345 |
newColl := StringCollection withSize:sz. |
1413 | 2346 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2347 |
^ newColl |
|
2348 |
||
2349 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2350 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2351 |
! |
|
2352 |
||
1188 | 2353 |
asStringWith:sepChar |
8874 | 2354 |
"return a string generated by concatenating my elements |
1188 | 2355 |
(which must be strings or nil) and embedding sepChar characters in between. |
2356 |
Nil entries and empty strings are counted as empty lines." |
|
2357 |
||
8874 | 2358 |
^ self |
12941
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2359 |
from:1 to:(self size) |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2360 |
asStringWith:sepChar |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2361 |
compressTabs:false |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2362 |
final:nil |
1413 | 2363 |
|
2364 |
" |
|
2365 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
12941
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2366 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:'|' |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2367 |
'hello|world|foo|bar|baz' asCollectionOfSubstringsSeparatedBy:$| |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2368 |
" |
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2369 |
|
967f28e7c0b1
comment/format in: #asStringWith:
Claus Gittinger <cg@exept.de>
parents:
12938
diff
changeset
|
2370 |
"Modified: / 10-07-2010 / 22:59:29 / cg" |
1188 | 2371 |
! |
2372 |
||
2373 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2374 |
"return part of myself as a string with embedded sepCharacters. |
|
2375 |
My elements must be strings or nil; nil entries and empty strings are |
|
2376 |
taken as empty lines." |
|
2377 |
||
8874 | 2378 |
^ self |
2379 |
from:firstLine to:lastLine |
|
2380 |
asStringWith:sepCharacter |
|
2381 |
compressTabs:false |
|
2382 |
final:nil |
|
1188 | 2383 |
" |
2384 |
creating entries for searchpath: |
|
2385 |
||
8874 | 2386 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2387 |
||
2388 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2389 |
" |
2390 |
||
2391 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2392 |
! |
|
2393 |
||
260 | 2394 |
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
|
2395 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2396 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2397 |
taken as empty lines. |
2398 |
If the argument compressTabs is true, leading spaces are converted |
|
2399 |
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
|
2400 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2401 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2402 |
^ self |
8874 | 2403 |
from:firstLine to:lastLine |
2404 |
asStringWith:sepCharacter |
|
2405 |
compressTabs:compressTabs |
|
2406 |
final:endCharacter |
|
2407 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2408 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2409 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2410 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2411 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2412 |
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
|
2413 |
"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
|
2414 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2415 |
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
|
2416 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2417 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2418 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2419 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2420 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2421 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2422 |
any emphasis. If false, a plain string is returned. |
260 | 2423 |
This method is tuned for big collections, in not creating many |
263 | 2424 |
intermediate strings (has linear runtime). For very small collections |
2425 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2426 |
|
5575 | 2427 |
^ self |
8874 | 2428 |
from:firstLine to:lastLine |
2429 |
asStringWith:sepCharacter |
|
2430 |
compressTabs:compressTabs |
|
2431 |
final:endCharacter |
|
2432 |
withEmphasis:withEmphasis |
|
5575 | 2433 |
! |
2434 |
||
2435 |
asStringWithCRs |
|
8874 | 2436 |
"return a string generated by concatenating my elements |
5575 | 2437 |
(which must be strings or nil) and embedding cr characters in between. |
2438 |
Nil entries and empty strings are counted as empty lines." |
|
2439 |
||
2440 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2441 |
||
2442 |
" |
|
8874 | 2443 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2444 |
|
2445 |
(OrderedCollection new |
|
8874 | 2446 |
add:'hello'; |
2447 |
add:'world'; |
|
2448 |
add:'foo'; |
|
2449 |
add:('bar' asText allBold); |
|
2450 |
yourself) asStringWithCRs |
|
5575 | 2451 |
|
2452 |
Transcript showCR: |
|
8874 | 2453 |
(OrderedCollection new |
2454 |
add:'hello'; |
|
2455 |
add:'world'; |
|
2456 |
add:'foo'; |
|
2457 |
add:('bar' asText allBold); |
|
2458 |
yourself) asStringWithCRs |
|
5575 | 2459 |
" |
2460 |
||
2461 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2462 |
! |
|
2463 |
||
2464 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2465 |
"return a string generated by concatenating some of my elements |
5575 | 2466 |
(which must be strings or nil) and embedding cr characters in between. |
2467 |
Nil entries and empty strings are counted as empty lines." |
|
2468 |
||
2469 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2470 |
||
2471 |
" |
|
8874 | 2472 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2473 |
|
2474 |
" |
|
2475 |
||
2476 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2477 |
! |
|
2478 |
||
2479 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2480 |
"return part of myself as a string with embedded cr's. |
|
2481 |
My elements must be strings or nil. |
|
2482 |
If the argument compressTabs is true, leading spaces are converted |
|
2483 |
to tab-characters (8col tabs). |
|
2484 |
Nil entries and empty strings are taken as empty lines." |
|
2485 |
||
2486 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2487 |
! |
|
2488 |
||
2489 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2490 |
"return part of myself as a string with embedded cr's. |
|
2491 |
My elements must be strings or nil; nil entries and empty strings are |
|
2492 |
taken as empty lines. |
|
2493 |
If the argument compressTabs is true, leading spaces are converted |
|
2494 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2495 |
should be followed by a cr or not." |
|
2496 |
||
2497 |
^ self asStringWith:(Character cr) |
|
8874 | 2498 |
from:firstLine |
2499 |
to:lastLine |
|
2500 |
compressTabs:compressTabs |
|
2501 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2502 |
! |
2503 |
||
2504 |
decodeAsLiteralArray |
|
2505 |
"given a literalEncoding in the receiver, |
|
2506 |
create & return the corresponding object. |
|
2507 |
The inverse operation to #literalArrayEncoding." |
|
2508 |
||
9414 | 2509 |
|clsName cls| |
5575 | 2510 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2511 |
self size == 0 ifTrue:[ |
9358 | 2512 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2513 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2514 |
|
9414 | 2515 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2516 |
clsName isSymbol ifFalse:[ |
9358 | 2517 |
^ MissingClassInLiteralArrayErrorSignal |
2518 |
raiseRequestWith:clsName |
|
9414 | 2519 |
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
|
2520 |
]. |
9414 | 2521 |
cls := Smalltalk classNamed:clsName. |
5575 | 2522 |
cls isNil ifTrue:[ |
9414 | 2523 |
^ MissingClassInLiteralArrayErrorSignal |
2524 |
raiseRequestWith:clsName |
|
2525 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2526 |
]. |
9414 | 2527 |
|
5575 | 2528 |
^ cls decodeFromLiteralArray:self. |
2529 |
||
2530 |
" |
|
10476 | 2531 |
#(Array 10 20) decodeAsLiteralArray |
2532 |
#(10 20) literalArrayEncoding |
|
2533 |
" |
|
2534 |
||
2535 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2536 |
! |
2537 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2538 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2539 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2540 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2541 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2542 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2543 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2544 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2545 |
#( |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2546 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2547 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2548 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2549 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2550 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2551 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2552 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2553 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2554 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2555 |
|
8874 | 2556 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2557 |
"return part of myself as a string with embedded sepCharacters. |
2558 |
My elements must be strings or nil; nil entries and empty strings are |
|
2559 |
taken as empty lines." |
|
2560 |
||
8874 | 2561 |
^ self |
2562 |
from:firstLine |
|
2563 |
to:lastLine |
|
2564 |
asStringWith:sepCharacter |
|
2565 |
compressTabs:false |
|
2566 |
final:nil |
|
5575 | 2567 |
" |
2568 |
creating entries for searchpath: |
|
2569 |
||
8874 | 2570 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2571 |
||
2572 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2573 |
||
2574 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2575 |
" |
2576 |
||
2577 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2578 |
! |
|
2579 |
||
2580 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2581 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2582 |
My elements must be strings or nil; nil entries and empty strings are |
|
2583 |
taken as empty lines. |
|
2584 |
If the argument compressTabs is true, leading spaces are converted |
|
2585 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2586 |
character (if non-nil)." |
|
2587 |
||
2588 |
^ self |
|
8874 | 2589 |
from:firstLine |
2590 |
to:lastLine |
|
2591 |
asStringWith:sepCharacter |
|
2592 |
compressTabs:compressTabs |
|
2593 |
final:endCharacter |
|
2594 |
withEmphasis:true |
|
5575 | 2595 |
|
2596 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2597 |
! |
|
2598 |
||
2599 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2600 |
"return part of myself as a string or text with embedded sepCharacters |
|
2601 |
and followup endCharacter. |
|
2602 |
My elements must be strings or nil; nil entries and empty strings are |
|
2603 |
taken as empty lines. |
|
2604 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2605 |
If the argument compressTabs is true, leading spaces are converted |
|
2606 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2607 |
character (if non-nil). |
|
2608 |
The withEmphais argument controls if the returned string should preserve |
|
2609 |
any emphasis. If false, a plain string is returned. |
|
2610 |
This method is tuned for big collections, in not creating many |
|
2611 |
intermediate strings (has linear runtime). For very small collections |
|
2612 |
and small strings, it may be faster to use the comma , operation." |
|
2613 |
||
293 | 2614 |
|idx1 "{ Class:SmallInteger }" |
2615 |
idx2 "{ Class:SmallInteger }" |
|
2616 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2617 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2618 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2619 |
newString lineString spaces idx nTabs |
8099 | 2620 |
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
|
2621 |
thisLen anyTab| |
260 | 2622 |
|
2623 |
" |
|
2624 |
first accumulate the size of the string, to avoid |
|
2625 |
countless reallocations. If tabs are compressed, |
|
2626 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2627 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2628 |
" |
8099 | 2629 |
needEmphasis := false. |
2630 |
maxBitsPerCharacter := 8. |
|
1015 | 2631 |
|
260 | 2632 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2633 |
sepCharacter isNil ifTrue:[ |
8874 | 2634 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2635 |
] ifFalse:[ |
8874 | 2636 |
sepCharacter isCharacter ifTrue:[ |
2637 |
sepCnt := 1 |
|
2638 |
] ifFalse:[ |
|
2639 |
sepCnt := sepCharacter size |
|
2640 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2641 |
]. |
260 | 2642 |
|
293 | 2643 |
idx1 := firstLine. |
2644 |
idx2 := lastLine. |
|
2645 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2646 |
lineString := self at:lineIndex. |
2647 |
||
2648 |
lineString notNil ifTrue:[ |
|
2649 |
withEmphasis ifTrue:[ |
|
2650 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2651 |
needEmphasis := true |
|
2652 |
]. |
|
2653 |
]. |
|
2654 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2655 |
totalLength := totalLength + lineString size |
|
2656 |
]. |
|
2657 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2658 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2659 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2660 |
|
8099 | 2661 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2662 |
maxBitsPerCharacter > 16 ifTrue:[ |
2663 |
stringClass := Unicode32String. |
|
2664 |
] ifFalse:[ |
|
2665 |
stringClass := Unicode16String. |
|
2666 |
] |
|
8099 | 2667 |
] ifFalse:[ |
8874 | 2668 |
stringClass := String. |
8099 | 2669 |
]. |
2670 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2671 |
endCharacter notNil ifTrue:[ |
8874 | 2672 |
endCharacter isCharacter ifTrue:[ |
2673 |
totalLength := totalLength + 1 |
|
2674 |
] ifFalse:[ |
|
2675 |
totalLength := totalLength + endCharacter size |
|
2676 |
]. |
|
260 | 2677 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2678 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2679 |
|
260 | 2680 |
spaces := ' '. |
1016 | 2681 |
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
|
2682 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2683 |
needEmphasis ifTrue:[ |
8874 | 2684 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2685 |
]. |
260 | 2686 |
|
2687 |
" |
|
2688 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2689 |
Be careful with runArrays: |
8874 | 2690 |
replacing individual elements is VERY expensive. |
2691 |
Therefore, create a new runArray from scratch. |
|
260 | 2692 |
" |
2693 |
pos := 1. |
|
293 | 2694 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2695 |
lineString := self at:lineIndex. |
2696 |
thisLen := lineString size. |
|
2697 |
thisLen ~~ 0 ifTrue:[ |
|
2698 |
withEmphasis ifFalse:[ |
|
2699 |
lineString := lineString string. |
|
2700 |
]. |
|
2701 |
||
2702 |
(anyTab := compressTabs) ifTrue:[ |
|
2703 |
" |
|
2704 |
mhmh: could use withTabs from String-class here, |
|
2705 |
but we should avoid creating too many temporary strings |
|
2706 |
(especially, since this method is typically used when converting |
|
2707 |
big texts such as when saving in the filebrowser ...). |
|
2708 |
Therefore, we convert tabs inline here doing a direct replace |
|
2709 |
in newString." |
|
2710 |
||
2711 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2712 |
nTabs := (idx-1) // 8. |
|
2713 |
anyTab := (nTabs > 0) |
|
2714 |
]. |
|
2715 |
anyTab ifTrue:[ |
|
2716 |
"any tabs" |
|
2717 |
||
2718 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2719 |
||
2720 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2721 |
newRuns notNil ifTrue:[ |
|
2722 |
newRuns add:nil withOccurrences:nTabs |
|
2723 |
]. |
|
2724 |
pos := pos + nTabs. |
|
2725 |
||
2726 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2727 |
newRuns notNil ifTrue:[ |
|
2728 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2729 |
idx to:lineString size do:[:pos | |
|
2730 |
newRuns add:(lineString emphasisAt:pos) |
|
2731 |
] |
|
2732 |
] ifFalse:[ |
|
2733 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2734 |
] |
|
2735 |
]. |
|
2736 |
pos := pos + thisLen - (nTabs * 8). |
|
2737 |
] ifFalse:[ |
|
2738 |
newString replaceFrom:pos with:lineString. |
|
2739 |
||
2740 |
newRuns notNil ifTrue:[ |
|
2741 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2742 |
newRuns addAll:(lineString emphasis) |
|
2743 |
] ifFalse:[ |
|
2744 |
newRuns add:nil withOccurrences:lineString size |
|
2745 |
] |
|
2746 |
]. |
|
2747 |
pos := pos + thisLen. |
|
2748 |
]. |
|
2749 |
]. |
|
2750 |
||
2751 |
lineIndex ~~ lastLine ifTrue:[ |
|
2752 |
c := sepCharacter |
|
2753 |
] ifFalse:[ |
|
2754 |
c := endCharacter |
|
2755 |
]. |
|
2756 |
||
2757 |
c notNil ifTrue:[ |
|
2758 |
c isCharacter ifTrue:[ |
|
2759 |
newString at:pos put:c. |
|
2760 |
newRuns notNil ifTrue:[ |
|
2761 |
newRuns add:nil. |
|
2762 |
]. |
|
2763 |
pos := pos + 1 |
|
2764 |
] ifFalse:[ |
|
2765 |
newString replaceFrom:pos with:c. |
|
2766 |
newRuns notNil ifTrue:[ |
|
2767 |
newRuns add:nil withOccurrences:c size |
|
2768 |
]. |
|
2769 |
pos := pos + c size |
|
2770 |
] |
|
2771 |
]. |
|
260 | 2772 |
]. |
2773 |
||
2774 |
" |
|
2775 |
in case of tab compression, the result has to be |
|
2776 |
cut to size ... sorry |
|
2777 |
" |
|
2778 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2779 |
newString := newString copyTo:(pos - 1) |
260 | 2780 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2781 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2782 |
newRuns notNil ifTrue:[ |
8874 | 2783 |
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
|
2784 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2785 |
|
260 | 2786 |
^ newString |
2787 |
||
2788 |
" |
|
2789 |
creating entries for searchpath: |
|
2790 |
||
8874 | 2791 |
#('foo' 'bar' 'baz' '/foo/bar') |
2792 |
asStringWith:$: |
|
2793 |
from:1 to:4 |
|
2794 |
compressTabs:false |
|
2795 |
final:nil |
|
260 | 2796 |
|
2797 |
with trailing colon: |
|
2798 |
||
8874 | 2799 |
#('foo' 'bar' 'baz' '/foo/bar') |
2800 |
asStringWith:$: |
|
2801 |
from:1 to:4 |
|
2802 |
compressTabs:false |
|
2803 |
final:$: |
|
260 | 2804 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2805 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2806 |
|
8874 | 2807 |
#('foo' 'bar' 'baz') |
2808 |
asStringWith:nil |
|
2809 |
from:1 to:3 |
|
2810 |
compressTabs:false |
|
2811 |
final:nil |
|
260 | 2812 |
|
2813 |
creating a string from a collection of lines: |
|
2814 |
||
8874 | 2815 |
#('foo' 'bar' 'baz') |
2816 |
asStringWith:(Character cr) |
|
2817 |
from:1 to:3 |
|
2818 |
compressTabs:false |
|
2819 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2820 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2821 |
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
|
2822 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2823 |
(Array |
8874 | 2824 |
with:'foo' |
2825 |
with:('bar' asText allBold) |
|
2826 |
with:'baz' |
|
2827 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2828 |
) |
|
2829 |
asStringWith:(Character cr) |
|
2830 |
from:1 to:4 |
|
2831 |
compressTabs:false |
|
2832 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2833 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2834 |
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
|
2835 |
|
8874 | 2836 |
#('foo' 'bar' 'baz') |
2837 |
asStringWith:'__' |
|
2838 |
from:1 to:3 |
|
2839 |
compressTabs:false |
|
2840 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2841 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2842 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2843 |
|
8874 | 2844 |
#('foo' 'bar' 'baz') |
2845 |
asStringWith:'__' |
|
2846 |
from:1 to:3 |
|
2847 |
compressTabs:false |
|
2848 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2849 |
|
260 | 2850 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2851 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2852 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2853 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
11827 | 2854 |
! |
2855 |
||
2856 |
subCollections:aBlock |
|
2857 |
"Answser an ordered collection of ordered collections |
|
2858 |
where each subcollection is delimited by an element of the receiver |
|
2859 |
for which the given block evaluates to true." |
|
2860 |
||
2861 |
|str answer current e| |
|
2862 |
||
2863 |
str := self readStream. |
|
2864 |
answer := OrderedCollection new. |
|
2865 |
current := OrderedCollection new. |
|
2866 |
[ str atEnd ] whileFalse:[ |
|
2867 |
e := str next. |
|
2868 |
current add:e. |
|
2869 |
(aBlock value:e) ifTrue:[ |
|
2870 |
answer add:current. |
|
2871 |
current := OrderedCollection new |
|
2872 |
] |
|
2873 |
]. |
|
2874 |
current notEmpty ifTrue:[ |
|
2875 |
answer add:current |
|
2876 |
]. |
|
2877 |
^ answer |
|
2878 |
||
2879 |
" |
|
2880 |
#( 1 2 3 nil 4 5 6 nil 7 8 9 nil ) subCollections:[:el | el isNil]. |
|
2881 |
" |
|
260 | 2882 |
! ! |
2883 |
||
7253 | 2884 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2885 |
||
8874 | 2886 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2887 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2888 |
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
|
2889 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2890 |
however, physically, no copy is made). |
9291 | 2891 |
Warning: |
2892 |
The slice SHARES the memory for the element-data with the original, |
|
2893 |
this means that any modifications in the original are visible in the slice |
|
2894 |
and vice versa." |
|
7253 | 2895 |
|
2896 |
^ self |
|
9291 | 2897 |
from:startIndex |
2898 |
to:self size |
|
2899 |
by:1 |
|
8874 | 2900 |
|
2901 |
" |
|
2902 |
#(1 2 3 4 5 6 7) from:3 |
|
2903 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2904 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2905 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2906 |
" |
|
2907 |
! |
|
2908 |
||
2909 |
from:startIndex by:step |
|
9291 | 2910 |
"Create a ReindexedCollection from the receiver. |
2911 |
The new collection represents the elements starting at startIndex to the end. |
|
2912 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2913 |
however, physically, no copy is made). |
|
2914 |
Warning: |
|
2915 |
The slice SHARES the memory for the element-data with the original, |
|
2916 |
this means that any modifications in the original are visible in the slice |
|
2917 |
and vice versa." |
|
7253 | 2918 |
|
2919 |
^ self |
|
9291 | 2920 |
from:startIndex |
2921 |
to:(step > 1 |
|
2922 |
ifTrue: [self size] |
|
2923 |
ifFalse: [1]) |
|
2924 |
by:step |
|
8874 | 2925 |
|
2926 |
" |
|
2927 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2928 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2929 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2930 |
( #(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
|
2931 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2932 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2933 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2934 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2935 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2936 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2937 |
Warning: |
2938 |
The slice SHARES the memory for the element-data with the original, |
|
2939 |
this means that any modifications in the original are visible in the slice |
|
2940 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2941 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2942 |
^ self |
9291 | 2943 |
from:startIndex |
2944 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2945 |
|
2946 |
" |
|
2947 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2948 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2949 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2950 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2951 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2952 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2953 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2954 |
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
|
2955 |
slice := coll from:3 to:7. |
8874 | 2956 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2957 |
coll at:4 put:40. |
8874 | 2958 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2959 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2960 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2961 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2962 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2963 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2964 |
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
|
2965 |
slice := coll from:3 to:7. |
8874 | 2966 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2967 |
slice at:1 put:40. |
8874 | 2968 |
slice. |
2969 |
" |
|
2970 |
! |
|
2971 |
||
2972 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2973 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2974 |
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
|
2975 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2976 |
however, physically, no copy is made). |
9291 | 2977 |
Warning: |
2978 |
The slice SHARES the memory for the element-data with the original, |
|
2979 |
this means that any modifications in the original are visible in the slice |
|
2980 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2981 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2982 |
^ self |
9291 | 2983 |
from:startIndex |
2984 |
to:endIndex |
|
2985 |
by: (startIndex <= endIndex |
|
2986 |
ifTrue: [1] |
|
2987 |
ifFalse: [-1]) |
|
8874 | 2988 |
|
2989 |
" |
|
2990 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2991 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2992 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2993 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2994 |
" |
|
2995 |
! |
|
2996 |
||
2997 |
from:startIndex to:endIndex by:step |
|
9291 | 2998 |
"Create a ReindexedCollection from the receiver. |
2999 |
The new collection represents the receivers elements up to endIndex. |
|
3000 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
3001 |
however, physically, no copy is made). |
|
3002 |
Warning: |
|
3003 |
The slice SHARES the memory for the element-data with the original, |
|
3004 |
this means that any modifications in the original are visible in the slice |
|
3005 |
and vice versa." |
|
7253 | 3006 |
|
3007 |
^ ReindexedCollection |
|
9291 | 3008 |
on:self |
3009 |
from:startIndex |
|
3010 |
to:endIndex |
|
3011 |
by:step |
|
8874 | 3012 |
|
3013 |
" |
|
3014 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
3015 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
3016 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
3017 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
3018 |
" |
|
3019 |
! |
|
3020 |
||
3021 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3022 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3023 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3024 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3025 |
however, physically, no copy is made). |
9291 | 3026 |
Warning: |
3027 |
The slice SHARES the memory for the element-data with the original, |
|
3028 |
this means that any modifications in the original are visible in the slice |
|
3029 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3030 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3031 |
^ self |
9291 | 3032 |
from:1 |
3033 |
to:endIndex |
|
3034 |
by:1 |
|
8874 | 3035 |
|
3036 |
" |
|
3037 |
#(1 2 3 4 5 6 7) to:4 |
|
3038 |
( #(1 2 3 4 5 6 7) to:4) first |
|
3039 |
( #(1 2 3 4 5 6 7) to:4) last |
|
3040 |
( #(1 2 3 4 5 6 7) to:4) size |
|
3041 |
" |
|
3042 |
! |
|
3043 |
||
3044 |
to:endIndex by:step |
|
9291 | 3045 |
"Create a ReindexedCollection from the receiver. |
3046 |
The new collection represents the receivers elements up to endIndex. |
|
3047 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
3048 |
however, physically, no copy is made). |
|
3049 |
Warning: |
|
3050 |
The slice SHARES the memory for the element-data with the original, |
|
3051 |
this means that any modifications in the original are visible in the slice |
|
3052 |
and vice versa." |
|
7253 | 3053 |
|
3054 |
^ self |
|
9291 | 3055 |
from:(step > 0 |
3056 |
ifTrue: [1] |
|
3057 |
ifFalse: [self size]) |
|
3058 |
to:endIndex |
|
3059 |
by:step |
|
8874 | 3060 |
|
3061 |
" |
|
3062 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
3063 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
3064 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
3065 |
( #(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
|
3066 |
" |
7253 | 3067 |
! ! |
3068 |
||
1 | 3069 |
!SequenceableCollection methodsFor:'copying'! |
3070 |
||
9106 | 3071 |
++ aCollection |
3072 |
"return a new collection formed from concatenating the receiver with |
|
3073 |
the argument. The class of the new collection is determined by the |
|
3074 |
receivers class, so mixing classes is possible, if the second collections |
|
3075 |
elements can be stored into instances of the receivers class." |
|
3076 |
||
3077 |
^ self , aCollection |
|
3078 |
||
3079 |
" |
|
3080 |
#($a $b $c) ++ #(1 2 3) |
|
3081 |
#($a $b $c) ++ '123' |
|
3082 |
'abc' ++ '123' |
|
3083 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
3084 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3085 |
'abc' asArray ++ #(1 2 3 4 5) |
|
3086 |
" |
|
3087 |
! |
|
3088 |
||
1 | 3089 |
, aCollection |
3090 |
"return a new collection formed from concatenating the receiver with |
|
44 | 3091 |
the argument. The class of the new collection is determined by the |
3092 |
receivers class, so mixing classes is possible, if the second collections |
|
3093 |
elements can be stored into instances of the receivers class." |
|
1 | 3094 |
|
8874 | 3095 |
|newCollection |
1 | 3096 |
mySize "{ Class: SmallInteger }" |
3097 |
newSize "{ Class: SmallInteger }" |
|
3098 |
otherSize "{ Class: SmallInteger }" |
|
3099 |
dstIndex "{ Class: SmallInteger }"| |
|
3100 |
||
3101 |
mySize := self size. |
|
3102 |
otherSize := aCollection size. |
|
3103 |
newSize := mySize + otherSize. |
|
202 | 3104 |
|
252 | 3105 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 3106 |
|
3107 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
3108 |
dstIndex := mySize + 1. |
|
202 | 3109 |
aCollection isSequenceable ifTrue:[ |
8874 | 3110 |
" |
3111 |
yes, aCollection has indexed elements |
|
3112 |
" |
|
3113 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 3114 |
] ifFalse:[ |
8874 | 3115 |
" |
3116 |
no, enumerate aCollection |
|
3117 |
" |
|
3118 |
aCollection do:[:element | |
|
3119 |
newCollection at:dstIndex put:element. |
|
3120 |
dstIndex := dstIndex + 1 |
|
3121 |
] |
|
1 | 3122 |
]. |
3123 |
^ newCollection |
|
44 | 3124 |
|
95 | 3125 |
" |
3126 |
#($a $b $c) , #(1 2 3) |
|
3127 |
#($a $b $c) , '123' |
|
3128 |
'abc' , '123' |
|
3129 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
3130 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3131 |
'abc' asArray , #(1 2 3 4 5) |
|
3132 |
" |
|
1 | 3133 |
! |
3134 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3135 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3136 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3137 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3138 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3139 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3140 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3141 |
" |
8874 | 3142 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3143 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3144 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3145 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3146 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3147 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3148 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3149 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3150 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3151 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3152 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3153 |
" |
8874 | 3154 |
#($a $b $c $d $e $f $g) copyButLast:2 |
3155 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3156 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3157 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3158 |
|
602 | 3159 |
copyFirst:count |
3160 |
"return a new collection consisting of the receivers first count |
|
3161 |
elements - this is just a rename of copyTo: - for compatibility." |
|
3162 |
||
3163 |
^ self copyFrom:1 to:count |
|
3164 |
||
3165 |
" |
|
3166 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
3167 |
'1234567890' copyFirst:4 |
|
3168 |
" |
|
3169 |
! |
|
3170 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3171 |
copyFrom:startIndex |
9234 | 3172 |
"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
|
3173 |
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
|
3174 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3175 |
^ self copyFrom:startIndex to:(self size) |
602 | 3176 |
|
3177 |
" |
|
3178 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
3179 |
'1234567890' copyFrom:2 |
|
8874 | 3180 |
(10 to:19) copyFrom:5 |
602 | 3181 |
" |
3182 |
! |
|
3183 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3184 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3185 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3186 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3187 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3188 |
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
|
3189 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3190 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3191 |
^ self |
8874 | 3192 |
copyFrom:startIndex |
3193 |
to:(startIndex + numberOfElements - 1) |
|
3194 |
||
3195 |
" |
|
3196 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3197 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3198 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3199 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3200 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3201 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3202 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3203 |
copyFrom:startIndex to:stopIndex |
602 | 3204 |
"return a new collection consisting of receivers elements |
4403 | 3205 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3206 |
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
|
3207 |
Raise an error, if stopIndex is out of range." |
602 | 3208 |
|
3209 |
|newCollection newSize| |
|
3210 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3211 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3212 |
newSize < 0 ifTrue:[ |
8874 | 3213 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3214 |
]. |
602 | 3215 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3216 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 3217 |
^ newCollection |
3218 |
||
3219 |
" |
|
3220 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3221 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3222 |
(1 to:10) copyFrom:2 to:5 |
3223 |
||
3224 |
'1234567890' copyFrom:2 to:15 |
|
3225 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 3226 |
" |
3227 |
! |
|
3228 |
||
3229 |
copyLast:count |
|
3230 |
"return a new collection consisting of the receivers last count |
|
3231 |
elements." |
|
3232 |
||
3233 |
|sz| |
|
3234 |
||
3235 |
sz := self size. |
|
3236 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3237 |
||
3238 |
" |
|
3239 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3240 |
'1234567890' copyLast:4 |
|
3241 |
" |
|
3242 |
! |
|
3243 |
||
11841 | 3244 |
copyMapping:map |
3245 |
"return a copy, where elements all replaced via the given mapping. |
|
3246 |
If the element is present as key in map, it is replaced by a corresponding value from the map. |
|
3247 |
Otherwise, it is left unchanged." |
|
3248 |
||
3249 |
|s| |
|
3250 |
||
3251 |
s := WriteStream on:self species new. |
|
3252 |
self do:[:el | |
|
3253 |
s nextPut:(map at:el ifAbsent:el) |
|
3254 |
]. |
|
3255 |
^ s contents |
|
3256 |
||
3257 |
" |
|
3258 |
|map| |
|
3259 |
||
3260 |
map := Dictionary new |
|
3261 |
at:1 put:'one'; |
|
3262 |
at:2 put:'two'; |
|
3263 |
yourself. |
|
3264 |
||
3265 |
#(1 2 3 4 1 2 3 4) copyMapping:map |
|
3266 |
" |
|
3267 |
! |
|
3268 |
||
3269 |
copyMappingFrom:inMap to:outMap |
|
3270 |
"return a copy, where elements all replaced via the given mapping. |
|
3271 |
If the element is present in inmap, it is replaced by a corresponding value from outmap. |
|
3272 |
Otherwise, it is left unchanged." |
|
3273 |
||
3274 |
|d| |
|
3275 |
||
3276 |
d := Dictionary new. |
|
3277 |
inMap keysAndValuesDo:[:k :i | d at:i put:(outMap at:k)]. |
|
3278 |
^ self copyMapping:d |
|
3279 |
||
3280 |
" |
|
3281 |
#(1 2 3 4 1 2 3 4) copyMappingFrom:#(1 2) to:#('one' 'two') |
|
3282 |
'hello' copyMappingFrom:'eo' to:'**' |
|
3283 |
" |
|
3284 |
! |
|
3285 |
||
3653 | 3286 |
copyReplaceAll:oldElement with:newElement |
3287 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3288 |
have been replaced by newElement." |
|
3289 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3290 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3291 |
^ self copy replaceAll:oldElement with:newElement |
3292 |
||
3293 |
" |
|
8874 | 3294 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3295 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3296 |
" |
3297 |
||
3298 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3299 |
! |
|
3300 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3301 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3302 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3303 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3304 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3305 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3306 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3307 |
|
5489 | 3308 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3309 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3310 |
"/ '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
|
3311 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3312 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3313 |
self do:[:el | |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3314 |
el = oldObject ifTrue:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3315 |
s nextPutAll:aCollection |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3316 |
] ifFalse:[ |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3317 |
s nextPut:el |
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3318 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3319 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3320 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3321 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3322 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3323 |
args: oldObject : <object> |
11840
160aca0ef697
changed #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
11827
diff
changeset
|
3324 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3325 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3326 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3327 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3328 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3329 |
" |
8874 | 3330 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3331 |
#(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
|
3332 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3333 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3334 |
|
602 | 3335 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3336 |
"return a copy of the receiver, where the elements from startIndex to |
|
3337 |
endIndex have been replaced by the elements of aCollection" |
|
3338 |
||
3339 |
|newColl sz replSize| |
|
3340 |
||
3341 |
replSize := aCollection size. |
|
3342 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
3343 |
newColl := self copyEmptyAndGrow:sz. |
|
3344 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3345 |
newColl replaceFrom:startIndex with:aCollection. |
|
3346 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3347 |
^ newColl |
|
3348 |
||
3349 |
" |
|
3350 |
#(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 | 3351 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 3352 |
" |
3353 |
! |
|
3354 |
||
3355 |
copyReplaceFrom:startIndex with:aCollection |
|
3356 |
"return a copy of the receiver, where the elements from startIndex to |
|
3357 |
the end have been replaced by the elements of aCollection" |
|
3358 |
||
3359 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
3360 |
||
3361 |
" |
|
8874 | 3362 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
3363 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 3364 |
" |
3365 |
! |
|
3366 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3367 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3368 |
"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
|
3369 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3370 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3371 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3372 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3373 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3374 |
" |
8874 | 3375 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
3376 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3377 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3378 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3379 |
|
602 | 3380 |
copyThrough:element |
3381 |
"return a new collection consisting of the receiver elements |
|
8874 | 3382 |
up-to (AND including) the first occurrence of element." |
602 | 3383 |
|
3384 |
|idx| |
|
3385 |
||
3386 |
idx := self indexOf:element. |
|
3387 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
3388 |
^ self copyFrom:1 to:idx |
|
3389 |
||
3390 |
" |
|
3391 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
3392 |
'1234567890' copyThrough:$5 |
|
3393 |
'1234567890' copyThrough:$a |
|
3394 |
'1234567890' copyThrough:$1 |
|
3395 |
" |
|
3396 |
! |
|
3397 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3398 |
copyTo:stopIndex |
9234 | 3399 |
"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
|
3400 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3401 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3402 |
^ self copyFrom:1 to:stopIndex |
602 | 3403 |
|
3404 |
" |
|
3405 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
3406 |
'1234567890' copyTo:4 |
|
3407 |
" |
|
3408 |
! |
|
3409 |
||
5526 | 3410 |
copyToMax:stop |
3411 |
"return a new collection consisting of receivers elements |
|
8874 | 3412 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 3413 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
3414 |
||
3415 |
^ self copyFrom:1 to:(self size min:stop) |
|
3416 |
||
3417 |
" |
|
3418 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
3419 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
3420 |
" |
|
3421 |
! |
|
3422 |
||
11844 | 3423 |
copyTransliterating:oldElements to:newElements |
3424 |
"return a copy, where elements all transliterated via the given mapping. |
|
3425 |
The transiteraion works like perl's tr operator. |
|
3426 |
||
3427 |
Transliterates all occurrences of the characters found in the search list with the |
|
11880 | 3428 |
corresponding character in the replacement list. |
11844 | 3429 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
3430 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3431 |
||
3432 |
Note that tr does not do other character escapes such as \n. |
|
3433 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3434 |
||
3435 |
Note also that the whole range idea is rather unportable between character sets, |
|
3436 |
and even within character sets they may cause results you probably didn't expect. |
|
3437 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3438 |
case (a-e, A-E), or digits (0-4). |
|
3439 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3440 |
" |
|
3441 |
||
3442 |
^ self |
|
3443 |
copyTransliterating:oldElements to:newElements |
|
3444 |
complement:false squashDuplicates:false |
|
3445 |
||
3446 |
" |
|
3447 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
|
3448 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3449 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3450 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3451 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11880 | 3452 |
|
3453 |
can also be used to remove character: |
|
3454 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'' |
|
3455 |
||
3456 |
'abcdefg |
|
3457 |
hijkl1 |
|
3458 |
234567890' copyTransliterating:(Array with:Character cr) to:'' |
|
3459 |
||
11844 | 3460 |
" |
3461 |
! |
|
3462 |
||
3463 |
copyTransliterating:oldElements to:newElements |
|
3464 |
complement:complement squashDuplicates:squashDuplicates |
|
3465 |
||
3466 |
"return a copy, where elements all transliterated via the given mapping. |
|
3467 |
The transiteraion works like perl's tr operator. |
|
3468 |
||
3469 |
Transliterates all occurrences of the characters found in the search list with the |
|
3470 |
corresponding character in the replacement list. It returns the number of characters replaced |
|
3471 |
or deleted. |
|
3472 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
|
3473 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
|
3474 |
||
3475 |
Note that tr does not do other character escapes such as \n. |
|
3476 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
|
3477 |
||
3478 |
Note also that the whole range idea is rather unportable between character sets, |
|
3479 |
and even within character sets they may cause results you probably didn't expect. |
|
3480 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
|
3481 |
case (a-e, A-E), or digits (0-4). |
|
3482 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
|
3483 |
||
3484 |
If complement is true, non-matching chars are affected. |
|
3485 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
|
3486 |
" |
|
3487 |
||
11875 | 3488 |
|map s1 s2 range1 range2 c1 c2 outStream prevXlated last inc| |
11844 | 3489 |
|
3490 |
map := Dictionary new. |
|
3491 |
||
3492 |
s1 := oldElements readStream. |
|
3493 |
s2 := newElements readStream. |
|
3494 |
range1 := range2 := nil. |
|
3495 |
[ |
|
3496 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
|
3497 |
range1 notNil or:[ s1 atEnd not ] |
|
3498 |
] whileTrue:[ |
|
3499 |
range1 notNil ifTrue:[ |
|
3500 |
c1 := range1 next. |
|
3501 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3502 |
] ifFalse:[ |
|
3503 |
c1 := s1 next. |
|
3504 |
s1 peek == $\ ifTrue:[ |
|
3505 |
s1 next. |
|
3506 |
] ifFalse:[ |
|
3507 |
s1 peek == $- ifTrue:[ |
|
3508 |
s1 next. |
|
11875 | 3509 |
last := s1 next. |
3510 |
inc := c1 < last ifTrue:[1] ifFalse:[-1]. |
|
3511 |
range1 := (c1 to:last by:inc) readStream. |
|
11844 | 3512 |
c1 := range1 next. |
3513 |
range1 atEnd ifTrue:[range1 := nil]. |
|
3514 |
]. |
|
3515 |
]. |
|
3516 |
]. |
|
3517 |
range2 notNil ifTrue:[ |
|
3518 |
c2 := range2 next. |
|
3519 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3520 |
] ifFalse:[ |
|
3521 |
s2 atEnd ifTrue:[ |
|
3522 |
"/ keep c2 |
|
3523 |
] ifFalse:[ |
|
3524 |
c2 := s2 next. |
|
3525 |
s2 peek == $\ ifTrue:[ |
|
3526 |
s1 next. |
|
3527 |
] ifFalse:[ |
|
3528 |
s2 peek == $- ifTrue:[ |
|
3529 |
s2 next. |
|
11875 | 3530 |
last := s2 next. |
3531 |
inc := c2 < last ifTrue:[1] ifFalse:[-1]. |
|
3532 |
range2 := (c2 to:last by:inc) readStream. |
|
11844 | 3533 |
c2 := range2 next. |
3534 |
range2 atEnd ifTrue:[range2 := nil]. |
|
3535 |
]. |
|
3536 |
]. |
|
3537 |
]. |
|
3538 |
]. |
|
3539 |
map at:c1 put:c2 |
|
3540 |
]. |
|
3541 |
||
3542 |
outStream := WriteStream on:(self species new). |
|
3543 |
self do:[:el | |
|
3544 |
|xLated out| |
|
3545 |
||
3546 |
complement ifTrue:[ |
|
3547 |
(map includesKey:el) ifTrue:[ |
|
3548 |
xLated := out := el. |
|
3549 |
] ifFalse:[ |
|
3550 |
out := newElements at:1 ifAbsent:nil. |
|
3551 |
]. |
|
3552 |
] ifFalse:[ |
|
3553 |
(map includesKey:el) ifTrue:[ |
|
3554 |
xLated := out := map at:el. |
|
3555 |
] ifFalse:[ |
|
3556 |
out := el. |
|
3557 |
]. |
|
3558 |
]. |
|
3559 |
out notNil ifTrue:[ |
|
3560 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
|
3561 |
"/ ignore |
|
3562 |
] ifFalse:[ |
|
3563 |
outStream nextPut:out. |
|
3564 |
]. |
|
3565 |
prevXlated := out. |
|
3566 |
]. |
|
3567 |
]. |
|
3568 |
^ outStream contents |
|
3569 |
||
3570 |
" |
|
11875 | 3571 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
11844 | 3572 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
3573 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
|
3574 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
|
3575 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
|
3576 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
|
11875 | 3577 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
3578 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
|
3579 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
|
11844 | 3580 |
|
3581 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3582 |
complement:false squashDuplicates:false |
|
3583 |
||
3584 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
|
3585 |
complement:false squashDuplicates:true |
|
3586 |
||
3587 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3588 |
complement:false squashDuplicates:false |
|
3589 |
||
3590 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
|
3591 |
complement:true squashDuplicates:false |
|
3592 |
||
3593 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3594 |
complement:true squashDuplicates:false |
|
3595 |
||
3596 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
|
3597 |
complement:false squashDuplicates:false |
|
3598 |
||
3599 |
- delete all a-zA-Z |
|
3600 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3601 |
complement:false squashDuplicates:false |
|
3602 |
||
3603 |
- delete all except a-zA-Z |
|
3604 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
|
3605 |
complement:true squashDuplicates:false |
|
3606 |
||
3607 |
- squash multiple spaces |
|
3608 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
|
3609 |
complement:false squashDuplicates:true |
|
3610 |
||
3611 |
- squash multiple digits |
|
3612 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
|
3613 |
complement:false squashDuplicates:true |
|
3614 |
" |
|
3615 |
! |
|
3616 |
||
13697 | 3617 |
copyUpThrough:element |
3618 |
"return a new collection consisting of the receiver elements |
|
3619 |
up-to (and including) the first occurrence of element; |
|
3620 |
or to the end, if element is not included" |
|
3621 |
||
3622 |
|idx| |
|
3623 |
||
3624 |
idx := self indexOf:element. |
|
3625 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3626 |
^ self copyFrom:1 to:idx |
|
3627 |
||
3628 |
" |
|
3629 |
#($a $b $c $d $e $f $g) copyUpThrough:$d |
|
3630 |
'1234567890' copyUpThrough:$5 |
|
3631 |
'1234567890' copyUpThrough:$a |
|
3632 |
'1234567890' copyUpThrough:$1 |
|
3633 |
'123456789' copyUpThrough:$0 |
|
3634 |
" |
|
3635 |
||
3636 |
"Created: / 14-09-2011 / 16:28:16 / cg" |
|
3637 |
! |
|
3638 |
||
602 | 3639 |
copyUpTo:element |
3640 |
"return a new collection consisting of the receiver elements |
|
8874 | 3641 |
up-to (but excluding) the first occurrence of element; |
602 | 3642 |
or to the end, if element is not included" |
3643 |
||
3644 |
|idx| |
|
3645 |
||
3646 |
idx := self indexOf:element. |
|
3647 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3648 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
3649 |
^ self copyFrom:1 to:(idx-1) |
|
3650 |
||
3651 |
" |
|
3652 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
3653 |
'1234567890' copyUpTo:$5 |
|
3654 |
'1234567890' copyUpTo:$a |
|
3655 |
'1234567890' copyUpTo:$1 |
|
3656 |
'123456789' copyUpTo:$0 |
|
3657 |
" |
|
3658 |
! |
|
3659 |
||
1 | 3660 |
copyWith:newElement |
233 | 3661 |
"return a new collection containing the receivers elements |
8874 | 3662 |
and the single new element, newElement. |
233 | 3663 |
This is different from concatentation, which expects another collection |
3664 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3665 |
|
3666 |
|newCollection mySize newSize| |
|
3667 |
||
3668 |
mySize := self size. |
|
3669 |
newSize := mySize + 1. |
|
252 | 3670 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3671 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3672 |
newCollection at:newSize put:newElement. |
|
3673 |
^newCollection |
|
44 | 3674 |
|
95 | 3675 |
" |
3676 |
#(1 2 3 4 5) copyWith:$a |
|
3677 |
'abcdefg' copyWith:$h |
|
3678 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3679 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3680 |
" |
|
1 | 3681 |
! |
3682 |
||
11855 | 3683 |
copyWithFirst:newFirstElement |
3684 |
"return a new collection containing the receivers elements |
|
3685 |
and the single new element, newElement up front. |
|
3686 |
This is different from concatentation, which expects two collections |
|
3687 |
as argument, but equivalent to copy-and-addFirst." |
|
3688 |
||
3689 |
|newCollection mySize newSize| |
|
3690 |
||
3691 |
mySize := self size. |
|
3692 |
newSize := mySize + 1. |
|
3693 |
newCollection := self copyEmptyAndGrow:newSize. |
|
3694 |
newCollection replaceFrom:2 to:mySize+1 with:self startingAt:1. |
|
3695 |
newCollection at:1 put:newFirstElement. |
|
3696 |
^newCollection |
|
3697 |
||
3698 |
" |
|
3699 |
#(1 2 3 4 5) copyWithFirst:$a |
|
3700 |
'abcdefg' copyWithFirst:$h |
|
3701 |
'abcdefg' copyWithFirst:'123' -- will fail: string cannot be stored into string |
|
3702 |
'abcdefg' copyWithFirst:1 -- will fail: integer cannot be stored into string |
|
3703 |
" |
|
3704 |
! |
|
3705 |
||
61 | 3706 |
copyWithout:elementToSkip |
3707 |
"return a new collection consisting of a copy of the receiver, with |
|
3708 |
ALL elements equal to elementToSkip are left out. |
|
3709 |
No error is reported, if elementToSkip is not in the collection." |
|
3710 |
||
293 | 3711 |
|n "{ Class: SmallInteger }" |
3712 |
sz "{ Class: SmallInteger }" |
|
3713 |
srcIndex "{ Class: SmallInteger }" |
|
3714 |
dstIndex "{ Class: SmallInteger }" |
|
3715 |
skipIndex "{ Class: SmallInteger }" |
|
3716 |
copy l| |
|
61 | 3717 |
|
8874 | 3718 |
"the code below may look like overkill, |
61 | 3719 |
however, for big collections its better to move data |
3720 |
around in big chunks" |
|
3721 |
||
3722 |
n := self occurrencesOf:elementToSkip. |
|
3723 |
n == 0 ifTrue:[^ self copy]. |
|
3724 |
||
3725 |
sz := self size. |
|
252 | 3726 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3727 |
|
3728 |
srcIndex := 1. |
|
3729 |
dstIndex := 1. |
|
3730 |
||
3731 |
n timesRepeat:[ |
|
8874 | 3732 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3733 |
l := skipIndex - srcIndex. |
|
3734 |
l ~~ 0 ifTrue:[ |
|
3735 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3736 |
with:self startingAt:srcIndex. |
|
3737 |
dstIndex := dstIndex + l |
|
3738 |
]. |
|
3739 |
srcIndex := skipIndex + 1 |
|
61 | 3740 |
]. |
3741 |
l := sz - srcIndex. |
|
3742 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3743 |
with:self startingAt:srcIndex. |
61 | 3744 |
^ copy |
3745 |
||
95 | 3746 |
" |
3747 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3748 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3749 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3750 |
'abcdefghi' copyWithout:$h |
3751 |
'abcdefg' copyWithout:$h |
|
95 | 3752 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3753 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3754 |
#(90 80 70 60 50) copyWithout:70 |
|
3755 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3756 |
" |
|
61 | 3757 |
! |
3758 |
||
602 | 3759 |
copyWithoutFirst:elementToSkip |
3760 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3761 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3762 |
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
|
3763 |
Do not confuse this with copyButFirst:" |
602 | 3764 |
|
3765 |
|copy skipIndex sz| |
|
3766 |
||
3767 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3768 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3769 |
||
3770 |
sz := self size - 1. |
|
3771 |
copy := self copyEmptyAndGrow:sz. |
|
3772 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3773 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3774 |
^ copy |
|
3775 |
||
3776 |
" |
|
3777 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3778 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3779 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3780 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3781 |
" |
|
3782 |
! |
|
3783 |
||
8673 | 3784 |
copyWithoutIdentical:elementToSkip |
3785 |
"return a new collection consisting of a copy of the receiver, with |
|
3786 |
ALL elements identical to elementToSkip are left out. |
|
3787 |
No error is reported, if elementToSkip is not in the collection." |
|
3788 |
||
3789 |
|n "{ Class: SmallInteger }" |
|
3790 |
sz "{ Class: SmallInteger }" |
|
3791 |
srcIndex "{ Class: SmallInteger }" |
|
3792 |
dstIndex "{ Class: SmallInteger }" |
|
3793 |
skipIndex "{ Class: SmallInteger }" |
|
3794 |
copy l| |
|
3795 |
||
8874 | 3796 |
"the code below may look like overkill, |
8673 | 3797 |
however, for big collections its better to move data |
3798 |
around in big chunks" |
|
3799 |
||
3800 |
n := self count:[:el | el == elementToSkip]. |
|
3801 |
n == 0 ifTrue:[^ self copy]. |
|
3802 |
||
3803 |
sz := self size. |
|
3804 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3805 |
||
3806 |
srcIndex := 1. |
|
3807 |
dstIndex := 1. |
|
3808 |
||
3809 |
n timesRepeat:[ |
|
8874 | 3810 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3811 |
l := skipIndex - srcIndex. |
|
3812 |
l ~~ 0 ifTrue:[ |
|
3813 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3814 |
with:self startingAt:srcIndex. |
|
3815 |
dstIndex := dstIndex + l |
|
3816 |
]. |
|
3817 |
srcIndex := skipIndex + 1 |
|
8673 | 3818 |
]. |
3819 |
l := sz - srcIndex. |
|
3820 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3821 |
with:self startingAt:srcIndex. |
8673 | 3822 |
^ copy |
3823 |
||
3824 |
" |
|
8874 | 3825 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3826 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3827 |
" |
3828 |
! |
|
3829 |
||
61 | 3830 |
copyWithoutIndex:omitIndex |
3831 |
"return a new collection consisting of receivers elements |
|
3832 |
without the argument stored at omitIndex" |
|
3833 |
||
3834 |
|copy sz| |
|
3835 |
||
202 | 3836 |
sz := self size - 1. |
252 | 3837 |
copy := self copyEmptyAndGrow:sz. |
293 | 3838 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3839 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3840 |
^ copy |
3841 |
||
95 | 3842 |
" |
3843 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3844 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3845 |
" |
|
1 | 3846 |
! |
3847 |
||
202 | 3848 |
copyWithoutLast:count |
3849 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3850 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3851 |
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
|
3852 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3853 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3854 |
|
9250 | 3855 |
<resource:#obsolete> |
3856 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3857 |
^ self copyTo:(self size - count) |
202 | 3858 |
|
3859 |
" |
|
8874 | 3860 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3861 |
'1234567890' copyWithoutLast:4 |
|
202 | 3862 |
" |
5231 | 3863 |
! |
3864 |
||
12750
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3865 |
repeatedConcatenation:nTimes |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3866 |
"return a new collection consisting of nTimes the concatenation of the receiver" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3867 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3868 |
|newColl idx| |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3869 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3870 |
newColl := self species new:(self size * nTimes). |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3871 |
idx := 1. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3872 |
nTimes timesRepeat:[ |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3873 |
newColl replaceFrom:idx with:self startingAt:1. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3874 |
idx := idx + self size. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3875 |
]. |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3876 |
^ newColl |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3877 |
|
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3878 |
" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3879 |
'hello' repeatedConcatenation:5 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3880 |
#[1 2 3] repeatedConcatenation:3 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3881 |
#(1 2 3) repeatedConcatenation:1 |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3882 |
" |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3883 |
! |
a2086ea63bfb
added: #repeatedConcatenation:
Claus Gittinger <cg@exept.de>
parents:
12726
diff
changeset
|
3884 |
|
5231 | 3885 |
restAfter:anElement |
8366 | 3886 |
"return a new collection consisting of the receivers elements after |
5231 | 3887 |
(but excluding) anElement. |
8366 | 3888 |
If anElement is not in the receiver, the returned collection |
3889 |
will be empty. |
|
3890 |
See also #upTo:." |
|
5231 | 3891 |
|
3892 |
|pos| |
|
3893 |
||
3894 |
pos := self indexOf:anElement. |
|
8366 | 3895 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3896 |
||
5231 | 3897 |
^ self copyFrom:(pos + 1) |
3898 |
||
3899 |
" |
|
8874 | 3900 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3901 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3902 |
'hello world' upTo:Character space |
|
3903 |
'hello world' restAfter:Character space |
|
3904 |
'1234.5678' upTo:$. |
|
3905 |
'1234.5678' restAfter:$. |
|
8366 | 3906 |
" |
5231 | 3907 |
! |
3908 |
||
12187 | 3909 |
trimForWhich:aCheckBlock |
3910 |
"return a copy of myself without leading and trailing elements for which aCheckBlock returns true. |
|
3911 |
Normally, this is mostly used with string receivers." |
|
3912 |
||
3913 |
|startIndex "{ Class: SmallInteger }" |
|
3914 |
endIndex "{ Class: SmallInteger }" |
|
3915 |
sz| |
|
3916 |
||
3917 |
sz := self size. |
|
3918 |
startIndex := 1. |
|
3919 |
endIndex := sz. |
|
3920 |
||
12320 | 3921 |
[(startIndex <= endIndex) and:[aCheckBlock value:(self at:startIndex)]] whileTrue:[ |
12187 | 3922 |
startIndex := startIndex + 1 |
3923 |
]. |
|
3924 |
[(endIndex > 1) and:[aCheckBlock value:(self at:endIndex)]] whileTrue:[ |
|
3925 |
endIndex := endIndex - 1 |
|
3926 |
]. |
|
3927 |
startIndex > endIndex ifTrue:[ |
|
3928 |
^ self class new |
|
3929 |
]. |
|
3930 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
|
3931 |
^ self |
|
3932 |
]. |
|
3933 |
^ self copyFrom:startIndex to:endIndex |
|
3934 |
||
3935 |
" |
|
12320 | 3936 |
' foo ' trimForWhich:[:ch | ch isSeparator] |
3937 |
' ' trimForWhich:[:ch | ch isSeparator] |
|
12187 | 3938 |
#(0 0 0 1 2 3 0 0 0) trimForWhich:[:e | e = 0] |
3939 |
" |
|
3940 |
! |
|
3941 |
||
5231 | 3942 |
upTo:anElement |
3943 |
"return a new collection consisting of the receivers elements upTo |
|
3944 |
(but excluding) anElement. |
|
3945 |
If anElement is not in the receiver, the returned collection |
|
3946 |
will consist of all elements of the receiver. |
|
9307 | 3947 |
See also #restAfter: , #copyFrom:index." |
5231 | 3948 |
|
3949 |
|pos| |
|
3950 |
||
3951 |
pos := self indexOf:anElement. |
|
3952 |
pos == 0 ifTrue:[^ self copy]. |
|
3953 |
||
3954 |
^ self copyFrom:1 to:(pos - 1) |
|
3955 |
||
3956 |
" |
|
8874 | 3957 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3958 |
'hello world' upTo:Character space |
|
3959 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3960 |
'1234.5678' upTo:$. |
|
3961 |
'1234' upTo:$. |
|
5231 | 3962 |
|
3963 |
raises an error: |
|
3964 |
||
8874 | 3965 |
(Dictionary new |
9307 | 3966 |
at:#foo put:'foo'; |
3967 |
at:#bar put:'bar'; |
|
3968 |
at:#baz put:'baz'; |
|
3969 |
yourself) upTo:#bar |
|
8874 | 3970 |
|
5231 | 3971 |
" |
3972 |
||
3973 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3974 |
! |
|
3975 |
||
3976 |
upTo:anElement count:count |
|
3977 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3978 |
(but excluding) count found anElements, i.e. the first found count-1 |
3979 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3980 |
procedure is done from the end of the collection backwards. |
3981 |
If anElement is not in the receiver, the returned collection |
|
3982 |
will consist of all elements of the receiver" |
|
3983 |
||
3984 |
|startPos endPos pos found| |
|
3985 |
||
3986 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3987 |
^ self upTo:anElement |
5231 | 3988 |
]. |
3989 |
||
3990 |
startPos := 1. |
|
3991 |
endPos := self size. |
|
3992 |
found := 0. |
|
3993 |
||
3994 |
count > 1 ifTrue:[ |
|
8874 | 3995 |
pos := 0. |
3996 |
[pos < self size and: [found < count]] |
|
3997 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3998 |
found == count ifTrue:[ |
|
3999 |
endPos := pos - 1 |
|
4000 |
] |
|
4001 |
]. |
|
5231 | 4002 |
|
4003 |
count < 0 ifTrue:[ |
|
8874 | 4004 |
pos := self size + 1. |
4005 |
[pos > 1 and: [found < count abs]] |
|
4006 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
4007 |
found == count abs ifTrue:[ |
|
4008 |
startPos := pos + 1 |
|
4009 |
] |
|
4010 |
]. |
|
5231 | 4011 |
|
4012 |
^ self copyFrom: startPos to: endPos |
|
4013 |
||
4014 |
" |
|
8874 | 4015 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 4016 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
4017 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
4018 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
4019 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
4020 |
" |
|
5563 | 4021 |
! |
4022 |
||
4023 |
upToAny:aCollectionOfObjects |
|
4024 |
"return a new collection consisting of the receivers elements upTo |
|
4025 |
(but excluding) any in aCollectionOfObjects. |
|
4026 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
4027 |
will consist of all elements of the receiver. |
|
4028 |
See also #upTo:" |
|
4029 |
||
4030 |
|pos| |
|
4031 |
||
4032 |
pos := self indexOfAny:aCollectionOfObjects. |
|
4033 |
pos == 0 ifTrue:[^ self copy]. |
|
4034 |
||
4035 |
^ self copyFrom:1 to:(pos - 1) |
|
4036 |
||
4037 |
" |
|
8874 | 4038 |
'hello world' upToAny:#($/ $:) |
4039 |
'hello:world' upToAny:#($/ $:) |
|
4040 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 4041 |
" |
12187 | 4042 |
! |
4043 |
||
4044 |
withoutLeadingForWhich:aCheckBlock |
|
4045 |
"return a copy of myself without leading eleents for which aCheckBlock returns true. |
|
4046 |
Returns an empty collection, if the receiver consist only of matching elements. |
|
4047 |
Normally, this is mostly used with string receivers." |
|
4048 |
||
4049 |
|index sz| |
|
4050 |
||
4051 |
sz := self size. |
|
4052 |
index := 1. |
|
4053 |
[index <= sz] whileTrue:[ |
|
4054 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
4055 |
index == 1 ifTrue:[^ self]. |
|
4056 |
^ self copyFrom:index |
|
4057 |
]. |
|
4058 |
index := index + 1 |
|
4059 |
]. |
|
4060 |
^ self class new |
|
4061 |
||
4062 |
" |
|
4063 |
'****foo****' withoutLeadingForWhich:[:ch | ch isLetter not] |
|
4064 |
#( 0 0 0 1 2 3 4 0 0 0) withoutLeadingForWhich:[:e | e = 0] |
|
4065 |
" |
|
4066 |
! |
|
4067 |
||
4068 |
withoutTrailingForWhich:aCheckBlock |
|
4069 |
"return a copy of myself without trailing characters for which aCheckBlock returns true. |
|
4070 |
Returns an empty collection, if the receiver consist only of matching chars." |
|
4071 |
||
4072 |
|index sz| |
|
4073 |
||
4074 |
index := sz := self size. |
|
4075 |
[index ~~ 0] whileTrue:[ |
|
4076 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
|
4077 |
index == sz ifTrue:[^ self]. |
|
4078 |
^ self copyTo:index |
|
4079 |
]. |
|
4080 |
index := index - 1 |
|
4081 |
]. |
|
4082 |
^ self class new |
|
4083 |
||
4084 |
" |
|
4085 |
' foo....' withoutTrailingForWhich:[:ch | ch isLetter not]. |
|
4086 |
#( 0 0 0 1 2 3 4 0 0 0) withoutTrailingForWhich:[:e | e = 0] |
|
4087 |
" |
|
602 | 4088 |
! ! |
4089 |
||
4090 |
!SequenceableCollection methodsFor:'enumerating'! |
|
4091 |
||
4092 |
collect:aBlock |
|
4093 |
"evaluate the argument, aBlock for every element in the collection |
|
4094 |
and return a collection of the results" |
|
4095 |
||
4096 |
|newCollection |
|
4097 |
sz "{ Class:SmallInteger }"| |
|
4098 |
||
4099 |
sz := self size. |
|
4100 |
newCollection := self copyEmptyAndGrow:sz. |
|
4101 |
1 to:sz do:[:index | |
|
8874 | 4102 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 4103 |
]. |
4104 |
^ newCollection |
|
4105 |
||
4106 |
" |
|
8874 | 4107 |
#(one two three four five six) collect:[:element | element asUppercase] |
4108 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
4109 |
(1 to:9) collect:[:element | element * element] |
|
602 | 4110 |
" |
202 | 4111 |
! |
4112 |
||
602 | 4113 |
do:aBlock |
4114 |
"evaluate the argument, aBlock for every element in the collection." |
|
4115 |
||
4116 |
|stop "{ Class:SmallInteger }"| |
|
4117 |
||
4118 |
stop := self size. |
|
4119 |
1 to:stop do:[:index | |
|
8874 | 4120 |
aBlock value:(self at:index). |
602 | 4121 |
] |
2927 | 4122 |
|
602 | 4123 |
" |
1422 | 4124 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 4125 |
" |
4126 |
! |
|
4127 |
||
2927 | 4128 |
do:aBlock separatedBy:sepBlock |
4129 |
"evaluate the argument, aBlock for every element in the collection. |
|
4130 |
Between each element (i.e. not before the first element and not after the |
|
4131 |
last element), evaluate sepBlock. |
|
4132 |
This supports printing of collections with elements separated by some |
|
4133 |
string." |
|
4134 |
||
4135 |
|stop "{ Class:SmallInteger }"| |
|
4136 |
||
4137 |
stop := self size. |
|
4138 |
stop < 1 ifTrue:[^ self]. |
|
4139 |
aBlock value:(self at:1). |
|
4140 |
2 to:stop do:[:index | |
|
8874 | 4141 |
sepBlock value. |
4142 |
aBlock value:(self at:index) |
|
2927 | 4143 |
]. |
4144 |
||
4145 |
" |
|
4146 |
#(one two three four five six) |
|
8874 | 4147 |
do:[:each | Transcript show:each] |
4148 |
separatedBy:[Transcript show:' , '] |
|
2927 | 4149 |
" |
4150 |
! |
|
3003 | 4151 |
|
3044 | 4152 |
doWithIndex:aBlock |
4153 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
4154 |
the index as second argument." |
|
4155 |
||
4156 |
self keysAndValuesDo:[:index :value | |
|
8874 | 4157 |
aBlock value:value value:index |
3044 | 4158 |
]. |
4159 |
||
4160 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
4161 |
! |
|
4162 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4163 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4164 |
"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
|
4165 |
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
|
4166 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4167 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4168 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4169 |
" |
8874 | 4170 |
#(one two three four five six) |
4171 |
from:2 |
|
4172 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4173 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4174 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4175 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4176 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4177 |
|
602 | 4178 |
from:startIndex do:aBlock |
4179 |
"evaluate the argument, aBlock for the elements starting with the |
|
4180 |
element at startIndex to the end." |
|
4181 |
||
4182 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 4183 |
|
95 | 4184 |
" |
8874 | 4185 |
#(one two three four five six) |
4186 |
from:3 |
|
4187 |
do:[:element | Transcript showCR:element] |
|
602 | 4188 |
" |
4189 |
! |
|
4190 |
||
7490 | 4191 |
from:startIndex keysAndValuesDo:aBlock |
4192 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
4193 |
element at startIndex to the end." |
|
4194 |
||
4195 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
4196 |
||
4197 |
" |
|
8874 | 4198 |
#(one two three four five six) |
4199 |
from:3 |
|
4200 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 4201 |
" |
4202 |
! |
|
4203 |
||
7337 | 4204 |
from:index1 to:index2 by:stepArg do:aBlock |
4205 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
4206 |
||
4207 |
|start "{ Class:SmallInteger }" |
|
4208 |
stop "{ Class:SmallInteger }" |
|
4209 |
step "{ Class:SmallInteger }" | |
|
4210 |
||
4211 |
start := index1. "/ these assignments force type checking... |
|
4212 |
stop := index2. "/ and guarantee inline loop code below. |
|
4213 |
step := stepArg. |
|
4214 |
||
4215 |
start to:stop by:step do:[:index | |
|
8874 | 4216 |
aBlock value:(self at:index). |
7337 | 4217 |
] |
4218 |
||
4219 |
" |
|
8874 | 4220 |
#(one two three four five six seven eight nine ten) |
4221 |
from:2 |
|
4222 |
to:10 |
|
4223 |
by:2 |
|
4224 |
do:[:element | Transcript showCR:element] |
|
7337 | 4225 |
" |
4226 |
! |
|
4227 |
||
602 | 4228 |
from:start to:stop collect:aBlock |
4229 |
"evaluate the argument, aBlock for the elements indexed by start |
|
4230 |
to stop in the collection and return a collection of the results" |
|
4231 |
||
4232 |
|newCollection sz |
|
4233 |
idx "{ Class:SmallInteger }"| |
|
4234 |
||
4235 |
sz := stop - start + 1. |
|
4236 |
newCollection := self copyEmptyAndGrow:sz. |
|
4237 |
idx := 1. |
|
4238 |
start to:stop do:[:index | |
|
8874 | 4239 |
newCollection at:idx put:(aBlock value:(self at:index)). |
4240 |
idx := idx + 1 |
|
602 | 4241 |
]. |
4242 |
^ newCollection |
|
4243 |
||
4244 |
" |
|
8874 | 4245 |
#(one two three four five six) |
4246 |
from:2 |
|
4247 |
to:4 |
|
4248 |
collect:[:element | element asUppercase] |
|
602 | 4249 |
" |
4250 |
! |
|
4251 |
||
4252 |
from:index1 to:index2 do:aBlock |
|
4253 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4254 |
index2 in the collection" |
|
4255 |
||
4256 |
|start "{ Class:SmallInteger }" |
|
4257 |
stop "{ Class:SmallInteger }" | |
|
4258 |
||
7337 | 4259 |
start := index1. "/ these assignments force type checking... |
4260 |
stop := index2. "/ and guarantee inline loop code below. |
|
4261 |
||
602 | 4262 |
start to:stop do:[:index | |
13384 | 4263 |
aBlock value:(self at:index). |
602 | 4264 |
] |
4265 |
||
4266 |
" |
|
8874 | 4267 |
#(one two three four five six) |
13384 | 4268 |
from:3 to:5 do:[:element | Transcript showCR:element] |
4269 |
" |
|
4270 |
||
4271 |
"Modified: / 02-06-2011 / 13:23:06 / cg" |
|
602 | 4272 |
! |
4273 |
||
6868 | 4274 |
from:index1 to:index2 keysAndValuesDo:aBlock |
4275 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4276 |
index2 in the collection; pass both index and value." |
|
4277 |
||
4278 |
|start "{ Class:SmallInteger }" |
|
4279 |
stop "{ Class:SmallInteger }" | |
|
4280 |
||
7337 | 4281 |
start := index1. "/ these assignments force type checking... |
4282 |
stop := index2. "/ and guarantee inline loop code below. |
|
4283 |
||
6868 | 4284 |
start to:stop do:[:index | |
8874 | 4285 |
aBlock value:index value:(self at:index). |
6868 | 4286 |
] |
4287 |
||
4288 |
" |
|
8874 | 4289 |
#(one two three four five six) |
4290 |
from:3 |
|
4291 |
to:5 |
|
4292 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 4293 |
" |
4294 |
! |
|
4295 |
||
13383 | 4296 |
from:index1 to:index2 orEndDo:aBlock |
4297 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4298 |
index2 or the end (whichever comes first) in the collection" |
|
4299 |
||
4300 |
self from:index1 to:(index2 min:self size) do:aBlock |
|
4301 |
||
4302 |
" |
|
4303 |
#(one two three four five six) |
|
13384 | 4304 |
from:3 to:10 orEndDo:[:element | Transcript showCR:element] |
13383 | 4305 |
" |
4306 |
||
4307 |
"Created: / 02-06-2011 / 13:21:32 / cg" |
|
4308 |
! |
|
4309 |
||
602 | 4310 |
from:index1 to:index2 reverseDo:aBlock |
4311 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4312 |
index2 in the collection. Step in reverse order" |
|
4313 |
||
4314 |
|start "{ Class:SmallInteger }" |
|
4315 |
stop "{ Class:SmallInteger }" | |
|
4316 |
||
7337 | 4317 |
start := index1. "/ these assignments force type checking... |
4318 |
stop := index2. "/ and guarantee inline loop code below. |
|
4319 |
||
602 | 4320 |
stop to:start by:-1 do:[:index | |
8874 | 4321 |
aBlock value:(self at:index). |
602 | 4322 |
] |
4323 |
||
4324 |
" |
|
8874 | 4325 |
#(one two three four five six) |
4326 |
from:3 |
|
4327 |
to:5 |
|
4328 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 4329 |
" |
61 | 4330 |
! |
4331 |
||
6027 | 4332 |
from:startIndex to:stopIndex select:aBlock |
4333 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
4334 |
and return a collection of those elements for which the block return true." |
|
4335 |
||
4336 |
|element newColl species needCopy |
|
4337 |
n "{ Class:SmallInteger }"| |
|
4338 |
||
4339 |
n := stopIndex - startIndex + 1. |
|
4340 |
species := self species. |
|
4341 |
species growIsCheap ifFalse:[ |
|
8874 | 4342 |
newColl := OrderedCollection new:n. |
4343 |
needCopy := true |
|
6027 | 4344 |
] ifTrue:[ |
8874 | 4345 |
newColl := self copyEmpty:n. |
4346 |
needCopy := false |
|
6027 | 4347 |
]. |
4348 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4349 |
element := self at:index. |
4350 |
(aBlock value:element) ifTrue:[ |
|
4351 |
newColl add:element |
|
4352 |
]. |
|
6027 | 4353 |
]. |
4354 |
needCopy ifTrue:[ |
|
8874 | 4355 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 4356 |
]. |
4357 |
^ newColl |
|
4358 |
||
4359 |
" |
|
8874 | 4360 |
#(faba one two three four five six) |
4361 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 4362 |
" |
4363 |
! |
|
4364 |
||
10150 | 4365 |
inGroupsOf:n collect:anNArgBlock |
4366 |
"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
|
4367 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4368 |
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
|
4369 |
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
|
4370 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4371 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4372 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4373 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4374 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4375 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4376 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4377 |
|
10150 | 4378 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
4379 |
"/ 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
|
4380 |
n == 2 ifTrue:[ |
10150 | 4381 |
1 to:stop by:n do:[:index | |
4382 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4383 |
newCollection at:dstIdx put:rslt. |
|
4384 |
dstIdx := dstIdx + 1. |
|
4385 |
]. |
|
4386 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4387 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4388 |
n == 3 ifTrue:[ |
10150 | 4389 |
1 to:stop by:n do:[:index | |
4390 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4391 |
newCollection at:dstIdx put:rslt. |
|
4392 |
dstIdx := dstIdx + 1. |
|
4393 |
]. |
|
4394 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4395 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4396 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4397 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4398 |
1 to:stop by:n do:[:index | |
10150 | 4399 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4400 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
4401 |
newCollection at:dstIdx put:rslt. |
|
4402 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4403 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4404 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4405 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4406 |
"for groups of 2, this is the same as: |
8874 | 4407 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4408 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 4409 |
|
4410 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4411 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4412 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4413 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4414 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
4415 |
" |
|
4416 |
||
4417 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
4418 |
! |
|
4419 |
||
4420 |
inGroupsOf:n do:anNArgBlock |
|
4421 |
"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
|
4422 |
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
|
4423 |
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
|
4424 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4425 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4426 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4427 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4428 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4429 |
|
10150 | 4430 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
4431 |
"/ 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
|
4432 |
n == 2 ifTrue:[ |
10150 | 4433 |
1 to:stop by:n do:[:index | |
4434 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4435 |
]. |
|
4436 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4437 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4438 |
n == 3 ifTrue:[ |
10150 | 4439 |
1 to:stop by:n do:[:index | |
4440 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4441 |
]. |
|
4442 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4443 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4444 |
n == 4 ifTrue:[ |
10150 | 4445 |
1 to:stop by:n do:[:index | |
4446 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
4447 |
]. |
|
4448 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4449 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4450 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4451 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4452 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4453 |
1 to:stop by:n do:[:index | |
10150 | 4454 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4455 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4456 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4457 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4458 |
"for groups of 2, this is the same as: |
8874 | 4459 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4460 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4461 |
|
4462 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4463 |
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
|
4464 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4465 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4466 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
4467 |
" |
|
4468 |
||
4469 |
"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
|
4470 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4471 |
|
8853 | 4472 |
keysAndValuesCollect:aTwoArgBlock |
4473 |
"evaluate the argument, aBlock for every element in the collection, |
|
4474 |
passing both index and element as arguments. |
|
4475 |
Collect the returned values and return them." |
|
4476 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4477 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4478 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4479 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4480 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4481 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4482 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 4483 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4484 |
|
8853 | 4485 |
^ newCollection |
4486 |
||
4487 |
" |
|
8874 | 4488 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4489 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4490 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 4491 |
|
4492 |
||
4493 |
|d| |
|
4494 |
||
4495 |
d := Dictionary new. |
|
4496 |
d at:'a' put:'A'. |
|
4497 |
d at:'b' put:'B'. |
|
4498 |
d at:'c' put:'C'. |
|
4499 |
d at:'d' put:'D'. |
|
4500 |
d at:'e' put:'E'. |
|
8874 | 4501 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4502 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4503 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4504 |
ifFalse:[element asLowercase]] |
8853 | 4505 |
" |
4506 |
! |
|
4507 |
||
602 | 4508 |
keysAndValuesDo:aTwoArgBlock |
4509 |
"evaluate the argument, aBlock for every element in the collection, |
|
4510 |
passing both index and element as arguments." |
|
4511 |
||
4512 |
|stop "{ Class:SmallInteger }"| |
|
4513 |
||
4514 |
stop := self size. |
|
4515 |
1 to:stop do:[:index | |
|
8874 | 4516 |
aTwoArgBlock value:index value:(self at:index). |
602 | 4517 |
] |
4518 |
" |
|
8874 | 4519 |
#(one two three four five six) |
4520 |
keysAndValuesDo:[:key :element | |
|
4521 |
Transcript show:key; space; showCR:element |
|
4522 |
] |
|
602 | 4523 |
" |
4524 |
! |
|
4525 |
||
1359 | 4526 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 4527 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 4528 |
in the collection, passing both index and element as arguments." |
4529 |
||
4530 |
|stop "{ Class:SmallInteger }"| |
|
4531 |
||
4532 |
stop := self size. |
|
4533 |
stop to:1 by:-1 do:[:index | |
|
8874 | 4534 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 4535 |
] |
4536 |
||
4537 |
" |
|
8874 | 4538 |
#(one two three four five six) |
4539 |
keysAndValuesReverseDo:[:key :element | |
|
4540 |
Transcript show:key; space; showCR:element |
|
4541 |
] |
|
1359 | 4542 |
" |
4543 |
||
4544 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
4545 |
! |
|
4546 |
||
13043 | 4547 |
keysDo:aBlock |
4548 |
"evaluate the argument, aBlock for every key in the collection. |
|
4549 |
That is: enumerate the indices. |
|
4550 |
Here mostly for protocol compatibility." |
|
4551 |
||
4552 |
1 to:self size do:aBlock |
|
4553 |
||
4554 |
" |
|
4555 |
#(one two three four five six) |
|
4556 |
keysDo:[:key | Transcript show:key; space; showCR:element |
|
4557 |
] |
|
4558 |
" |
|
4559 |
||
4560 |
"Created: / 24-08-2010 / 10:11:23 / cg" |
|
4561 |
! |
|
4562 |
||
602 | 4563 |
nonNilElementsDo:aBlock |
4564 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
4565 |
||
4566 |
|stop "{ Class:SmallInteger }" |
|
4567 |
element| |
|
4568 |
||
4569 |
stop := self size. |
|
4570 |
1 to:stop do:[:index | |
|
8874 | 4571 |
(element := self at:index) notNil ifTrue:[ |
4572 |
aBlock value:element. |
|
4573 |
] |
|
602 | 4574 |
] |
4575 |
" |
|
1422 | 4576 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 4577 |
" |
4578 |
! |
|
4579 |
||
10150 | 4580 |
pairWiseCollect:aTwoArgBlock |
4581 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 4582 |
The block is called with 2 arguments for each 2 elements in the receiver. |
4583 |
An error will be reported, if the number of elements in the receiver |
|
4584 |
is not a multiple of 2. |
|
4585 |
Collect the results and return a new collection containing those." |
|
4586 |
||
10150 | 4587 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 4588 |
|
95 | 4589 |
" |
8874 | 4590 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4591 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 4592 |
|
4593 |
||
4594 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4595 |
pairWiseCollect:[:x :y | x@y] |
4596 |
" |
|
4597 |
||
4598 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
4599 |
! |
|
4600 |
||
4601 |
pairWiseDo:aTwoArgBlock |
|
10385 | 4602 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
4603 |
An error will be reported, if the number of elements in the receiver |
|
12346 | 4604 |
is not a multiple of 2. |
4605 |
CONFUSION ATTACK: |
|
4606 |
this is different from pairWiseDo:. |
|
4607 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
|
4608 |
(sigh: but we were first, so they should have adapted...)" |
|
602 | 4609 |
|
10150 | 4610 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4611 |
|
602 | 4612 |
" |
8874 | 4613 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4614 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4615 |
|
4616 |
||
4617 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4618 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
4619 |
" |
|
4620 |
||
10385 | 4621 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 4622 |
! |
4623 |
||
602 | 4624 |
reverseDo:aBlock |
4625 |
"evaluate the argument, aBlock for every element in the collection |
|
4626 |
in reverse order" |
|
4627 |
||
4628 |
|sz "{ Class:SmallInteger }"| |
|
4629 |
||
4630 |
sz := self size. |
|
4631 |
sz to:1 by:-1 do:[:index | |
|
8874 | 4632 |
aBlock value:(self at:index). |
602 | 4633 |
] |
4634 |
||
4635 |
" |
|
1422 | 4636 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 4637 |
" |
4638 |
! |
|
4639 |
||
4640 |
select:aBlock |
|
4641 |
"evaluate the argument, aBlock for every element in the collection |
|
4642 |
and return a collection of all elements for which the block return |
|
4643 |
true" |
|
4644 |
||
4645 |
|element newColl species needCopy |
|
4646 |
sz "{ Class:SmallInteger }"| |
|
4647 |
||
4648 |
sz := self size. |
|
4649 |
species := self species. |
|
4650 |
species growIsCheap ifFalse:[ |
|
8874 | 4651 |
newColl := OrderedCollection new:sz. |
4652 |
needCopy := true |
|
602 | 4653 |
] ifTrue:[ |
8874 | 4654 |
newColl := self copyEmpty:sz. |
4655 |
needCopy := false |
|
602 | 4656 |
]. |
4657 |
1 to:sz do:[:index | |
|
8874 | 4658 |
element := self at:index. |
4659 |
(aBlock value:element) ifTrue:[ |
|
4660 |
newColl add:element |
|
4661 |
]. |
|
602 | 4662 |
]. |
4663 |
needCopy ifTrue:[ |
|
8874 | 4664 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 4665 |
]. |
61 | 4666 |
^ newColl |
4667 |
||
95 | 4668 |
" |
8874 | 4669 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
4670 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
4671 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
4672 |
(1 to:9) select:[:element | element odd] |
|
4673 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 4674 |
" |
186 | 4675 |
! |
4676 |
||
602 | 4677 |
with:aSequenceableCollection do:aTwoArgBlock |
4678 |
"evaluate the argument, aBlock for successive elements from |
|
4679 |
each the receiver and the argument, aSequenceableCollection. |
|
4680 |
The second argument, aBlock must be a two-argument block. |
|
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
4681 |
The collection argument must implement access via a numeric key |
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
4682 |
and (new!!) the sizes must be the same." |
602 | 4683 |
|
4684 |
|stop "{ Class: SmallInteger }" | |
|
4685 |
||
4686 |
stop := self size. |
|
13465 | 4687 |
aSequenceableCollection size == stop ifFalse:[ |
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
4688 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
13465 | 4689 |
]. |
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
4690 |
|
602 | 4691 |
1 to:stop do:[:index | |
13465 | 4692 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 4693 |
] |
13465 | 4694 |
|
186 | 4695 |
" |
8874 | 4696 |
#(one two three four five six) |
13465 | 4697 |
with:(1 to:10) |
4698 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4699 |
" |
|
4700 |
||
13469
7f911a9f891d
comment/format in:6 methods
Claus Gittinger <cg@exept.de>
parents:
13465
diff
changeset
|
4701 |
"Modified (Format): / 30-06-2011 / 17:39:59 / cg" |
9454 | 4702 |
! |
4703 |
||
4704 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
4705 |
"evaluate the argument, aBlock for successive elements from |
|
4706 |
each the receiver and the argument, aSequenceableCollection. |
|
4707 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4708 |
the index as arguments. |
|
4709 |
The collection argument must implement access via a numeric key." |
|
4710 |
||
4711 |
|stop "{ Class: SmallInteger }" | |
|
4712 |
||
4713 |
stop := self size. |
|
4714 |
1 to:stop do:[:index | |
|
4715 |
aThreeArgBlock |
|
4716 |
value:(self at:index) |
|
4717 |
value:(aSequenceableCollection at:index) |
|
4718 |
value:index. |
|
4719 |
] |
|
4720 |
" |
|
4721 |
#(one two three four five six) |
|
4722 |
with:(1 to:10) |
|
4723 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4724 |
" |
|
4725 |
||
4726 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 4727 |
! ! |
4728 |
||
4154 | 4729 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 4730 |
|
602 | 4731 |
atAllPut:anObject |
4732 |
"replace all elements of the collection by the argument, anObject. |
|
4733 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4734 |
therefore the change may affect all others referencing the receiver." |
|
4735 |
||
4736 |
self from:1 to:(self size) put:anObject |
|
4737 |
||
4738 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4739 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4740 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4741 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4742 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4743 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4744 |
" |
602 | 4745 |
(Array new:10) atAllPut:1 |
4746 |
(String new:10) atAllPut:$a |
|
4747 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4748 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4749 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 4750 |
! |
4751 |
||
1 | 4752 |
from:index1 to:index2 put:anObject |
4753 |
"replace the elements from index1 to index2 of the collection |
|
44 | 4754 |
by the argument, anObject. |
4755 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4756 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4757 |
|
4758 |
|index "{ Class: SmallInteger }" |
|
4759 |
end "{ Class: SmallInteger }"| |
|
4760 |
||
4761 |
index := index1. |
|
4762 |
end := index2. |
|
4763 |
[index <= end] whileTrue:[ |
|
8874 | 4764 |
self at:index put:anObject. |
4765 |
index := index + 1 |
|
1 | 4766 |
] |
44 | 4767 |
|
95 | 4768 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4769 |
args: index1 : <integer> |
8874 | 4770 |
index2 : <integer> |
4771 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4772 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4773 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4774 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4775 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4776 |
" |
95 | 4777 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
4778 |
'abcdefghijkl' from:3 to:6 put:$X |
|
4779 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4780 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4781 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 4782 |
! |
4783 |
||
3003 | 4784 |
replaceAll:oldObject by:newObject |
4785 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
4786 |
Pleace do no longer use this one; |
|
4787 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
4788 |
||
5872 | 4789 |
<resource:#obsolete> |
4790 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4791 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 4792 |
^ self replaceAll:oldObject with:newObject |
4793 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4794 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4795 |
args: oldObject : <object> |
8874 | 4796 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4797 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4798 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4799 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4800 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4801 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 4802 |
! |
4803 |
||
4804 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
4805 |
"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
|
4806 |
Please do no longer use this one; |
3003 | 4807 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
4808 |
||
5872 | 4809 |
<resource:#obsolete> |
4810 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4811 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 4812 |
^ 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
|
4813 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4814 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4815 |
args: oldObject : <object> |
8874 | 4816 |
newObject : <object> |
4817 |
startIndex : <integer> |
|
4818 |
stopIndex : <integer> |
|
4819 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4820 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4821 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4822 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4823 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 4824 |
! |
4825 |
||
2927 | 4826 |
replaceAll:oldObject with:newObject |
44 | 4827 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4828 |
|
44 | 4829 |
Notice: This operation modifies the receiver, NOT a copy; |
4830 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4831 |
|
8874 | 4832 |
^ self |
4833 |
replaceAll:oldObject |
|
4834 |
with:newObject |
|
4835 |
from:1 |
|
4836 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4837 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4838 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4839 |
args: oldObject : <object> |
8874 | 4840 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4841 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4842 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4843 |
" |
362 | 4844 |
|
4845 |
" |
|
8874 | 4846 |
'123123abc123' replaceAll:$1 with:$* |
4847 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4848 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4849 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4850 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4851 |
! |
293 | 4852 |
|
2927 | 4853 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4854 |
"replace all oldObjects found between startIndex and endIndex, |
4855 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4856 |
|
362 | 4857 |
Notice: This operation modifies the receiver, NOT a copy; |
4858 |
therefore the change may affect all others referencing the receiver." |
|
4859 |
||
4860 |
|start "{ Class: SmallInteger }" |
|
4861 |
stop "{ Class: SmallInteger }"| |
|
4862 |
||
4863 |
start := startIndex. |
|
4864 |
stop := stopIndex. |
|
4865 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4866 |
(self at:index) = oldObject ifTrue:[ |
4867 |
self at:index put:newObject |
|
4868 |
] |
|
1 | 4869 |
] |
44 | 4870 |
|
95 | 4871 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4872 |
args: oldObject : <object> |
8874 | 4873 |
newObject : <object> |
4874 |
startIndex : <integer> |
|
4875 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4876 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4877 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4878 |
" |
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 |
" |
2980 | 4881 |
'123123abc123' replaceAll:$1 with:$* |
4882 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4883 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4884 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4885 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4886 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4887 |
! |
4888 |
||
7615 | 4889 |
replaceAllForWhich:aConditionBlock with:newObject |
4890 |
"replace all elements for which aConditionBlock returns true |
|
4891 |
between startIndex and endIndex, by newObject in the receiver. |
|
4892 |
||
4893 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4894 |
therefore the change may affect all others referencing the receiver." |
|
4895 |
||
8874 | 4896 |
self |
4897 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4898 |
from:1 to:(self size) |
|
7615 | 4899 |
|
4900 |
" |
|
4901 |
args: aConditionBlock : <block> |
|
8874 | 4902 |
newObject : <object> |
7615 | 4903 |
|
4904 |
returns: self |
|
4905 |
" |
|
4906 |
! |
|
4907 |
||
4908 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4909 |
"replace all elements for which aConditionBlock returns true |
|
4910 |
between startIndex and endIndex, by newObject in the receiver. |
|
4911 |
||
4912 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4913 |
therefore the change may affect all others referencing the receiver." |
|
4914 |
||
4915 |
|start "{ Class: SmallInteger }" |
|
4916 |
stop "{ Class: SmallInteger }"| |
|
4917 |
||
4918 |
start := startIndex. |
|
4919 |
stop := stopIndex. |
|
4920 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4921 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4922 |
self at:index put:newObject |
|
4923 |
] |
|
7615 | 4924 |
] |
4925 |
||
4926 |
" |
|
4927 |
args: aConditionBlock : <block> |
|
8874 | 4928 |
newObject : <object> |
4929 |
startIndex : <integer> |
|
4930 |
stopIndex : <integer> |
|
7615 | 4931 |
|
4932 |
returns: self |
|
4933 |
" |
|
4934 |
! |
|
4935 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4936 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4937 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4938 |
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
|
4939 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4940 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4941 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4942 |
|
8874 | 4943 |
^ self |
4944 |
replaceAllIdentical:oldObject |
|
4945 |
with:newObject |
|
4946 |
from:1 |
|
4947 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4948 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4949 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4950 |
args: oldObject : <object> |
8874 | 4951 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4952 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4953 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4954 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4955 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4956 |
" |
8874 | 4957 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4958 |
#(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
|
4959 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4960 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4961 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4962 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4963 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4964 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4965 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4966 |
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
|
4967 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4968 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4969 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4970 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4971 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4972 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4973 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4974 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4975 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4976 |
startIndex to:stopIndex do:[:index | |
8874 | 4977 |
(self at:index) == oldObject ifTrue:[ |
4978 |
self at:index put:newObject |
|
4979 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4980 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4981 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4982 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4983 |
args: oldObject : <object> |
8874 | 4984 |
newObject : <object> |
4985 |
startIndex : <integer> |
|
4986 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4987 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4988 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4989 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4990 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4991 |
" |
8874 | 4992 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4993 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4994 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4995 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4996 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4997 |
|
3003 | 4998 |
replaceAny:aCollection by:newObject |
4999 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 5000 |
Pleace do no longer use this one; |
3003 | 5001 |
instead use #replaceAny:with: for naming consistence." |
5002 |
||
5872 | 5003 |
<resource:#obsolete> |
5004 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5005 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 5006 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 5007 |
|
5008 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5009 |
args: aCollection : <colleciton of <object> > |
8874 | 5010 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5011 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5012 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5013 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5014 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5015 |
" |
3003 | 5016 |
'123123abc123' replaceAny:#($1 $2) with:$* |
5017 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
5018 |
" |
|
5019 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5020 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 5021 |
! |
5022 |
||
3003 | 5023 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
5024 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 5025 |
Pleace do no longer use this one; |
3003 | 5026 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 5027 |
|
5872 | 5028 |
<resource:#obsolete> |
5029 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5030 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 5031 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 5032 |
|
5033 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5034 |
args: aCollection : <colleciton of <object> > |
8874 | 5035 |
newObject : <object> |
5036 |
startIndex : <integer> |
|
5037 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5038 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5039 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5040 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5041 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5042 |
" |
8874 | 5043 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
5044 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 5045 |
" |
5046 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5047 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 5048 |
! |
5049 |
||
5050 |
replaceAny:aCollection with:newObject |
|
5051 |
"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
|
5052 |
|
2927 | 5053 |
Notice: This operation modifies the receiver, NOT a copy; |
5054 |
therefore the change may affect all others referencing the receiver." |
|
5055 |
||
8874 | 5056 |
^ self |
10439 | 5057 |
replaceAny:aCollection |
5058 |
with:newObject |
|
5059 |
from:1 |
|
5060 |
to:self size |
|
5061 |
||
5062 |
" |
|
5063 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
5064 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
5065 |
" |
|
5066 |
||
5067 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 5068 |
! |
5069 |
||
2927 | 5070 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 5071 |
"replace all elements within a range, |
5072 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5073 |
|
362 | 5074 |
Notice: This operation modifies the receiver, NOT a copy; |
5075 |
therefore the change may affect all others referencing the receiver." |
|
5076 |
||
5077 |
|start "{ Class: SmallInteger }" |
|
5078 |
stop "{ Class: SmallInteger }"| |
|
5079 |
||
5080 |
start := startIndex. |
|
5081 |
stop := stopIndex. |
|
5082 |
startIndex to:stopIndex do:[:index | |
|
8874 | 5083 |
(aCollection includes:(self at:index)) ifTrue:[ |
5084 |
self at:index put:newObject |
|
5085 |
] |
|
95 | 5086 |
] |
5087 |
||
5088 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5089 |
args: aCollection : <colleciton of <object> > |
8874 | 5090 |
newObject : <object> |
5091 |
startIndex : <integer> |
|
5092 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5093 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5094 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5095 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5096 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5097 |
" |
8874 | 5098 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
5099 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 5100 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5101 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5102 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 5103 |
! |
5104 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5105 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5106 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5107 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5108 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5109 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5110 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5111 |
|
8874 | 5112 |
^ self |
5113 |
replaceFrom:startIndex |
|
5114 |
to:(startIndex + numberOfElements - 1) |
|
5115 |
with:replacementCollection |
|
5116 |
startingAt:1 |
|
5117 |
||
5118 |
" |
|
5119 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5120 |
#($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
|
5121 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5122 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5123 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5124 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5125 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5126 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5127 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5128 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5129 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5130 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5131 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5132 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5133 |
^ self |
8874 | 5134 |
replaceFrom:startIndex |
5135 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
5136 |
with:replacementCollection |
|
5137 |
startingAt:repStartIndex |
|
5138 |
||
5139 |
" |
|
5140 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
5141 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
5142 |
#($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
|
5143 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5144 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5145 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5146 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5147 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5148 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5149 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 5150 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5151 |
|
44 | 5152 |
Notice: This operation modifies the receiver, NOT a copy; |
5153 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5154 |
|
8874 | 5155 |
^ self |
5156 |
replaceFrom:startIndex |
|
5157 |
to:stopIndex |
|
5158 |
with:replacementCollection |
|
5159 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5160 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5161 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5162 |
args: startIndex : <integer> |
8874 | 5163 |
stopIndex : <integer> |
5164 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5165 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5166 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5167 |
" |
61 | 5168 |
|
95 | 5169 |
" |
5170 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
5171 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
5172 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5173 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5174 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 5175 |
! |
5176 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5177 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5178 |
"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
|
5179 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 5180 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5181 |
|
44 | 5182 |
Notice: This operation modifies the receiver, NOT a copy; |
5183 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5184 |
|
5185 |
|srcIndex "{ Class: SmallInteger }" |
|
5186 |
dstIndex "{ Class: SmallInteger }" |
|
5187 |
end "{ Class: SmallInteger }" | |
|
5188 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5189 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 5190 |
" must do reverse copy " |
5191 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
5192 |
dstIndex := stopIndex. |
|
5193 |
end := startIndex. |
|
5194 |
[dstIndex >= end] whileTrue:[ |
|
5195 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
5196 |
srcIndex := srcIndex - 1. |
|
5197 |
dstIndex := dstIndex - 1 |
|
5198 |
]. |
|
5199 |
^ self |
|
1 | 5200 |
]. |
5201 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5202 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5203 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5204 |
end := stopIndex. |
1 | 5205 |
[dstIndex <= end] whileTrue:[ |
8874 | 5206 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
5207 |
srcIndex := srcIndex + 1. |
|
5208 |
dstIndex := dstIndex + 1 |
|
1 | 5209 |
] |
61 | 5210 |
|
95 | 5211 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5212 |
args: startIndex : <integer> |
8874 | 5213 |
stopIndex : <integer> |
5214 |
replacementCollection : <colleciton of <object> > |
|
5215 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5216 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5217 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5218 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5219 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5220 |
" |
8874 | 5221 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
5222 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
5223 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 5224 |
|
5225 |
|c| |
|
5226 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5227 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 5228 |
|
5229 |
|c| |
|
5230 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5231 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 5232 |
" |
3234 | 5233 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5234 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 5235 |
! |
5236 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5237 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5238 |
"replace elements in the receiver starting at startIndex, |
602 | 5239 |
with elements taken from replacementCollection starting at 1 |
5240 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5241 |
|
602 | 5242 |
Notice: This operation modifies the receiver, NOT a copy; |
5243 |
therefore the change may affect all others referencing the receiver." |
|
5244 |
||
8874 | 5245 |
^ self |
5246 |
replaceFrom:startIndex |
|
5247 |
to:(startIndex + replacementCollection size - 1) |
|
5248 |
with:replacementCollection |
|
5249 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5250 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5251 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5252 |
args: startIndex : <integer> |
8874 | 5253 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5254 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5255 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5256 |
" |
602 | 5257 |
|
5258 |
" |
|
5259 |
'1234567890' replaceFrom:5 with:'abc' |
|
5260 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
5261 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5262 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5263 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 5264 |
! |
5265 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5266 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5267 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5268 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 5269 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5270 |
|
602 | 5271 |
Notice: This operation modifies the receiver, NOT a copy; |
5272 |
therefore the change may affect all others referencing the receiver." |
|
5273 |
||
8874 | 5274 |
^ self |
5275 |
replaceFrom:startIndex |
|
5276 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
5277 |
with:replacementCollection |
|
5278 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5279 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5280 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5281 |
args: startIndex : <integer> |
8874 | 5282 |
replacementCollection : <colleciton of <object> > |
5283 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5284 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5285 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5286 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5287 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5288 |
" |
8874 | 5289 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 5290 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
5291 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5292 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5293 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 5294 |
! |
5295 |
||
61 | 5296 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
5297 |
"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
|
5298 |
|
61 | 5299 |
Notice: This operation modifies the destination collection, NOT a copy; |
5300 |
therefore the change may affect all others referencing this object." |
|
5301 |
||
8874 | 5302 |
destColl |
5303 |
replaceFrom:destStartIndex |
|
5304 |
to:destEndIndex |
|
5305 |
with:self |
|
5306 |
startingAt:sourceStart |
|
61 | 5307 |
|
5308 |
" |
|
5309 |
|s| |
|
5310 |
s := 'abcdefghijklmnop'. |
|
5311 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
5312 |
s'123defghijklmnop' |
|
5313 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5314 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5315 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 5316 |
! ! |
5317 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5318 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5319 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5320 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5321 |
"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
|
5322 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5323 |
Elements on the left are filled with padElement. |
8874 | 5324 |
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
|
5325 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5326 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5327 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5328 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5329 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5330 |
(len < size) ifTrue:[ |
8874 | 5331 |
s := self species new:size withAll:padElement. |
5332 |
s replaceFrom:(size - len + 1) with:self. |
|
5333 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5334 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5335 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5336 |
" |
8874 | 5337 |
'foo' leftPaddedTo:10 with:$. |
5338 |
'fooBar' leftPaddedTo:5 with:$. |
|
5339 |
123 printString leftPaddedTo:10 with:$. |
|
5340 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
5341 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
5342 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
5343 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
5344 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
5345 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
5346 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5347 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5348 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5349 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5350 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5351 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5352 |
plus pad elements up to length. |
8874 | 5353 |
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
|
5354 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5355 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5356 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5357 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5358 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5359 |
len < newSize ifTrue:[ |
8874 | 5360 |
s := self species new:newSize withAll:padElement. |
5361 |
s replaceFrom:1 to:len with:self. |
|
5362 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5363 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5364 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5365 |
" |
8874 | 5366 |
'foo' paddedTo:10 with:$. |
5367 |
123 printString paddedTo:10 with:$* |
|
5368 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
5369 |
(Float pi printString) paddedTo:15 with:$- |
|
5370 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
5371 |
#[1 2 3 4] paddedTo:6 with:0 |
|
5372 |
#[1 2 3 4] paddedTo:10 with:99 |
|
5373 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5374 |
" |
7116 | 5375 |
! |
5376 |
||
5377 |
paddedToMultipleOf:sizeModulu with:padElement |
|
5378 |
|mySize mod numPad| |
|
5379 |
||
5380 |
mySize := self size. |
|
5381 |
mod := (mySize \\ sizeModulu). |
|
5382 |
mod == 0 ifTrue:[^ self]. |
|
5383 |
||
5384 |
numPad := sizeModulu - mod. |
|
5385 |
^ self , (self species new:numPad withAll:padElement). |
|
5386 |
||
5387 |
" |
|
8874 | 5388 |
#[] paddedToMultipleOf:3 with:0 |
5389 |
#[1] paddedToMultipleOf:3 with:0 |
|
5390 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
5391 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 5392 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
5393 |
||
5394 |
#() paddedToMultipleOf:3 with:nil |
|
5395 |
#(1) paddedToMultipleOf:3 with:nil |
|
5396 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
5397 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
5398 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
5399 |
||
5400 |
'' paddedToMultipleOf:3 with:Character space |
|
5401 |
'a' paddedToMultipleOf:3 with:Character space |
|
5402 |
'ab' paddedToMultipleOf:3 with:Character space |
|
5403 |
'abc' paddedToMultipleOf:3 with:Character space |
|
5404 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
5405 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5406 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5407 |
|
7260 | 5408 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 5409 |
|
5358 | 5410 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 5411 |
"Private!! |
5358 | 5412 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
5413 |
||
5414 |
| i1 i2 val1 val2 out | |
|
5415 |
||
5416 |
i1 := first. |
|
5417 |
i2 := middle + 1. |
|
5418 |
val1 := self at: i1. |
|
5419 |
val2 := self at: i2. |
|
5420 |
out := first - 1. "will be pre-incremented" |
|
5421 |
||
5422 |
"select 'lower' half of the elements based on comparator" |
|
5423 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 5424 |
(aBlock value: val1 value: val2) |
5425 |
ifTrue: [ |
|
5426 |
dst at: (out := out + 1) put: val1. |
|
5427 |
val1 := self at: (i1 := i1 + 1)] |
|
5428 |
ifFalse: [ |
|
5429 |
dst at: (out := out + 1) put: val2. |
|
5430 |
i2 := i2 + 1. |
|
5431 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 5432 |
|
5433 |
"copy the remaining elements" |
|
5434 |
i1 <= middle |
|
8874 | 5435 |
ifTrue: [ |
5436 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
5437 |
ifFalse: [ |
|
5438 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 5439 |
|
5440 |
! |
|
5441 |
||
5442 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
5443 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
5444 |
||
5445 |
| middle | |
|
5446 |
||
5447 |
first = last ifTrue: [^ self]. |
|
5448 |
middle := (first + last) // 2. |
|
5449 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
5450 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
5451 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
5452 |
||
5453 |
||
5454 |
! |
|
5455 |
||
2069 | 5456 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5457 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5458 |
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
|
5459 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5460 |
|
8874 | 5461 |
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
|
5462 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5463 |
|
5464 |
|begin "{ Class: SmallInteger }" |
|
5465 |
end "{ Class: SmallInteger }" |
|
5466 |
b "{ Class: SmallInteger }" |
|
5467 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5468 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5469 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5470 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5471 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5472 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5473 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5474 |
[stack notEmpty] whileTrue:[ |
8874 | 5475 |
end := stack removeLast. |
5476 |
begin := stack removeLast. |
|
5477 |
||
5478 |
b := begin. |
|
5479 |
e := end. |
|
5480 |
middleElement := self at:((b + e) // 2). |
|
5481 |
||
5482 |
[b < e] whileTrue:[ |
|
5483 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5484 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5485 |
||
5486 |
(b <= e) ifTrue:[ |
|
5487 |
(b == e) ifFalse:[ |
|
5488 |
temp1 := self at:b. temp2 := self at:e. |
|
5489 |
self at:b put:temp2. self at:e put:temp1 |
|
5490 |
]. |
|
5491 |
b := b + 1. |
|
5492 |
e := e - 1 |
|
5493 |
] |
|
5494 |
]. |
|
5495 |
(begin < e) ifTrue:[ |
|
5496 |
stack add:begin. |
|
5497 |
stack add:e. |
|
5498 |
]. |
|
5499 |
(b < end) ifTrue:[ |
|
5500 |
stack add:b. |
|
5501 |
stack add:end. |
|
5502 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5503 |
] |
2069 | 5504 |
! |
5505 |
||
5506 |
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
|
5507 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5508 |
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
|
5509 |
with degenerated collections." |
2069 | 5510 |
|
5511 |
|begin "{ Class: SmallInteger }" |
|
5512 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5513 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5514 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5515 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5516 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5517 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5518 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5519 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5520 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5521 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5522 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5523 |
[stack notEmpty] whileTrue:[ |
8874 | 5524 |
end := stack removeLast. |
5525 |
begin := stack removeLast. |
|
5526 |
||
5527 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5528 |
elB := self at:begin. |
|
5529 |
elE := self at:end. |
|
5530 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5531 |
temp := elB. elB := elE. elE := temp. |
|
5532 |
self at:begin put:elB. |
|
5533 |
self at:end put:elE. |
|
5534 |
]. |
|
5535 |
||
5536 |
nToSort > 2 ifTrue:[ |
|
5537 |
m := (begin + end) // 2. |
|
5538 |
elM := self at:m. |
|
5539 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5540 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5541 |
temp := self at:end. |
|
5542 |
self at:end put:(self at:m). |
|
5543 |
self at:m put:temp. |
|
5544 |
elM := elE |
|
5545 |
] |
|
5546 |
] ifFalse:[ |
|
5547 |
temp := self at:begin. |
|
5548 |
self at:begin put:(self at:m). |
|
5549 |
self at:m put:temp. |
|
5550 |
elM := elB |
|
5551 |
]. |
|
5552 |
nToSort > 3 ifTrue:[ |
|
5553 |
bRun := begin. |
|
5554 |
eRun := end. |
|
5555 |
[ |
|
5556 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5557 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5558 |
bRun <= eRun |
|
5559 |
] whileTrue:[ |
|
5560 |
temp := self at:bRun. |
|
5561 |
self at:bRun put:(self at:eRun). |
|
5562 |
self at:eRun put:temp. |
|
5563 |
]. |
|
5564 |
stack add:begin. |
|
5565 |
stack add:eRun. |
|
5566 |
||
5567 |
stack add:bRun. |
|
5568 |
stack add:end. |
|
5569 |
] |
|
5570 |
] |
|
5571 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5572 |
] |
2069 | 5573 |
! |
5574 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5575 |
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
|
5576 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5577 |
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
|
5578 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5579 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5580 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5581 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5582 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5583 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5584 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5585 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5586 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5587 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5588 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5589 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5590 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5591 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5592 |
[stack notEmpty] whileTrue:[ |
8874 | 5593 |
end := stack removeLast. |
5594 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5595 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5596 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 5597 |
elB := self at:begin. |
5598 |
elE := self at:end. |
|
5599 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
5600 |
temp := elB. elB := elE. elE := temp. |
|
5601 |
self at:begin put:elB. |
|
5602 |
self at:end put:elE. |
|
5603 |
]. |
|
5604 |
||
5605 |
nToSort > 2 ifTrue:[ |
|
5606 |
m := (begin + end) // 2. |
|
5607 |
elM := self at:m. |
|
5608 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
5609 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
5610 |
temp := self at:end. |
|
5611 |
self at:end put:(self at:m). |
|
5612 |
self at:m put:temp. |
|
5613 |
elM := elE |
|
5614 |
] |
|
5615 |
] ifFalse:[ |
|
5616 |
temp := self at:begin. |
|
5617 |
self at:begin put:(self at:m). |
|
5618 |
self at:m put:temp. |
|
5619 |
elM := elB |
|
5620 |
]. |
|
5621 |
nToSort > 3 ifTrue:[ |
|
5622 |
bRun := begin. |
|
5623 |
eRun := end. |
|
5624 |
[ |
|
5625 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
5626 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
5627 |
bRun <= eRun |
|
5628 |
] whileTrue:[ |
|
5629 |
temp := self at:bRun. |
|
5630 |
self at:bRun put:(self at:eRun). |
|
5631 |
self at:eRun put:temp. |
|
5632 |
]. |
|
5633 |
stack add:begin. |
|
5634 |
stack add:eRun. |
|
5635 |
||
5636 |
stack add:bRun. |
|
5637 |
stack add:end. |
|
5638 |
] |
|
5639 |
] |
|
5640 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5641 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5642 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5643 |
|
2069 | 5644 |
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
|
5645 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5646 |
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
|
5647 |
with degenerated collections." |
2069 | 5648 |
|
5649 |
|begin "{ Class: SmallInteger }" |
|
5650 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5651 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5652 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5653 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5654 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5655 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5656 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5657 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5658 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5659 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5660 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5661 |
[stack notEmpty] whileTrue:[ |
8874 | 5662 |
end := stack removeLast. |
5663 |
begin := stack removeLast. |
|
5664 |
||
5665 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5666 |
||
5667 |
elB := self at:begin. |
|
5668 |
elE := self at:end. |
|
5669 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5670 |
temp := elB. elB := elE. elE := temp. |
|
5671 |
self at:begin put:elB. |
|
5672 |
self at:end put:elE. |
|
5673 |
||
5674 |
temp := aCollection at:end. |
|
5675 |
aCollection at:end put:(aCollection at:begin). |
|
5676 |
aCollection at:begin put:temp. |
|
5677 |
]. |
|
5678 |
||
5679 |
nToSort > 2 ifTrue:[ |
|
5680 |
m := (begin + end) // 2. |
|
5681 |
elM := self at:m. |
|
5682 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5683 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5684 |
temp := self at:end. |
|
5685 |
self at:end put:(self at:m). |
|
5686 |
self at:m put:temp. |
|
5687 |
||
5688 |
temp := aCollection at:end. |
|
5689 |
aCollection at:end put:(aCollection at:m). |
|
5690 |
aCollection at:m put:temp. |
|
5691 |
||
5692 |
elM := elE |
|
5693 |
] |
|
5694 |
] ifFalse:[ |
|
5695 |
temp := self at:begin. |
|
5696 |
self at:begin put:(self at:m). |
|
5697 |
self at:m put:temp. |
|
5698 |
||
5699 |
temp := aCollection at:begin. |
|
5700 |
aCollection at:begin put:(aCollection at:m). |
|
5701 |
aCollection at:m put:temp. |
|
5702 |
elM := elB |
|
5703 |
]. |
|
5704 |
nToSort > 3 ifTrue:[ |
|
5705 |
bRun := begin. |
|
5706 |
eRun := end. |
|
5707 |
[ |
|
5708 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5709 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5710 |
bRun <= eRun |
|
5711 |
] whileTrue:[ |
|
5712 |
temp := self at:bRun. |
|
5713 |
self at:bRun put:(self at:eRun). |
|
5714 |
self at:eRun put:temp. |
|
5715 |
||
5716 |
temp := aCollection at:bRun. |
|
5717 |
aCollection at:bRun put:(aCollection at:eRun). |
|
5718 |
aCollection at:eRun put:temp. |
|
5719 |
]. |
|
5720 |
stack add:begin. |
|
5721 |
stack add:eRun. |
|
5722 |
||
5723 |
stack add:bRun. |
|
5724 |
stack add:end. |
|
5725 |
] |
|
5726 |
] |
|
5727 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5728 |
] |
2069 | 5729 |
! |
5730 |
||
5731 |
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
|
5732 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5733 |
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
|
5734 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5735 |
|
8874 | 5736 |
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
|
5737 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5738 |
|
5739 |
|begin "{ Class: SmallInteger }" |
|
5740 |
end "{ Class: SmallInteger }" |
|
5741 |
b "{ Class: SmallInteger }" |
|
5742 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5743 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5744 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5745 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5746 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5747 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5748 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5749 |
[stack notEmpty] whileTrue:[ |
8874 | 5750 |
end := stack removeLast. |
5751 |
begin := stack removeLast. |
|
5752 |
||
5753 |
b := begin. |
|
5754 |
e := end. |
|
5755 |
middleElement := self at:((b + e) // 2). |
|
5756 |
||
5757 |
[b < e] whileTrue:[ |
|
5758 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5759 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5760 |
||
5761 |
(b <= e) ifTrue:[ |
|
5762 |
(b == e) ifFalse:[ |
|
5763 |
temp1 := self at:b. temp2 := self at:e. |
|
5764 |
self at:b put:temp2. self at:e put:temp1. |
|
5765 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5766 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5767 |
]. |
|
5768 |
b := b + 1. |
|
5769 |
e := e - 1 |
|
5770 |
] |
|
5771 |
]. |
|
5772 |
(begin < e) ifTrue:[ |
|
5773 |
stack add:begin. |
|
5774 |
stack add:e. |
|
5775 |
]. |
|
5776 |
(b < end) ifTrue:[ |
|
5777 |
stack add:b. |
|
5778 |
stack add:end. |
|
5779 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5780 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5781 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5782 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5783 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5784 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5785 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5786 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5787 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5788 |
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
|
5789 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5790 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5791 |
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
|
5792 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5793 |
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
|
5794 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5795 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5796 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5797 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5798 |
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
|
5799 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5800 |
"/ 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
|
5801 |
^ 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
|
5802 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5803 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5804 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5805 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5806 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5807 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5808 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5809 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5810 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5811 |
"/ 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
|
5812 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5813 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5814 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5815 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5816 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5817 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5818 |
"/ "/ 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
|
5819 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5820 |
"/ 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
|
5821 |
"/ 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
|
5822 |
"/ 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
|
5823 |
"/ aCollection notNil ifTrue:[ |
8874 | 5824 |
"/ 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
|
5825 |
"/ 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
|
5826 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5827 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5828 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5829 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5830 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5831 |
"/ 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
|
5832 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5833 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5834 |
"/ [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
|
5835 |
"/ [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
|
5836 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5837 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5838 |
"/ (b == e) ifFalse:[ |
8874 | 5839 |
"/ 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
|
5840 |
"/ 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
|
5841 |
"/ aCollection notNil ifTrue:[ |
8874 | 5842 |
"/ 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
|
5843 |
"/ 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
|
5844 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5845 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5846 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5847 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5848 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5849 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5850 |
"/ (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
|
5851 |
"/ (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
|
5852 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5853 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5854 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5855 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5856 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5857 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5858 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5859 |
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
|
5860 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5861 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5862 |
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
|
5863 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5864 |
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
|
5865 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5866 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5867 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5868 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5869 |
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
|
5870 |
"/ 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
|
5871 |
^ 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
|
5872 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5873 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5874 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5875 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5876 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5877 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5878 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5879 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5880 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5881 |
"/ 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
|
5882 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5883 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5884 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5885 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5886 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5887 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5888 |
"/ "/ 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
|
5889 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5890 |
"/ 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
|
5891 |
"/ 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
|
5892 |
"/ 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
|
5893 |
"/ aCollection notNil ifTrue:[ |
8874 | 5894 |
"/ 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
|
5895 |
"/ 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
|
5896 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5897 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5898 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5899 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5900 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5901 |
"/ 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
|
5902 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5903 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5904 |
"/ [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
|
5905 |
"/ [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
|
5906 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5907 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5908 |
"/ (b == e) ifFalse:[ |
8874 | 5909 |
"/ 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
|
5910 |
"/ 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
|
5911 |
"/ aCollection notNil ifTrue:[ |
8874 | 5912 |
"/ 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
|
5913 |
"/ 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
|
5914 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5915 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5916 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5917 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5918 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5919 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5920 |
"/ (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
|
5921 |
"/ (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
|
5922 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5923 |
"/ "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
|
5924 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5925 |
! ! |
5926 |
||
602 | 5927 |
!SequenceableCollection methodsFor:'queries'! |
5928 |
||
5929 |
firstIndex |
|
5930 |
"return the first elements index" |
|
5931 |
||
5932 |
^ 1 |
|
5933 |
! |
|
5934 |
||
5935 |
isSequenceable |
|
5936 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5937 |
||
5938 |
^ true |
|
1 | 5939 |
! |
5940 |
||
602 | 5941 |
isSequenceableCollection |
5942 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5943 |
This method is a historic leftover and will be removed." |
|
5944 |
||
5872 | 5945 |
<resource:#obsolete> |
5946 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5947 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5948 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5949 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5950 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5951 |
! |
5952 |
||
5358 | 5953 |
isSorted |
5954 |
"return true. if my elements are sorted (already)" |
|
5955 |
||
5956 |
|lastEl el| |
|
5957 |
||
5958 |
lastEl := self first. |
|
5959 |
2 to:self size do:[:i | |
|
8874 | 5960 |
el := self at:i. |
5961 |
el >= lastEl ifFalse: [^ false]. |
|
5962 |
lastEl := el |
|
5358 | 5963 |
]. |
5964 |
^ true |
|
5965 |
||
5966 |
" |
|
8874 | 5967 |
#(1 2 3 5 10 100) isSorted |
5968 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5969 |
" |
5970 |
! |
|
5971 |
||
5972 |
isSortedBy:aBlock |
|
5973 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5974 |
||
5975 |
|lastEl el| |
|
5976 |
||
5977 |
lastEl := self first. |
|
5978 |
2 to:self size do:[:i | |
|
8874 | 5979 |
el := self at:i. |
5980 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5981 |
lastEl := el |
|
5358 | 5982 |
]. |
5983 |
^ true |
|
5984 |
||
5985 |
" |
|
8874 | 5986 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5987 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5988 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5989 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5990 |
" |
5991 |
||
5992 |
! |
|
5993 |
||
602 | 5994 |
keys |
5995 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5996 |
||
5997 |
|sz| |
|
5998 |
||
1 | 5999 |
sz := self size. |
602 | 6000 |
sz == 0 ifTrue:[ |
8874 | 6001 |
^ #() |
61 | 6002 |
]. |
602 | 6003 |
^ 1 to:sz |
1 | 6004 |
! |
6005 |
||
602 | 6006 |
lastIndex |
6007 |
"return the last elements index" |
|
6008 |
||
6009 |
^ self size |
|
61 | 6010 |
! |
6011 |
||
602 | 6012 |
size |
6013 |
"return the number of elements in the collection. |
|
6014 |
concrete implementations must define this" |
|
6015 |
||
6016 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6017 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6018 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6019 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6020 |
"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
|
6021 |
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
|
6022 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6023 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6024 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6025 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6026 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 6027 |
! ! |
6028 |
||
6029 |
!SequenceableCollection methodsFor:'searching'! |
|
6030 |
||
3887 | 6031 |
detect:aBlock startingAt:startIndex |
6032 |
"find the first element, for which evaluation of the argument, aBlock |
|
6033 |
return true, starting the search with startIndex. |
|
6034 |
If none does so, report an error" |
|
6035 |
||
6036 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
6037 |
||
6038 |
" |
|
8874 | 6039 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
6040 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 6041 |
" |
6042 |
||
6043 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
6044 |
! |
|
6045 |
||
6046 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
6047 |
"find the first element, for which evaluation of the argument, aBlock |
|
6048 |
return true, starting the search with startIndex. |
|
5717 | 6049 |
If none does so, return the evaluation of exceptionBlock" |
6050 |
||
6051 |
|stop| |
|
3887 | 6052 |
|
6053 |
stop := self size. |
|
5717 | 6054 |
self from:startIndex to:stop do:[:el | |
8874 | 6055 |
(aBlock value:el) ifTrue:[ |
6056 |
^ el |
|
6057 |
]. |
|
3887 | 6058 |
]. |
6059 |
^ exceptionBlock value |
|
6060 |
||
6061 |
" |
|
8874 | 6062 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
6063 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 6064 |
" |
6065 |
||
6066 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
6067 |
! |
|
6068 |
||
11705 | 6069 |
findFirst:aBlock ifNone:exceptionalValue |
10453 | 6070 |
"find the index of the first element, for which evaluation of the argument, aBlock |
11705 | 6071 |
returns true; return its index or the value from exceptionalValue if none detected. |
3887 | 6072 |
This is much like #detect, however, here an INDEX is returned, |
6073 |
while #detect returns the element." |
|
602 | 6074 |
|
6075 |
|stop "{ Class: SmallInteger }" | |
|
6076 |
||
32 | 6077 |
stop := self size. |
602 | 6078 |
1 to:stop do:[:index | |
10453 | 6079 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 6080 |
]. |
11705 | 6081 |
^ exceptionalValue value |
6082 |
||
6083 |
" |
|
6084 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
6085 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
6086 |
" |
|
1 | 6087 |
! |
6088 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6089 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6090 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 6091 |
returns true; return its index or 0 if none detected. |
6092 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
6093 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6094 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6095 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6096 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6097 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6098 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6099 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6100 |
start to:stop do:[:index | |
8874 | 6101 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6102 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6103 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6104 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6105 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6106 |
#(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
|
6107 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6108 |
" |
3887 | 6109 |
|
6110 |
"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
|
6111 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6112 |
|
602 | 6113 |
findLast:aBlock |
6114 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 6115 |
returns true. |
6116 |
Return its index or 0 if none detected." |
|
602 | 6117 |
|
6118 |
|start "{ Class: SmallInteger }"| |
|
6119 |
||
6120 |
start := self size. |
|
6121 |
start to:1 by:-1 do:[:index | |
|
8874 | 6122 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 6123 |
]. |
6124 |
^ 0 |
|
6125 |
||
6126 |
" |
|
602 | 6127 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
6128 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 6129 |
" |
6130 |
! |
|
6131 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6132 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6133 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 6134 |
returns true. Start the search at startIndex. |
6135 |
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
|
6136 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6137 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6138 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6139 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6140 |
start to:1 by:-1 do:[:index | |
8874 | 6141 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6142 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6143 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6144 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6145 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6146 |
#(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
|
6147 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6148 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6149 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6150 |
|
8193 | 6151 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
6152 |
"find the last element, for which evaluation of the argument, aBlock |
|
6153 |
returns true. Start the search at startIndex. |
|
6154 |
End the search at endIndex or when an element is found. |
|
6155 |
Return its index or 0 if none detected." |
|
6156 |
||
6157 |
|start "{ Class: SmallInteger }"| |
|
6158 |
||
6159 |
start := startIndex. |
|
6160 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 6161 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 6162 |
]. |
6163 |
^ 0 |
|
6164 |
||
6165 |
" |
|
6166 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
6167 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
6168 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
6169 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
6170 |
" |
|
6171 |
! |
|
6172 |
||
159 | 6173 |
indexOfAny:aCollection |
6174 |
"search the collection for an element in aCollection. |
|
6175 |
if found, return the index otherwise return 0. |
|
8874 | 6176 |
The comparison is done using = |
159 | 6177 |
(i.e. equality test - not identity test). |
6178 |
||
6179 |
Notice, that for big collections, the runtime of this search |
|
6180 |
grows proportional to size(receiver) * size(aCollection). |
|
6181 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6182 |
||
6183 |
^ self indexOfAny:aCollection startingAt:1 |
|
6184 |
||
6185 |
" |
|
6186 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
6187 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
6188 |
" |
|
6189 |
! |
|
6190 |
||
6191 |
indexOfAny:aCollection startingAt:start |
|
6192 |
"search the collection for an element in aCollection, |
|
6193 |
starting the search at index start; |
|
6194 |
if found, return the index otherwise return 0. |
|
8874 | 6195 |
The comparison is done using = |
159 | 6196 |
(i.e. equality test - not identity test). |
6197 |
||
6198 |
Notice, that for big collections, the runtime of this search |
|
6199 |
grows proportional to size(receiver) * size(aCollection). |
|
6200 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6201 |
||
6202 |
||
6203 |
|startIndex "{ Class: SmallInteger }" |
|
6204 |
stop "{ Class: SmallInteger }" | |
|
6205 |
||
6206 |
startIndex := start. |
|
6207 |
stop := self size. |
|
6208 |
startIndex to:stop do:[:index | |
|
8874 | 6209 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 6210 |
]. |
6211 |
^ 0 |
|
6212 |
" |
|
8874 | 6213 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
6214 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 6215 |
" |
1 | 6216 |
! |
6217 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6218 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6219 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6220 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6221 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 6222 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6223 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6224 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6225 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6226 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6227 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6228 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6229 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6230 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6231 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6232 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6233 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6234 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6235 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6236 |
" |
8874 | 6237 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
6238 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
6239 |
#(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
|
6240 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6241 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6242 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6243 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6244 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6245 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6246 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6247 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6248 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6249 |
#(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
|
6250 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) |
13252
92b2e4de2ba5
comment/format in: #indexOfAnyOf:
Claus Gittinger <cg@exept.de>
parents:
13202
diff
changeset
|
6251 |
'abcdefg' indexOfAnyOf:(CharacterSet newFrom:'cef') |
92b2e4de2ba5
comment/format in: #indexOfAnyOf:
Claus Gittinger <cg@exept.de>
parents:
13202
diff
changeset
|
6252 |
" |
92b2e4de2ba5
comment/format in: #indexOfAnyOf:
Claus Gittinger <cg@exept.de>
parents:
13202
diff
changeset
|
6253 |
|
92b2e4de2ba5
comment/format in: #indexOfAnyOf:
Claus Gittinger <cg@exept.de>
parents:
13202
diff
changeset
|
6254 |
"Modified: / 28-01-2011 / 18:03:14 / cg" |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6255 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6256 |
|
8874 | 6257 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6258 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6259 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6260 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6261 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6262 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6263 |
#(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
|
6264 |
#(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
|
6265 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6266 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6267 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6268 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6269 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6270 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6271 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6272 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6273 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6274 |
|
368 | 6275 |
indexOfSubCollection:aCollection |
9234 | 6276 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 6277 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6278 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 6279 |
|
6280 |
" |
|
8874 | 6281 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
6282 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
6283 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
6284 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
6285 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
6286 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
6287 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
6288 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
13345
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
6289 |
'foobarbaz' indexOfSubCollection:'bar' |
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
6290 |
" |
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
6291 |
|
f3e950c4cb98
comment/format in: #indexOfSubCollection:
Claus Gittinger <cg@exept.de>
parents:
13252
diff
changeset
|
6292 |
"Modified: / 20-04-2011 / 19:11:20 / cg" |
368 | 6293 |
! |
6294 |
||
602 | 6295 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
6296 |
"find a subcollection. If found, return the index; |
|
6297 |
if not found, return the result of evaluating exceptionBlock." |
|
6298 |
||
6299 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
6300 |
! |
|
6301 |
||
368 | 6302 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6303 |
"find a subcollection starting at index. |
368 | 6304 |
If found, return the index; if not found, return 0." |
6305 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6306 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 6307 |
! |
6308 |
||
6309 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
6310 |
"find a subcollection, starting at index. If found, return the index; |
|
6311 |
if not found, return the result of evaluating exceptionBlock. |
|
6312 |
This is a q&d hack - not very efficient" |
|
6313 |
||
8874 | 6314 |
|same first |
368 | 6315 |
mySize "{Class: SmallInteger }" |
6316 |
checkIndex "{Class: SmallInteger }" |
|
6317 |
cmpIndex "{Class: SmallInteger }" |
|
6318 |
sz "{Class: SmallInteger }"| |
|
6319 |
||
6320 |
mySize := self size. |
|
6321 |
sz := aCollection size. |
|
6322 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
6323 |
first := aCollection at:1. |
|
6324 |
||
8874 | 6325 |
checkIndex := startIndex - 1. |
368 | 6326 |
[true] whileTrue:[ |
8874 | 6327 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
6328 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6329 |
||
6330 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
6331 |
^ 0 |
|
6332 |
]. |
|
6333 |
same := true. |
|
6334 |
cmpIndex := 1. |
|
6335 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6336 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
6337 |
same := false |
|
6338 |
] ifTrue:[ |
|
6339 |
cmpIndex := cmpIndex + 1 |
|
6340 |
] |
|
6341 |
]. |
|
6342 |
same ifTrue:[^ checkIndex]. |
|
6343 |
] |
|
6344 |
||
6345 |
" |
|
6346 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
6347 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
6348 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
6349 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
6350 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
6351 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 6352 |
" |
602 | 6353 |
! |
6354 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6355 |
lastIndexOfSubCollection:aCollection |
8874 | 6356 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6357 |
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
|
6358 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6359 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6360 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6361 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6362 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6363 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6364 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6365 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6366 |
"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
|
6367 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6368 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6369 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6370 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6371 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6372 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6373 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6374 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6375 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6376 |
"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
|
6377 |
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
|
6378 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6379 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6380 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6381 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6382 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6383 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6384 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6385 |
"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
|
6386 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6387 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6388 |
|
8874 | 6389 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6390 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6391 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6392 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6393 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6394 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6395 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6396 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6397 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6398 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6399 |
|
8874 | 6400 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6401 |
checkIndex > mySize ifTrue:[ |
8874 | 6402 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6403 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6404 |
[true] whileTrue:[ |
8874 | 6405 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
6406 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6407 |
||
6408 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
6409 |
^ 0 |
|
6410 |
]. |
|
6411 |
same := true. |
|
6412 |
cmpIndex := 1. |
|
6413 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6414 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
6415 |
same := false |
|
6416 |
] ifTrue:[ |
|
6417 |
cmpIndex := cmpIndex + 1 |
|
6418 |
] |
|
6419 |
]. |
|
6420 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
6421 |
] |
|
6422 |
||
6423 |
" |
|
6424 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
6425 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
6426 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
6427 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
6428 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
6429 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
6430 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
6431 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
6432 |
#(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
|
6433 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6434 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6435 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6436 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6437 |
|
7264 | 6438 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6439 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6440 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6441 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6442 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6443 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6444 |
|
10194 | 6445 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6446 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6447 |
" |
8874 | 6448 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6449 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6450 |
returns: true - if found |
10194 | 6451 |
false - if not found |
8874 | 6452 |
" |
6453 |
||
6454 |
" |
|
6455 |
#(10 20 30 40 50 60 70) includes:99 |
|
6456 |
#(10 20 30 40 50 60 70) includes:40 |
|
6457 |
#(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
|
6458 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6459 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6460 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6461 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6462 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6463 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6464 |
"search the collection for anElement; |
602 | 6465 |
if found, return the index otherwise return 0. |
8874 | 6466 |
The comparison is done using = |
602 | 6467 |
(i.e. equality test - not identity test)." |
6468 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6469 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6470 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6471 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6472 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6473 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6474 |
returns: elementIndex - if found |
8874 | 6475 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6476 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6477 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6478 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6479 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6480 |
#(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
|
6481 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6482 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6483 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6484 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6485 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6486 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6487 |
"search the collection for anElement; |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6488 |
if found, return the index; otherwise return the value of the exceptionBlock. |
8874 | 6489 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6490 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6491 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6492 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6493 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6494 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6495 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6496 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6497 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6498 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6499 |
args: anElement : <object> |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6500 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6501 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6502 |
returns: elementIndex - if found |
12037
fa963aa96dcd
comment/format in: #indexOf:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
12007
diff
changeset
|
6503 |
exceptionBlock value - if not found |
8874 | 6504 |
" |
6505 |
||
6506 |
" |
|
6507 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
6508 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
6509 |
#(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
|
6510 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6511 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6512 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6513 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6514 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6515 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 6516 |
"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
|
6517 |
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
|
6518 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6519 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6520 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6521 |
|idx| |
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 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6524 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 6525 |
self at:idx put:replacement. |
6526 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6527 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6528 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6529 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6530 |
" |
8874 | 6531 |
args: elementToFind : <object> |
6532 |
replacement : <integer> |
|
6533 |
start : <integer> |
|
6534 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6535 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6536 |
returns: elementIndex - if found (and replaced) |
8874 | 6537 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6538 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6539 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6540 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6541 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6542 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6543 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6544 |
(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
|
6545 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6546 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6547 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6548 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6549 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6550 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6551 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6552 |
"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
|
6553 |
if found, return the index otherwise return 0. |
8874 | 6554 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6555 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6556 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6557 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6558 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6559 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6560 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6561 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6562 |
startIndex to:stop do:[:index | |
8874 | 6563 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6564 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6565 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6566 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6567 |
" |
8874 | 6568 |
args: anElement : <object> |
6569 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6570 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6571 |
returns: elementIndex - if found |
8874 | 6572 |
0 - if not found |
6573 |
" |
|
6574 |
||
6575 |
" |
|
6576 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
6577 |
#(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
|
6578 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6579 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6580 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6581 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6582 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6583 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6584 |
"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
|
6585 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6586 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6587 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6588 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6589 |
|
602 | 6590 |
|startIndex "{ Class: SmallInteger }" |
6591 |
stopIndex "{ Class: SmallInteger }" | |
|
6592 |
||
6593 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6594 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6595 |
startIndex to:stopIndex do:[:index | |
8874 | 6596 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6597 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6598 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6599 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6600 |
" |
8874 | 6601 |
args: anElement : <object> |
6602 |
start : <integer> |
|
6603 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6604 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6605 |
returns: elementIndex - if found |
8874 | 6606 |
exceptionBlock value - if not found |
6607 |
" |
|
6608 |
||
6609 |
" |
|
6610 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
6611 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
6612 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
6613 |
#(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
|
6614 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6615 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6616 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6617 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6618 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6619 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6620 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6621 |
"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
|
6622 |
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
|
6623 |
exceptionBlock. |
8874 | 6624 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6625 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6626 |
|
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 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6630 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6631 |
^ index |
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 |
" |
8874 | 6634 |
args: anElement : <object> |
6635 |
start : <integer> |
|
6636 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6637 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6638 |
returns: elementIndex - if found |
8874 | 6639 |
exceptionBlock value - if not found |
6640 |
" |
|
6641 |
||
6642 |
" |
|
6643 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
6644 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
6645 |
#(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
|
6646 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6647 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6648 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6649 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6650 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6651 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6652 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6653 |
if found, return the index otherwise return 0. |
8874 | 6654 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6655 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6656 |
|
8874 | 6657 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6658 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6659 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6660 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6661 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6662 |
returns: elementIndex - if found |
8874 | 6663 |
0 - if not found |
6664 |
" |
|
6665 |
||
6666 |
" |
|
6667 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
6668 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
6669 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
6670 |
#(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
|
6671 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6672 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6673 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6674 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6675 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6676 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6677 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6678 |
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
|
6679 |
exceptionBlock. |
8874 | 6680 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6681 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6682 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6683 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6684 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6685 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6686 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6687 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6688 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6689 |
" |
8874 | 6690 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
6691 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
6692 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
6693 |
#(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
|
6694 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6695 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6696 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6697 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6698 |
"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
|
6699 |
if found, return the index otherwise return 0. |
8874 | 6700 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6701 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6702 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6703 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6704 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6705 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6706 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6707 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 6708 |
]. |
6709 |
^ 0 |
|
6710 |
||
6711 |
" |
|
8874 | 6712 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
6713 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
6714 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
6715 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
6716 |
#(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
|
6717 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6718 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6719 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6720 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6721 |
"search the collection backwards for anElement starting the search at |
6722 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6723 |
otherwise return the value of the exceptionBlock. |
8874 | 6724 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6725 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6726 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6727 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6728 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6729 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6730 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6731 |
^ index |
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 |
" |
8874 | 6734 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
6735 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6736 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6737 |
#(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
|
6738 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6739 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6740 |
|
4788 | 6741 |
lastIndexOfAny:aCollection |
6742 |
"search the collection backwards for any in aCollection; |
|
6743 |
if found, return the index, otherwise return 0. |
|
8874 | 6744 |
The comparison is done using = |
4788 | 6745 |
(i.e. equality test - not identity test)." |
6746 |
||
8874 | 6747 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 6748 |
|
6749 |
" |
|
6750 |
args: aCollection : <collection> |
|
6751 |
||
6752 |
returns: elementIndex - if found |
|
8874 | 6753 |
0 - if not found |
6754 |
" |
|
6755 |
||
6756 |
" |
|
6757 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 6758 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 6759 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
6760 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 6761 |
" |
6762 |
||
6763 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
6764 |
! |
|
6765 |
||
6766 |
lastIndexOfAny:aCollection startingAt:start |
|
6767 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
6768 |
if found, return the index, otherwise return 0. |
|
8874 | 6769 |
The comparison is done using = |
4788 | 6770 |
(i.e. equality test - not identity test)." |
6771 |
||
6772 |
|startIndex "{ Class: SmallInteger }"| |
|
6773 |
||
6774 |
startIndex := start. |
|
6775 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 6776 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 6777 |
]. |
6778 |
^ 0 |
|
6779 |
||
6780 |
" |
|
8874 | 6781 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
6782 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 6783 |
" |
6784 |
! |
|
6785 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6786 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6787 |
"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
|
6788 |
stopping at:stop; |
13719 | 6789 |
if found, return the index otherwise return 0. |
8874 | 6790 |
The comparison is done using = |
13719 | 6791 |
(i.e. equality test - not identity test)." |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6792 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6793 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6794 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6795 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6796 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6797 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6798 |
startIndex to:stop do:[:index | |
13719 | 6799 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6800 |
]. |
13719 | 6801 |
^ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6802 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6803 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6804 |
args: anElement : <object> |
13719 | 6805 |
start : <integer> |
6806 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6807 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6808 |
returns: elementIndex - if found |
13719 | 6809 |
nil - if not found |
8874 | 6810 |
" |
6811 |
||
6812 |
" |
|
6813 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
6814 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
6815 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 6816 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6817 |
|
13719 | 6818 |
"Modified: / 23-09-2011 / 14:03:05 / cg" |
602 | 6819 |
! |
6820 |
||
6821 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6822 |
"search the collection for anElement, starting the search at index start |
|
6823 |
and stopping at stop; |
|
6824 |
if found, return the index otherwise return the value of the |
|
6825 |
exceptionBlock. |
|
8874 | 6826 |
The comparison is done using = |
602 | 6827 |
(i.e. equality test - not identity test)." |
6828 |
||
6829 |
|index| |
|
6830 |
||
6831 |
index := self nextIndexOf:anElement from:start to:stop. |
|
13719 | 6832 |
index == 0 ifTrue:[^ exceptionBlock value]. |
602 | 6833 |
^ index |
6834 |
||
6835 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6836 |
args: anElement : <object> |
13719 | 6837 |
start : <integer> |
6838 |
stop : <integer> |
|
6839 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6840 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6841 |
returns: elementIndex - if found |
13719 | 6842 |
valueImplementor value - if not found |
8874 | 6843 |
" |
6844 |
||
6845 |
" |
|
6846 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6847 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6848 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6849 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6850 |
|
13719 | 6851 |
"Modified: / 23-09-2011 / 14:03:36 / cg" |
2275 | 6852 |
! |
6853 |
||
6854 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6855 |
"search the collection for anElement, starting the search at index start; |
|
6856 |
ending at stop, going in reverse direction. |
|
13719 | 6857 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6858 |
The comparison is done using = |
13719 | 6859 |
(i.e. equality test - not identity test)." |
2275 | 6860 |
|
6861 |
|startIndex "{ Class: SmallInteger }" |
|
6862 |
stopIndex "{ Class: SmallInteger }" | |
|
6863 |
||
6864 |
startIndex := startSearchIndex. |
|
6865 |
stopIndex := endSearchIndex max:1. |
|
6866 |
startIndex to:stopIndex by:-1 do:[:index | |
|
13719 | 6867 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6868 |
]. |
13719 | 6869 |
^ 0 |
3468
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6872 |
args: anElement : <object> |
13719 | 6873 |
start : <integer> |
6874 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6875 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6876 |
returns: elementIndex - if found |
13719 | 6877 |
nil - if not found |
8874 | 6878 |
" |
6879 |
||
6880 |
" |
|
6881 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6882 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6883 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6884 |
" |
6885 |
||
13719 | 6886 |
"Modified: / 23-09-2011 / 14:02:36 / cg" |
2275 | 6887 |
! |
6888 |
||
6889 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6890 |
"search the collection for anElement, starting the search at index start |
|
6891 |
and stopping at stop, doing a reverse search; |
|
6892 |
if found, return the index otherwise return the value of the |
|
6893 |
exceptionBlock. |
|
8874 | 6894 |
The comparison is done using = |
2275 | 6895 |
(i.e. equality test - not identity test)." |
6896 |
||
6897 |
|index| |
|
6898 |
||
6899 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6900 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6901 |
^ index |
|
6902 |
||
6903 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6904 |
args: anElement : <object> |
8874 | 6905 |
start : <integer> |
6906 |
stop : <integer> |
|
6907 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6908 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6909 |
returns: elementIndex - if found |
8874 | 6910 |
valueImplementor value - if not found |
6911 |
" |
|
6912 |
||
6913 |
" |
|
6914 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6915 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6916 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6917 |
" |
6918 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6919 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6920 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6921 |
|
7264 | 6922 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6923 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6924 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6925 |
"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
|
6926 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6927 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6928 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6929 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6930 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6931 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6932 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6933 |
returns: elementIndex - if found |
8874 | 6934 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6935 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6936 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6937 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6938 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6939 |
#(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
|
6940 |
#(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
|
6941 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6942 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6943 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6944 |
#(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
|
6945 |
#(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
|
6946 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6947 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6948 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6949 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6950 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6951 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6952 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6953 |
"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
|
6954 |
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
|
6955 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6956 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6957 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6958 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6959 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6960 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6961 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6962 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6963 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6964 |
args: anElement : <object> |
8874 | 6965 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6966 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6967 |
returns: elementIndex - if found |
8874 | 6968 |
exceptionBlock value - if not found |
6969 |
" |
|
6970 |
||
6971 |
" |
|
6972 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6973 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6974 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6975 |
#(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
|
6976 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6977 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6978 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6979 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6980 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6981 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6982 |
"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
|
6983 |
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
|
6984 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6985 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6986 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6987 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6988 |
args: anElement : <object> |
8874 | 6989 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6990 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6991 |
returns: elementIndex - if found |
8874 | 6992 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6993 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6994 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6995 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6996 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6997 |
"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
|
6998 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6999 |
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
|
7000 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7001 |
|startIndex "{ Class: SmallInteger }" |
8874 | 7002 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7003 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7004 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7005 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7006 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7007 |
startIndex to:stop do:[:index | |
8874 | 7008 |
el := self at:index. |
7009 |
anElement == el ifTrue:[^ index]. |
|
7010 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7011 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7012 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7013 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7014 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7015 |
args: anElement : <object> |
8874 | 7016 |
anotherElement : <object> |
7017 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7018 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7019 |
returns: elementIndex - if found |
8874 | 7020 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7021 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7022 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7023 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7024 |
#(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
|
7025 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7026 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7027 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7028 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7029 |
"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
|
7030 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7031 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7032 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7033 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7034 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7035 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7036 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7037 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7038 |
startIndex to:stop do:[:index | |
8874 | 7039 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7040 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7041 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7042 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7043 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7044 |
args: anElement : <object> |
8874 | 7045 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7046 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7047 |
returns: elementIndex - if found |
8874 | 7048 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7049 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7050 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7051 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7052 |
#(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
|
7053 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 7054 |
#(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
|
7055 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7056 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7057 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7058 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7059 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7060 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7061 |
"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
|
7062 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7063 |
If found (within the range), return the index, otherwise return 0. |
8874 | 7064 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7065 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7066 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7067 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7068 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7069 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7070 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7071 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7072 |
startIndex to:stopIndex do:[:index | |
8874 | 7073 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7074 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7075 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7076 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7077 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7078 |
args: anElement : <object> |
8874 | 7079 |
start : <integer> |
7080 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7081 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7082 |
returns: elementIndex - if found |
8874 | 7083 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7084 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7085 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7086 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7087 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7088 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7089 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7090 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7091 |
"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
|
7092 |
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
|
7093 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7094 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7095 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7096 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7097 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7098 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7099 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7100 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7101 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7102 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7103 |
args: anElement : <object> |
8874 | 7104 |
start : <integer> |
7105 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7106 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7107 |
returns: elementIndex - if found |
8874 | 7108 |
exceptionBlock value - if not found |
7109 |
" |
|
7110 |
||
7111 |
" |
|
7112 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
7113 |
#(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
|
7114 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7115 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7116 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7117 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7118 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7119 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7120 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7121 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7122 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7123 |
|
10194 | 7124 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7125 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7126 |
" |
8874 | 7127 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7128 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7129 |
returns: true - if found |
10194 | 7130 |
false - if not found |
8874 | 7131 |
" |
7132 |
||
7133 |
" |
|
7134 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
7135 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
7136 |
#(10 20 30 40 50 60 70) includes:40 |
|
7137 |
#(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
|
7138 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7139 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7140 |
|
8874 | 7141 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
7142 |
#(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
|
7143 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7144 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7145 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7146 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7147 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7148 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7149 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7150 |
if found, return the index otherwise return 0. |
8874 | 7151 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7152 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7153 |
|
8874 | 7154 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7155 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7156 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7157 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7158 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7159 |
returns: elementIndex - if found |
8874 | 7160 |
0 - if not found |
7161 |
" |
|
7162 |
||
7163 |
" |
|
7164 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
7165 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
7166 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
7167 |
#(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
|
7168 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7169 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7170 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7171 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7172 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7173 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7174 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7175 |
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
|
7176 |
exceptionBlock. |
8874 | 7177 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7178 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7179 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7180 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7181 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7182 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7183 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7184 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7185 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7186 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7187 |
args: anElement : <object> |
8874 | 7188 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7189 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7190 |
returns: elementIndex - if found |
8874 | 7191 |
exceptionBlock value - if not found |
7192 |
" |
|
7193 |
||
7194 |
" |
|
7195 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
7196 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
7197 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
7198 |
#(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
|
7199 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7200 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7201 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7202 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7203 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7204 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7205 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7206 |
"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
|
7207 |
if found, return the index otherwise return 0. |
8874 | 7208 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7209 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7210 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7211 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7212 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7213 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7214 |
startIndex to:1 by:-1 do:[:index | |
8874 | 7215 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7216 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7217 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7218 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7219 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7220 |
args: anElement : <object> |
8874 | 7221 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7222 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7223 |
returns: elementIndex - if found |
8874 | 7224 |
0 - if not found |
7225 |
" |
|
7226 |
||
7227 |
" |
|
7228 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
7229 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
7230 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
7231 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
7232 |
#(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
|
7233 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7234 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7235 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7236 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7237 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7238 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 7239 |
"search the collection backwards for anElement starting the search at |
7240 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7241 |
otherwise return the value of the exceptionBlock. |
8874 | 7242 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7243 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7244 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7245 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7246 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7247 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7248 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7249 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7250 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7251 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7252 |
args: anElement : <object> |
8874 | 7253 |
start : <integer> |
7254 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7255 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7256 |
returns: elementIndex - if found |
8874 | 7257 |
exceptionBlock value - if not found |
7258 |
" |
|
7259 |
||
7260 |
" |
|
7261 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
7262 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
7263 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
7264 |
#(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
|
7265 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7266 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7267 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7268 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 7269 |
! ! |
7270 |
||
7271 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
7272 |
||
6958 | 7273 |
detectFirstInOrder:sortBlock |
7274 |
"find the first element of the collection sorted with sortBlock" |
|
7275 |
||
7276 |
|selectedForNow start| |
|
7277 |
||
7278 |
start := true. |
|
7279 |
self do:[:element| |
|
8874 | 7280 |
start ifTrue:[ |
7281 |
start := false. |
|
7282 |
selectedForNow := element. |
|
7283 |
] ifFalse:[ |
|
7284 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
7285 |
selectedForNow := element. |
|
7286 |
]. |
|
7287 |
]. |
|
6958 | 7288 |
]. |
7289 |
||
7290 |
^ selectedForNow |
|
7291 |
||
7292 |
" |
|
8874 | 7293 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
7294 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
7295 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
7296 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 7297 |
" |
7298 |
! |
|
7299 |
||
5358 | 7300 |
mergeSort |
8874 | 7301 |
"sort the collection using a mergeSort algorithm. |
7302 |
The elements are compared using'<' |
|
5358 | 7303 |
i.e. they should offer a magnitude-like protocol. |
7304 |
||
7305 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 7306 |
the best possible for all situations |
5358 | 7307 |
See also #quickSort and #randomizedSort for other sort algorithms |
7308 |
with different worst- and average case behavior)" |
|
7309 |
||
12888 | 7310 |
self mergeSort:[:a :b | a < b] |
5358 | 7311 |
|
7312 |
" |
|
7313 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
12888 | 7314 |
#(1 16 7 98 7 3 19 4 0) mergeSort |
5358 | 7315 |
|
7316 |
|data| |
|
7317 |
data := Random new next:100000. |
|
7318 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
7319 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 7320 |
data reverse. |
5358 | 7321 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
7322 |
||
7323 |
data := Random new next:100000. |
|
7324 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7325 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7326 |
data reverse. |
5358 | 7327 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7328 |
||
7329 |
data := Random new next:100000. |
|
7330 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
7331 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 7332 |
data reverse. |
5358 | 7333 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
7334 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7335 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7336 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7337 |
mergeSort:sortBlock |
8874 | 7338 |
"sort the collection using a mergeSort algorithm. |
7339 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7340 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7341 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7342 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 7343 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7344 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7345 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7346 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7347 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7348 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7349 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7350 |
(stop > 1) ifTrue:[ |
8874 | 7351 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7352 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7353 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7354 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7355 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7356 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7357 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7358 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7359 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7360 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7361 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7362 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7363 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7364 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7365 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7366 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 7367 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7368 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7369 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7370 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7371 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7372 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 7373 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7374 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7375 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7376 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7377 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7378 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 7379 |
"Sort the given range of indices using the mergesort algorithm. |
7380 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 7381 |
as many comparisons as heapsort or quicksort." |
7382 |
||
8874 | 7383 |
"Details: recursively split the range to be sorted into two halves, |
7384 |
mergesort each half, then merge the two halves together. |
|
7385 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
7386 |
and forth between the receiver and this copy. |
|
7387 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 7388 |
resulting in the receiver being completely sorted." |
7389 |
||
7390 |
| temp | |
|
7391 |
||
7392 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
7393 |
startIndex = stopIndex ifTrue: [^ self]. |
|
7394 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 7395 |
ifFalse: [self error: 'bad start index']. |
5358 | 7396 |
stopIndex <= self size |
8874 | 7397 |
ifFalse: [self error: 'bad stop index']. |
5358 | 7398 |
temp := self clone. |
7399 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
7400 |
||
7401 |
" |
|
8874 | 7402 |
#(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
|
7403 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7404 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7405 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7406 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7407 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7408 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7409 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7410 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7411 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7412 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7413 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7414 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7415 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7416 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7417 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 7418 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7419 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7420 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7421 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7422 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7423 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7424 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7425 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7426 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7427 |
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
|
7428 |
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
|
7429 |
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
|
7430 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7431 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7432 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7433 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7434 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7435 |
(stop > 1) ifTrue:[ |
8874 | 7436 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7437 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7438 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7439 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7440 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 7441 |
|
7442 |
|data| |
|
7443 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7444 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7445 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 7446 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7447 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7448 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7449 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7450 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7451 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7452 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7453 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7454 |
"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
|
7455 |
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
|
7456 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7457 |
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
|
7458 |
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
|
7459 |
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
|
7460 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7461 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7462 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7463 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7464 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7465 |
(stop > 1) ifTrue:[ |
8874 | 7466 |
sortBlock numArgs == 3 ifTrue:[ |
7467 |
"/ TODO: pass a collating policy to aBlock |
|
7468 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
7469 |
] ifFalse:[ |
|
7470 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
7471 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7472 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7473 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7474 |
" |
8874 | 7475 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7476 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7477 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7478 |
#(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
|
7479 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7480 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7481 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7482 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7483 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7484 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7485 |
"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
|
7486 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7487 |
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
|
7488 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7489 |
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
|
7490 |
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
|
7491 |
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
|
7492 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7493 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7494 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7495 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7496 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7497 |
(stop > 1) ifTrue:[ |
8874 | 7498 |
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
|
7499 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7500 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7501 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7502 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7503 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7504 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7505 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7506 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7507 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7508 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7509 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7510 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7511 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7512 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7513 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7514 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7515 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7516 |
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
|
7517 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7518 |
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
|
7519 |
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
|
7520 |
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
|
7521 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7522 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7523 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7524 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7525 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7526 |
(stop > 1) ifTrue:[ |
8874 | 7527 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7528 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7529 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7530 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 7531 |
! |
7532 |
||
11272 | 7533 |
randomShuffle |
7534 |
"random shuffle my elements in place. |
|
7535 |
This is a destructive algorithm. |
|
7536 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
7537 |
See 'The Art of Computer Programming'." |
|
7538 |
||
7539 |
self size to:2 by:-1 do:[:endIndex | |
|
7540 |
|rndIndex t| |
|
7541 |
||
7542 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
7543 |
"/ exchange |
|
7544 |
t := self at:rndIndex. |
|
7545 |
self at:rndIndex put:(self at:endIndex). |
|
7546 |
self at:endIndex put:t. |
|
7547 |
]. |
|
7548 |
||
7549 |
" |
|
7550 |
(1 to:10) asOrderedCollection randomShuffle |
|
7551 |
||
7552 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7553 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7554 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7555 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7556 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7557 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7558 |
" |
|
7559 |
||
11273 | 7560 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
7561 |
||
7562 |
|a score| |
|
7563 |
||
7564 |
a := 'abcd'. |
|
7565 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
7566 |
||
7567 |
1000000 times do:[ |
|
7568 |
|sorted| |
|
7569 |
sorted := a copy randomShuffle. |
|
7570 |
score at:sorted put:(score at:sorted)+1. |
|
7571 |
]. |
|
7572 |
||
7573 |
score keys asSortedCollection do:[:k | |
|
7574 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
7575 |
]. |
|
7576 |
" |
|
7577 |
||
7578 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
11272 | 7579 |
! |
7580 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7581 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7582 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7583 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7584 |
|
8874 | 7585 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7586 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7587 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7588 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7589 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7590 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7591 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7592 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7593 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7594 |
"/ 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
|
7595 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7596 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7597 |
"/ 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
|
7598 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7599 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7600 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7601 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7602 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7603 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7604 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7605 |
|
8874 | 7606 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7607 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7608 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7609 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7610 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7611 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7612 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7613 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7614 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7615 |
"/ 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
|
7616 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7617 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7618 |
"/ 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
|
7619 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7620 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7621 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7622 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7623 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7624 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7625 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7626 |
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
|
7627 |
|
8874 | 7628 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7629 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7630 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7631 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7632 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7633 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7634 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7635 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7636 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7637 |
"/ 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
|
7638 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7639 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7640 |
"/ 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
|
7641 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7642 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7643 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7644 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7645 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7646 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7647 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7648 |
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
|
7649 |
|
8874 | 7650 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7651 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7652 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7653 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7654 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7655 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7656 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7657 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7658 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7659 |
"/ 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
|
7660 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7661 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7662 |
"/ 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
|
7663 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7664 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7665 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7666 |
|
602 | 7667 |
reverse |
7335 | 7668 |
"destructively reverse the order of the elements inplace. |
11264 | 7669 |
WARNING: this is a destructive operation, which modifies the receiver. |
7670 |
Please use reversed (with a d) for a functional version." |
|
602 | 7671 |
|
7672 |
|lowIndex "{ Class: SmallInteger }" |
|
7673 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7674 |
t1 t2| |
602 | 7675 |
|
7676 |
hiIndex := self size. |
|
7677 |
lowIndex := 1. |
|
7678 |
[lowIndex < hiIndex] whileTrue:[ |
|
11264 | 7679 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
7680 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
7681 |
||
7682 |
lowIndex := lowIndex + 1. |
|
7683 |
hiIndex := hiIndex - 1 |
|
602 | 7684 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7685 |
|
69 | 7686 |
" |
602 | 7687 |
#(4 5 6 7 7) reverse |
7688 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 7689 |
" |
1 | 7690 |
! |
7691 |
||
5004 | 7692 |
reversed |
7693 |
"return a copy with elements in reverse order" |
|
7694 |
||
7695 |
^ self copy reverse |
|
7696 |
||
7697 |
" |
|
8874 | 7698 |
#(4 5 6 7 7) reversed |
5004 | 7699 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
7700 |
" |
|
7701 |
! |
|
7702 |
||
1 | 7703 |
sort |
7704 |
"sort the collection inplace. The elements are compared using |
|
2069 | 7705 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7706 |
|
159 | 7707 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7708 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7709 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7710 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 7711 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7712 |
self quickSort |
1 | 7713 |
|
95 | 7714 |
" |
7715 |
#(1 16 7 98 3 19 4 0) sort |
|
7716 |
||
7717 |
|data| |
|
7718 |
data := Random new next:100000. |
|
7719 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7720 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7721 |
data reverse. |
95 | 7722 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7723 |
" |
|
2069 | 7724 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7725 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 7726 |
! |
7727 |
||
7728 |
sort:sortBlock |
|
7729 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7730 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7731 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7732 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7733 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7734 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7735 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7736 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7737 |
self quickSort:sortBlock |
1 | 7738 |
|
95 | 7739 |
" |
8874 | 7740 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7741 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7742 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7743 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 7744 |
" |
2069 | 7745 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7746 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 7747 |
! |
7748 |
||
7749 |
sort:sortBlock with:aCollection |
|
7750 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 7751 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7752 |
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
|
7753 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7754 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7755 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7756 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7757 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7758 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7759 |
self quickSort:sortBlock with:aCollection |
1 | 7760 |
|
69 | 7761 |
" |
7762 |
|c1 c2| |
|
1 | 7763 |
c1 := #(1 16 7 9). |
7764 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
7765 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 7766 |
c1 printNL. |
7767 |
c2 printNL |
|
69 | 7768 |
" |
2069 | 7769 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7770 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 7771 |
! |
7772 |
||
12647 | 7773 |
sortByApplying:aBlock |
7774 |
"Sort my contents inplace based on the value of what aBlock returns for each element. |
|
7775 |
Similar to, but even more flexible than sortBySelector." |
|
7776 |
||
7777 |
^ self sort:[:a :b | (aBlock value:a) < (aBlock value:b)] |
|
7778 |
||
7779 |
" |
|
7780 |
replace all uses of sort as in: |
|
7781 |
... sort:[:a :b | a foo < b foo] |
|
7782 |
by: |
|
7783 |
... sortByApplying:[:each | each foo] |
|
7784 |
" |
|
7785 |
! |
|
7786 |
||
11154 | 7787 |
sortBySelector:aSelector |
7788 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
7789 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
7790 |
||
7791 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
7792 |
||
7793 |
" |
|
7794 |
replace all uses of sort as in: |
|
7795 |
... sort:[:a :b | a foo < b foo] |
|
7796 |
by: |
|
7797 |
... sortBySelector:#foo |
|
7798 |
||
7799 |
find these by searching for code matching (code-search in the browsers method list): |
|
7800 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
7801 |
" |
|
7802 |
! |
|
7803 |
||
602 | 7804 |
sortWith:aCollection |
2069 | 7805 |
"sort the receiver collection inplace, using '<' to compare elements. |
7806 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7807 |
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
|
7808 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7809 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7810 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7811 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7812 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 7813 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7814 |
self quickSortWith:aCollection |
424 | 7815 |
! |
7816 |
||
11279 | 7817 |
sortedBy:aBlock |
9072 | 7818 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
7819 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
7820 |
||
7821 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
11279 | 7822 |
|
7823 |
"Modified: / 22-10-2008 / 21:25:07 / cg" |
|
9072 | 7824 |
! |
7825 |
||
602 | 7826 |
topologicalSort:sortBlock |
7827 |
"sort the collection inplace using a sloooow sort algorithm. |
|
7828 |
This algorithm has O-square runtime behavior and should be used only |
|
7829 |
in special situations. |
|
7830 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
7831 |
a>c (for example, to sort classes by inheritance) |
|
7832 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7833 |
In all other situations, use #sort; which implements the quicksort algorithm. |
10812 | 7834 |
|
7835 |
If there are cycles, this algorithm may loop endless!! |
|
7836 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
77 | 7837 |
" |
602 | 7838 |
|
7839 |
|smallestIndex "{ Class: SmallInteger }" |
|
7840 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7841 |
index2 "{ Class: SmallInteger }" |
602 | 7842 |
smallest thisOne| |
7843 |
||
7844 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
7845 |
||
7846 |
end := self size. |
|
862 | 7847 |
1 to:end-1 do:[:index | |
10812 | 7848 |
smallest := self at:index. |
7849 |
smallestIndex := index. |
|
7850 |
||
7851 |
index2 := index + 1. |
|
7852 |
[ |
|
7853 |
thisOne := self at:index2. |
|
7854 |
(index2 ~~ smallestIndex |
|
7855 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
7856 |
smallestIndex := index2. |
|
7857 |
smallest := thisOne. |
|
7858 |
index2 := index + 1. |
|
7859 |
] ifFalse:[ |
|
7860 |
index2 := index2 + 1. |
|
7861 |
]. |
|
7862 |
] doWhile:[index2 <= end]. |
|
862 | 7863 |
|
7864 |
"/ (index + 1) to:end do:[:index2 | |
|
7865 |
"/ thisOne := self at:index2. |
|
7866 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
7867 |
"/ smallestIndex := index2. |
|
7868 |
"/ smallest := thisOne |
|
7869 |
"/ ]. |
|
7870 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7871 |
|
10812 | 7872 |
(smallestIndex ~~ index) ifTrue:[ |
7873 |
thisOne := self at:index. |
|
7874 |
self at:index put:smallest. |
|
7875 |
self at:smallestIndex put:thisOne |
|
7876 |
]. |
|
1 | 7877 |
] |
95 | 7878 |
|
77 | 7879 |
" |
8874 | 7880 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
7881 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
10812 | 7882 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
8874 | 7883 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
7884 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 7885 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7886 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7887 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 7888 |
! ! |
602 | 7889 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7890 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7891 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7892 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7893 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7894 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 7895 |
however, having a common protocol with indexed collections |
7896 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7897 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7898 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7899 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7900 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7901 |
" |
8874 | 7902 |
#(1 2 3) includesKey:4 |
7903 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7904 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7905 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7906 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7907 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7908 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7909 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7910 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7911 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7912 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7913 |
|
2069 | 7914 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7915 |
|
12051 | 7916 |
version_CVS |
13719 | 7917 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.308 2011-09-23 12:03:55 cg Exp $' |
628 | 7918 |
! ! |
6820 | 7919 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7920 |
SequenceableCollection initialize! |