author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 08 Jun 2011 22:53:07 +0100 | |
branch | jv |
changeset 17841 | 7abcc4aef871 |
parent 17834 | 04ff72c5039a |
child 17845 | 7e0cfaac936d |
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" |
17815 | 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 |
||
7261 | 383 |
!SequenceableCollection methodsFor:'Compatibility-Squeak'! |
4414 | 384 |
|
7796 | 385 |
allButFirst |
8874 | 386 |
"Return a copy of the receiver containing all but the first element. |
7796 | 387 |
Raise an error if there are not enough elements." |
388 |
||
389 |
^ self copyFrom:2 |
|
8571 | 390 |
|
391 |
" |
|
392 |
'1234567890' allButFirst |
|
393 |
" |
|
394 |
! |
|
395 |
||
396 |
allButFirst:n |
|
8874 | 397 |
"Return a copy of the receiver containing all but the first n elements. |
8571 | 398 |
Raise an error if there are not enough elements." |
399 |
||
400 |
^ self copyFrom:n+1 |
|
401 |
||
402 |
" |
|
403 |
'1234567890' allButFirst:5 |
|
404 |
" |
|
7796 | 405 |
! |
406 |
||
6149 | 407 |
allButLast |
8874 | 408 |
"Return a copy of the receiver containing all but the last element. |
7796 | 409 |
Raise an error if there are not enough elements." |
6183 | 410 |
|
411 |
^ self allButLast:1 |
|
412 |
||
8571 | 413 |
" |
414 |
'1234567890' allButLast |
|
415 |
" |
|
416 |
||
6183 | 417 |
"Modified: / 13.11.2001 / 13:36:36 / cg" |
418 |
! |
|
419 |
||
420 |
allButLast:n |
|
8874 | 421 |
"Return a copy of the receiver containing all but the last n elements. |
7796 | 422 |
Raise an error if there are not enough elements." |
6183 | 423 |
|
424 |
^ self copyFrom: 1 to: self size - n |
|
425 |
||
8571 | 426 |
" |
427 |
'1234567890' allButLast:5 |
|
428 |
" |
|
429 |
||
6183 | 430 |
"Modified: / 13.11.2001 / 13:36:28 / cg" |
6149 | 431 |
! |
432 |
||
8874 | 433 |
atPin: index |
4414 | 434 |
"Return the index'th element of me if possible. |
435 |
Return the first or last element if index is out of bounds." |
|
436 |
||
437 |
index < 1 ifTrue:[^ self first]. |
|
438 |
index > self size ifTrue:[^ self last]. |
|
439 |
^ self at:index |
|
440 |
||
441 |
" |
|
8874 | 442 |
#(1 2 3) atPin:4 |
443 |
#(1 2 3) atPin:0 |
|
444 |
#(1 2 3) atPin:1 |
|
445 |
#(1 2 3) atPin:3 |
|
446 |
" |
|
447 |
! |
|
448 |
||
449 |
atRandom |
|
4459 | 450 |
"Return any random element from the receiver" |
451 |
||
8573 | 452 |
^ self atRandom:Random |
453 |
||
454 |
" |
|
8874 | 455 |
#(1 2 3) atRandom |
456 |
" |
|
457 |
! |
|
458 |
||
459 |
atRandom:aRandomGenerator |
|
8573 | 460 |
"Return any random element from the receiver" |
461 |
||
4459 | 462 |
|max idx| |
463 |
||
464 |
(max := self size) == 0 ifTrue:[ |
|
8874 | 465 |
self emptyCollectionError |
4459 | 466 |
]. |
8573 | 467 |
idx := aRandomGenerator nextIntegerBetween:1 and:max. |
4459 | 468 |
^ self at:idx |
469 |
||
470 |
" |
|
8874 | 471 |
#(1 2 3) atRandom:(Random new) |
472 |
" |
|
473 |
! |
|
474 |
||
475 |
atWrap:index |
|
4975 | 476 |
"Return the index'th element of me if possible. |
477 |
Wrap the index modulu the receivers size if it is out of bounds." |
|
478 |
||
479 |
^ self at:((index - 1) \\ (self size)) + 1 |
|
480 |
||
481 |
" |
|
8874 | 482 |
#(1 2 3) atWrap:1 |
483 |
#(1 2 3) atWrap:2 |
|
484 |
#(1 2 3) atWrap:3 |
|
485 |
#(1 2 3) atWrap:4 |
|
486 |
#(1 2 3) atWrap:5 |
|
487 |
#(1 2 3) atWrap:6 |
|
488 |
#(1 2 3) atWrap:7 |
|
489 |
#(1 2 3) atWrap:0 |
|
490 |
#(1 2 3) atWrap:-1 |
|
4975 | 491 |
" |
5372 | 492 |
! |
493 |
||
494 |
beginsWith:aCollection |
|
6183 | 495 |
"Squeak & VW compatibility: same as #startsWith: - sigh" |
5372 | 496 |
|
497 |
^ self startsWith:aCollection |
|
6183 | 498 |
|
499 |
"Modified: / 13.11.2001 / 13:49:18 / cg" |
|
6149 | 500 |
! |
501 |
||
8611 | 502 |
collect:aBlock from:startIndex to:stopIndex |
8612 | 503 |
"squeak uses a different naming here - sigh. |
504 |
Notice, that the squeak name is actually wrong and misleading, as it suggests |
|
505 |
collecting first and then taking the subcollection; this is wrong, as conceptually, |
|
506 |
this method first takes a slice (from:to:) and then collects over that. |
|
507 |
I.e. it is equivalent to: |
|
8874 | 508 |
(self startIndex to:stopIndex) collect:aBlock" |
8611 | 509 |
|
510 |
^ self from:startIndex to:stopIndex collect:aBlock |
|
511 |
! |
|
512 |
||
7012 | 513 |
copyAfter: anElement |
8874 | 514 |
"Answer a copy of the receiver from after the first occurrence |
515 |
of anElement up to the end. If no such element exists, answer |
|
516 |
an empty copy." |
|
7012 | 517 |
|idx| |
518 |
||
519 |
idx := self indexOf:anElement. |
|
520 |
idx == 0 ifTrue:[idx := self size]. |
|
521 |
^ self copyFrom:idx + 1 |
|
522 |
||
523 |
" |
|
8874 | 524 |
'hello world' copyAfter:$l |
525 |
'123456123456' copyAfter:$2 |
|
526 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:3 |
|
527 |
#(1 2 3 4 2 3 3 4 5 6) copyAfter:1 |
|
528 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfter:7 |
|
7012 | 529 |
" |
530 |
! |
|
531 |
||
6982 | 532 |
copyAfterLast:element |
8874 | 533 |
"return a copy of the receiver from (but excluding) the last occurrence |
6982 | 534 |
of element to the end; uses = for comparison" |
535 |
||
536 |
|idx| |
|
537 |
||
538 |
idx := self lastIndexOf:element. |
|
539 |
idx == 0 ifTrue:[idx := self size]. |
|
540 |
^ self copyFrom:idx + 1 |
|
541 |
||
542 |
" |
|
8874 | 543 |
'hello world' copyAfterLast:$l |
544 |
'123456123456' copyAfterLast:$2 |
|
545 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:3 |
|
546 |
#(1 2 3 4 2 3 3 4 5 6) copyAfterLast:1 |
|
547 |
#(1 2 3 4 1 2 3 3 4 5 6) copyAfterLast:7 |
|
6982 | 548 |
" |
549 |
! |
|
550 |
||
6980 | 551 |
copyUpToLast:element |
8874 | 552 |
"return a copy of the receiver up to (but excluding) the last occurrence |
6980 | 553 |
of element; uses = for comparison" |
554 |
||
555 |
|idx| |
|
556 |
||
557 |
idx := self lastIndexOf:element. |
|
558 |
idx == 0 ifTrue:[idx := self size + 1]. |
|
559 |
^ self copyTo:idx - 1 |
|
560 |
||
561 |
" |
|
8874 | 562 |
'hello world' copyUpToLast:$l |
563 |
'123456123456' copyUpToLast:$2 |
|
564 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:3 |
|
565 |
#(1 2 3 4 2 3 3 4 5 6) copyUpToLast:1 |
|
566 |
#(1 2 3 4 1 2 3 3 4 5 6) copyUpToLast:7 |
|
6980 | 567 |
" |
568 |
! |
|
569 |
||
8276 | 570 |
copyWithoutAll:elementsToSkip |
571 |
"return a new collection consisting of a copy of the receiver, with |
|
572 |
ALL elements equal to any in elementsToSkip are left out. |
|
573 |
No error is reported, if any in elementsToSkip is not in the collection." |
|
574 |
||
575 |
|n "{ Class: SmallInteger }" |
|
576 |
sz "{ Class: SmallInteger }" |
|
577 |
srcIndex "{ Class: SmallInteger }" |
|
578 |
dstIndex "{ Class: SmallInteger }" |
|
579 |
skipIndex "{ Class: SmallInteger }" |
|
580 |
copy l| |
|
581 |
||
8874 | 582 |
"the code below may look like overkill, |
8276 | 583 |
however, for big collections its better to move data |
584 |
around in big chunks" |
|
585 |
||
586 |
n := self occurrencesOfAny:elementsToSkip. |
|
587 |
n == 0 ifTrue:[^ self copy]. |
|
588 |
||
589 |
sz := self size. |
|
590 |
copy := self copyEmptyAndGrow:(sz - n). |
|
591 |
||
592 |
srcIndex := 1. |
|
593 |
dstIndex := 1. |
|
594 |
||
595 |
n timesRepeat:[ |
|
8874 | 596 |
skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex. |
597 |
l := skipIndex - srcIndex. |
|
598 |
l ~~ 0 ifTrue:[ |
|
599 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
600 |
with:self startingAt:srcIndex. |
|
601 |
dstIndex := dstIndex + l |
|
602 |
]. |
|
603 |
srcIndex := skipIndex + 1 |
|
8276 | 604 |
]. |
605 |
l := sz - srcIndex. |
|
606 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 607 |
with:self startingAt:srcIndex. |
8276 | 608 |
^ copy |
609 |
||
610 |
" |
|
611 |
#($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f) |
|
8874 | 612 |
'abcdefghi' copyWithoutAll:'hai' |
8276 | 613 |
#(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45) |
614 |
" |
|
615 |
! |
|
616 |
||
7304 | 617 |
copyWithoutFirst |
618 |
^ self copyFrom:2 |
|
619 |
! |
|
620 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
621 |
shuffled |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
622 |
"return a randomly shuffled copy of the receiver" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
623 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
624 |
^ self shuffledBy:Random |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
625 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
626 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
627 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
628 |
#(1 2 3 4 5 6 7 8 9) shuffled. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
629 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
630 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
631 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
632 |
shuffledBy:aRandom |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
633 |
"return a randomly shuffled copy of the receiver, using the given random generator" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
634 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
635 |
|copy| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
636 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
637 |
copy := self shallowCopy. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
638 |
copy size to: 1 by: -1 do:[:i | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
639 |
copy swap: i with: ((1 to: i) atRandom: aRandom) |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
640 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
641 |
^ copy |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
642 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
643 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
644 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
645 |
#(1 2 3 4 5 6 7 8 9) shuffledBy:(Random new). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
646 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
647 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
648 |
|
11279 | 649 |
sortBy:aBlock |
650 |
"Sort inplace - destructive" |
|
651 |
||
652 |
self sort:aBlock |
|
653 |
||
654 |
"Created: / 22-10-2008 / 21:25:35 / cg" |
|
4414 | 655 |
! ! |
656 |
||
7261 | 657 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 658 |
|
659 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
660 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
661 |
! ! |
|
662 |
||
17815 | 663 |
|
1 | 664 |
!SequenceableCollection methodsFor:'accessing'! |
665 |
||
1366 | 666 |
after:anObject |
667 |
"return the element, after anObject. |
|
5231 | 668 |
If anObject is not in the receiver, report an error; |
669 |
if anObject is the last in the receiver, return nil. |
|
1366 | 670 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
671 |
||
672 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
673 |
||
674 |
" |
|
8874 | 675 |
#(4 3 2 1) asOrderedCollection after:3. |
676 |
#(4 3 2 1) asOrderedCollection after:5 |
|
677 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 678 |
" |
679 |
||
680 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
681 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
682 |
! |
|
683 |
||
684 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 685 |
"return the element, after anObject. |
5231 | 686 |
If anObject is the last in the receiver, return nil. |
687 |
If there is no such element (anObject is not found), |
|
1366 | 688 |
return the value from exceptionBlock." |
689 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
690 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
691 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
692 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
693 |
idx ~~ 0 ifTrue:[ |
8874 | 694 |
idx == self size ifTrue:[^ nil]. |
695 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
696 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
697 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
698 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
699 |
" |
8874 | 700 |
#(4 3 2 1) after:3 ifAbsent:nil. |
701 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
702 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
703 |
(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
|
704 |
" |
1366 | 705 |
! |
706 |
||
6967 | 707 |
anElement |
8874 | 708 |
"return any element from the collection, |
6967 | 709 |
report an error if there is none" |
710 |
||
711 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
712 |
|
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 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
715 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
716 |
" |
6967 | 717 |
! |
718 |
||
32 | 719 |
at:index ifAbsent:exceptionBlock |
8874 | 720 |
"return the element at index if valid. |
721 |
If the index is invalid, return the result of evaluating |
|
302 | 722 |
the exceptionblock. |
8874 | 723 |
NOTICE: |
724 |
in ST-80, this message is only defined for Dictionaries, |
|
725 |
however, having a common protocol with indexed collections |
|
726 |
often simplifies things." |
|
32 | 727 |
|
8579 | 728 |
(index between:1 and:self size) ifFalse:[ |
8874 | 729 |
^ exceptionBlock value |
32 | 730 |
]. |
731 |
^ self at:index |
|
732 |
||
95 | 733 |
" |
734 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 735 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
736 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 737 |
|
95 | 738 |
(Dictionary with:(#foo -> #bar) |
8874 | 739 |
with:(#frob -> #baz)) |
740 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 741 |
" |
362 | 742 |
! |
743 |
||
17795 | 744 |
atAllIndices:indexCollection |
745 |
"return the elements at each index from indexCollection." |
|
746 |
||
747 |
^ indexCollection collect:[:eachIdx | self at:eachIdx]. |
|
748 |
||
749 |
" |
|
750 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:#( 8 5 12 12 15) |
|
751 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:( 5 to: 10) |
|
752 |
" |
|
753 |
||
754 |
"Created: / 08-08-2010 / 01:15:06 / cg" |
|
755 |
! |
|
756 |
||
757 |
atIndex:index |
|
758 |
"return an element at a given index. This allows for seqentialCollections |
|
759 |
and orderedDictionaries to be both accessed via a numeric index." |
|
760 |
||
761 |
^ self at:index |
|
762 |
||
763 |
"Created: / 08-08-2010 / 00:50:10 / cg" |
|
764 |
! |
|
765 |
||
766 |
atIndex:index put:newValue |
|
767 |
"return an element at a given index. This allows for seqentialCollections |
|
768 |
and orderedDictionaries to be both accessed via a numeric index." |
|
769 |
||
770 |
^ self at:index put:newValue |
|
771 |
||
772 |
"Created: / 08-08-2010 / 00:50:27 / cg" |
|
773 |
! |
|
774 |
||
1366 | 775 |
before:anObject |
776 |
"return the element before the argument, anObject. |
|
5231 | 777 |
If anObject is not in the receiver, report an error; |
778 |
if anObject is the first in the receiver, return nil. |
|
1366 | 779 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
780 |
||
781 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
782 |
||
783 |
" |
|
8874 | 784 |
#(4 3 2 1) asOrderedCollection before:3. |
785 |
#(4 3 2 1) asOrderedCollection before:4 |
|
786 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 787 |
" |
788 |
||
789 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
790 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
791 |
! |
|
792 |
||
793 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 794 |
"return the element, before anObject. |
5231 | 795 |
If anObject is the first in the receiver, return nil. |
796 |
If there is no such element (anObject is not found), |
|
1366 | 797 |
return the value from exceptionBlock." |
798 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
799 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
800 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
801 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
802 |
idx ~~ 0 ifTrue:[ |
8874 | 803 |
idx == 1 ifTrue:[^ nil]. |
804 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
805 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
806 |
^ exceptionBlock value |
1366 | 807 |
|
5231 | 808 |
" |
8874 | 809 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
810 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
811 |
#(4 3 2 1) asOrderedCollection before:5. |
|
812 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
813 |
#(4 3 2 1) asOrderedCollection before:4. |
|
814 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
815 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 816 |
" |
817 |
||
1366 | 818 |
! |
819 |
||
602 | 820 |
first |
8874 | 821 |
"return the first element; |
5231 | 822 |
report an error, if the collection is empty." |
602 | 823 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
824 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 825 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
826 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
827 |
" |
8874 | 828 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
829 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
830 |
" |
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 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 833 |
! |
834 |
||
5887 | 835 |
first:n |
836 |
"return the n first elements of the collection. |
|
837 |
Raises an error if there are not enough elements in the receiver." |
|
838 |
||
839 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
840 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
841 |
||
842 |
^ self copyFirst:n |
|
843 |
||
844 |
" |
|
8874 | 845 |
#(1 2 3 4 5) first:3 |
846 |
#(1 2 3 4 5) first:6 |
|
847 |
#(1 2 3 4 5) asSet first:3 |
|
848 |
'hello world' first:5 |
|
5887 | 849 |
" |
850 |
! |
|
851 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
852 |
indexOfNth:n occurrenceOf:what |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
853 |
"return the index of the nTh occurence of a value, or 0 if there are not that many" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
854 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
855 |
|idx cnt| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
856 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
857 |
n > self size ifTrue:[^ 0]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
858 |
cnt := 0. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
859 |
idx := 0. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
860 |
[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
861 |
idx := self indexOf:what startingAt:idx+1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
862 |
idx == 0 ifTrue:[^ 0]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
863 |
cnt := cnt + 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
864 |
cnt = n ifTrue:[ ^ idx]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
865 |
] loop. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
866 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
867 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
868 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
869 |
#(1 2 3 4 1 2 3 1 9 8 7 1 7 8 9 0) indexOfNth:3 occurrenceOf:9 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
870 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
871 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
872 |
|
362 | 873 |
keyAtEqualValue:value |
874 |
"return the index of a value. |
|
875 |
This is normally not used (use indexOf:), but makes the |
|
876 |
protocol more compatible with dictionaries." |
|
877 |
||
878 |
^ self indexOf:value |
|
879 |
! |
|
880 |
||
881 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
882 |
"return the index of a value. |
|
883 |
This is normally not used (use indexOf:), but makes the |
|
884 |
protocol more compatible with dictionaries." |
|
885 |
||
886 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 887 |
! |
888 |
||
889 |
keyAtValue:value |
|
890 |
"return the index of a value. |
|
891 |
This is normally not used (use indexOf:), but makes the |
|
892 |
protocol more compatible with dictionaries." |
|
893 |
||
894 |
^ self identityIndexOf:value |
|
895 |
! |
|
896 |
||
897 |
keyAtValue:value ifAbsent:exceptionBlock |
|
898 |
"return the index of a value. |
|
899 |
This is normally not used (use indexOf:), but makes the |
|
900 |
protocol more compatible with dictionaries." |
|
901 |
||
902 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
903 |
! |
|
904 |
||
905 |
last |
|
5231 | 906 |
"return the last element; |
907 |
report an error, if the collection is empty." |
|
602 | 908 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
909 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
910 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
911 |
(sz := self size) == 0 ifFalse:[ |
8874 | 912 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
913 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
914 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
915 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
916 |
|
5231 | 917 |
" |
918 |
#(1 2 3 4 5) last |
|
919 |
#() last |
|
920 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
921 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
922 |
|
5887 | 923 |
last:n |
924 |
"return the n last elements of the collection. |
|
925 |
Raises an error if there are not enough elements in the receiver." |
|
926 |
||
927 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
928 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
929 |
||
930 |
^ self copyLast:n |
|
931 |
||
932 |
" |
|
8874 | 933 |
#(1 2 3 4 5) last:3 |
934 |
#(1 2 3 4 5) last:6 |
|
935 |
#(1 2 3 4 5) asSet last:3 |
|
936 |
'hello world' last:5 |
|
5887 | 937 |
" |
938 |
! |
|
939 |
||
4986 | 940 |
nth:n |
5231 | 941 |
"return the nth element; |
942 |
report an error, if the collections size is smaller than n." |
|
4986 | 943 |
|
944 |
^ self at:n |
|
945 |
||
946 |
" |
|
8874 | 947 |
args: |
4986 | 948 |
returns: nth element <object> |
949 |
" |
|
950 |
||
5231 | 951 |
" |
8874 | 952 |
#(10 20 30 40 50) nth:4 |
5231 | 953 |
#(10 20 30 40 50) nth:6 |
954 |
#() nth:1 |
|
955 |
" |
|
956 |
||
4986 | 957 |
! |
958 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
959 |
order |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
960 |
"the order is the set of keys " |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
961 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
962 |
^ 1 to:self size |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
963 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
964 |
|
4855 | 965 |
second |
5231 | 966 |
"return the second element; |
967 |
report an error, if the collections size is smaller than 2." |
|
4855 | 968 |
|
969 |
^ self at:2 |
|
970 |
||
971 |
" |
|
8874 | 972 |
args: |
4855 | 973 |
returns: secondElement <object> |
974 |
" |
|
975 |
||
976 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
977 |
! |
|
978 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
979 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
980 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
981 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
982 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
983 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
984 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
985 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
986 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
987 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
988 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 989 |
! ! |
990 |
||
602 | 991 |
!SequenceableCollection methodsFor:'adding & removing'! |
992 |
||
993 |
add:anObject |
|
994 |
"append the argument, anObject to the collection. |
|
995 |
Return the argument, anObject. |
|
996 |
||
997 |
Notice, that this is modifies the receiver NOT a copy. |
|
998 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 999 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1000 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1001 |
||
1002 |
|newSize| |
|
1003 |
||
1004 |
newSize := self size + 1. |
|
1005 |
self grow:newSize. |
|
1006 |
self at:newSize put:anObject. |
|
1007 |
^ anObject |
|
1008 |
||
1009 |
" |
|
1010 |
|a| |
|
1011 |
a := #(1 2 3 4 5 6 7 8). |
|
1012 |
a add:'hello'. |
|
1013 |
a |
|
1014 |
" |
|
1015 |
" |
|
1016 |
|c| |
|
1017 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1018 |
c add:'hello'. |
|
1019 |
c |
|
1020 |
" |
|
1021 |
! |
|
1022 |
||
1023 |
add:anElement beforeIndex:index |
|
1024 |
"insert the first argument, anObject into the collection before slot index. |
|
1025 |
Return the receiver (sigh - ST-80 compatibility). |
|
1026 |
||
1027 |
Notice, that this is modifies the receiver NOT a copy. |
|
1028 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1029 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1030 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1031 |
||
1032 |
|newSize| |
|
1033 |
||
1034 |
newSize := self size + 1. |
|
1035 |
self grow:newSize. |
|
1036 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
1037 |
self at:index put:anElement |
|
1038 |
||
1039 |
" |
|
1040 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
1041 |
" |
|
202 | 1042 |
! |
1043 |
||
602 | 1044 |
addFirst:anObject |
1045 |
"prepend the argument, anObject to the collection. |
|
1046 |
Return the argument, anObject. |
|
1047 |
||
1048 |
Notice, that this is modifies the receiver NOT a copy. |
|
1049 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1050 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1051 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1052 |
||
1053 |
|newSize| |
|
1054 |
||
1055 |
newSize := self size + 1. |
|
1056 |
self grow:newSize. |
|
1057 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
1058 |
self at:1 put:anObject. |
|
1059 |
^ anObject |
|
1060 |
||
1061 |
" |
|
8874 | 1062 |
|a| |
1063 |
a:= #(1 2 3 4 5 6 7 8). |
|
1064 |
a addFirst:'hello'. |
|
1065 |
a |
|
602 | 1066 |
" |
1067 |
" |
|
1068 |
|c| |
|
1069 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1070 |
c addFirst:'hello'. |
|
1071 |
c |
|
1072 |
" |
|
69 | 1073 |
! |
1074 |
||
602 | 1075 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1076 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1077 |
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
|
1078 |
Use equality compare (=) to search for an occurrence. |
602 | 1079 |
|
1080 |
Notice, that this is modifies the receiver NOT a copy. |
|
1081 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1082 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1083 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1084 |
||
8874 | 1085 |
|any |
602 | 1086 |
dstIndex "{ Class: SmallInteger }" |
1087 |
sz "{ Class: SmallInteger }"| |
|
1088 |
||
1089 |
dstIndex := 1. |
|
1090 |
any := false. |
|
1091 |
sz := self size. |
|
1092 |
1 to:sz do:[:srcIndex | |
|
8874 | 1093 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1094 |
any := true |
|
1095 |
] ifFalse:[ |
|
1096 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1097 |
self at:dstIndex put:(self at:srcIndex) |
|
1098 |
]. |
|
1099 |
dstIndex := dstIndex + 1 |
|
1100 |
] |
|
602 | 1101 |
]. |
1102 |
any ifTrue:[ |
|
8874 | 1103 |
self grow:dstIndex - 1. |
1104 |
^ anElement |
|
602 | 1105 |
]. |
1106 |
^ aBlock value |
|
1107 |
||
1108 |
" |
|
1422 | 1109 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1110 |
#(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
|
1111 |
#(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
|
1112 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1113 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1114 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1115 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1116 |
! |
1117 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1118 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1119 |
"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
|
1120 |
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
|
1121 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1122 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1123 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1124 |
"/ 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
|
1125 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1126 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1127 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1128 |
removed := self species new. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1129 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1130 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1131 |
[runIndex <= self size] whileTrue:[ |
8874 | 1132 |
element := self at:runIndex. |
1133 |
(aBlock value:element) ifTrue:[ |
|
1134 |
removed add:element. |
|
1135 |
self removeAtIndex:runIndex |
|
1136 |
] ifFalse:[ |
|
1137 |
runIndex := runIndex + 1 |
|
1138 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1139 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1140 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1141 |
|
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 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1144 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1145 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1146 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1147 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1148 |
" |
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 |
"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
|
1151 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1152 |
|
602 | 1153 |
removeAtIndex:anIndex |
1154 |
"remove the element stored at anIndex. Return the removed object. |
|
1155 |
||
1156 |
Notice, that this is modifies the receiver NOT a copy. |
|
1157 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1158 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1159 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1160 |
||
1161 |
|element| |
|
1162 |
||
1163 |
element := self at:anIndex. |
|
1164 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1165 |
^ element |
|
1166 |
||
1167 |
" |
|
1168 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1169 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1170 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1171 |
" |
|
13 | 1172 |
! |
1173 |
||
602 | 1174 |
removeFirst |
1175 |
"remove the first element of the receiver and return it. |
|
1176 |
||
1177 |
Notice, that this is modifies the receiver NOT a copy. |
|
1178 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1179 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1180 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1181 |
||
1182 |
^ self removeAtIndex:1 |
|
1183 |
||
1184 |
" |
|
1185 |
|a| |
|
1186 |
a := #(1 2 3 4 5 6). |
|
1187 |
a removeFirst. |
|
8874 | 1188 |
a |
602 | 1189 |
" |
13 | 1190 |
! |
1191 |
||
2294 | 1192 |
removeFromIndex:startIndex |
1193 |
"remove the elements stored at indexes from startIndex to the end. |
|
1194 |
Return the receiver. |
|
1195 |
Returning the receiver is a historic leftover - it may at one |
|
1196 |
time return a collection of the removed elements. |
|
1197 |
||
8874 | 1198 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1199 |
therefore any other users of the receiver will also see this change. |
1200 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1201 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1202 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1203 |
||
1204 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1205 |
||
1206 |
" |
|
8874 | 1207 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1208 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1209 |
" |
|
1210 |
||
1211 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1212 |
! |
|
1213 |
||
602 | 1214 |
removeFromIndex:startIndex toIndex:endIndex |
1215 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1216 |
Return the receiver. |
|
1217 |
Returning the receiver is a historic leftover - it may at one |
|
1218 |
time return a collection of the removed elements. |
|
1219 |
||
8874 | 1220 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1221 |
therefore any other users of the receiver will also see this change. |
1222 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1223 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1224 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1225 |
||
1226 |
|size newSize| |
|
1227 |
||
1228 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1229 |
self grow:(startIndex - 1) |
602 | 1230 |
] ifFalse:[ |
8874 | 1231 |
newSize := size - endIndex + startIndex - 1. |
1232 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1233 |
self grow:newSize |
|
602 | 1234 |
] |
1235 |
||
1236 |
"/ |newSize| |
|
1237 |
"/ |
|
1238 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1239 |
"/ newSize <= 0 ifTrue:[ |
|
1240 |
"/ self grow:0 |
|
1241 |
"/ ] ifFalse:[ |
|
1242 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1243 |
"/ self grow:newSize |
|
1244 |
"/ ] |
|
1245 |
||
1246 |
" |
|
8874 | 1247 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1248 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1249 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1250 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1251 |
" |
1252 |
! |
|
1253 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1254 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1255 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1256 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1257 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1258 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1259 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1260 |
Also note, that it may be a slow operation for some collections, |
8874 | 1261 |
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
|
1262 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1263 |
|
2363 | 1264 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1265 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1266 |
sz "{ Class: SmallInteger }"| |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1267 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1268 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1269 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1270 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1271 |
1 to:sz do:[:srcIndex | |
8874 | 1272 |
el := self at:srcIndex. |
1273 |
(anElement == el) ifTrue:[ |
|
1274 |
any := true |
|
1275 |
] ifFalse:[ |
|
1276 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1277 |
self at:dstIndex put:el |
|
1278 |
]. |
|
1279 |
dstIndex := dstIndex + 1 |
|
1280 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1281 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1282 |
any ifTrue:[ |
8874 | 1283 |
self grow:dstIndex - 1. |
1284 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1285 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1286 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1287 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1288 |
" |
8874 | 1289 |
#(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
|
1290 |
#(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
|
1291 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1292 |
|
2363 | 1293 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1294 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1295 |
|
602 | 1296 |
removeIndex:index |
1297 |
"remove the argument stored at index. Return the receiver. |
|
1298 |
||
1299 |
Notice, that this is modifies the receiver NOT a copy. |
|
1300 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1301 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1302 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1303 |
||
1304 |
self removeFromIndex:index toIndex:index |
|
1305 |
||
1306 |
" |
|
1307 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1308 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1309 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1310 |
" |
|
77 | 1311 |
! |
1312 |
||
602 | 1313 |
removeLast |
1314 |
"remove the last element of the receiver and return it. |
|
1315 |
||
1316 |
Notice, that this is modifies the receiver NOT a copy. |
|
1317 |
Also note, that it may be a slow operation for some collections, |
|
1318 |
due to the grow:-message, which is inefficient for fixed size |
|
1319 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1320 |
||
8874 | 1321 |
^ self removeAtIndex:(self size) |
602 | 1322 |
|
1323 |
" |
|
1324 |
|a| |
|
1325 |
a := #(1 2 3 4 5 6). |
|
1326 |
a removeLast. |
|
8874 | 1327 |
a |
602 | 1328 |
" |
1329 |
! ! |
|
1330 |
||
11407 | 1331 |
!SequenceableCollection methodsFor:'combinatoric'! |
1332 |
||
1333 |
combinationsDo: aBlock |
|
11463 | 1334 |
"Repeatly evaluate aBlock with all combinations of elements from the receivers elements. |
1335 |
The receivers elements must be collections of the individuals to be taken for the combinations" |
|
11407 | 1336 |
|
1337 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1338 |
||
1339 |
" |
|
1340 |
(Array |
|
11410 | 1341 |
with:($a to:$d)) |
1342 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1343 |
" |
|
1344 |
" |
|
1345 |
(Array |
|
11407 | 1346 |
with:($a to:$d) |
1347 |
with:(1 to: 4)) |
|
1348 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1349 |
" |
|
1350 |
" |
|
1351 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1352 |
" |
|
11408 | 1353 |
"all combinations of two letters are generated with: |
1354 |
(Array |
|
1355 |
with:($a to:$z) |
|
1356 |
with:($a to:$z)) |
|
1357 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1358 |
" |
|
11432 | 1359 |
" |
1360 |
(Array |
|
1361 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1362 |
with:#(A)) |
|
1363 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1364 |
" |
|
11407 | 1365 |
! |
1366 |
||
1367 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1368 |
"a helper for combinationsDo:" |
|
1369 |
||
11431 | 1370 |
|loopedElement| |
1371 |
||
1372 |
loopedElement := self at:anInteger. |
|
1373 |
||
11407 | 1374 |
anInteger == self size ifTrue:[ |
11431 | 1375 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1376 |
^ self. |
11431 | 1377 |
]. |
1378 |
||
1379 |
loopedElement do:[:el | |
|
1380 |
|newPrefix| |
|
1381 |
||
1382 |
newPrefix := (prefix copyWith:el). |
|
1383 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1384 |
]. |
1385 |
||
1386 |
" |
|
1387 |
(Array |
|
1388 |
with:($a to:$d) |
|
1389 |
with:(1 to: 4)) |
|
1390 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1391 |
" |
|
11431 | 1392 |
" |
1393 |
(Array |
|
1394 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1395 |
with:#(A)) |
|
1396 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1397 |
" |
|
11407 | 1398 |
! |
1399 |
||
11462 | 1400 |
lexicographicPermutationsDo:aBlock |
1401 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1402 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1403 |
permutations in lexicographical order (i.e. sorted by size). |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1404 |
|
11462 | 1405 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1406 |
the identical same collection (but with different contents) every time. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1407 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1408 |
of permuted copies. The user of this function must be careful in the block, |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1409 |
to copy the argument, if it has to be stored somewhere." |
11462 | 1410 |
|
1411 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1412 |
||
1413 |
" |
|
1414 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1415 |
" |
|
1416 |
! |
|
1417 |
||
1418 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1419 |
"a helper for lexicographicPermutationsDo:" |
|
1420 |
||
1421 |
|mySize values indices| |
|
1422 |
||
1423 |
mySize := self size. |
|
1424 |
anInteger > mySize ifTrue: [^ self]. |
|
1425 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1426 |
values := self copyFrom:anInteger to:mySize. |
|
1427 |
indices := (anInteger to:mySize) asArray. |
|
1428 |
values sortWith:indices. |
|
1429 |
indices do:[:i | |
|
1430 |
self swap: anInteger with: i. |
|
1431 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1432 |
self swap: anInteger with: i |
|
1433 |
] |
|
1434 |
||
1435 |
" |
|
1436 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1437 |
" |
|
1438 |
! |
|
1439 |
||
1440 |
permutationsDo:aBlock |
|
1441 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1442 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1443 |
|
11462 | 1444 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1445 |
the identical same collection (but with different contents) every time. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1446 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1447 |
of permuted copies. The user of this function must be careful in the block, |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1448 |
to copy the argument, if it has to be stored somewhere." |
11407 | 1449 |
|
1450 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1451 |
||
1452 |
" |
|
11462 | 1453 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1454 |
" |
1455 |
||
1456 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1457 |
! |
|
1458 |
||
1459 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1460 |
"a helper for permutationsDo:" |
11407 | 1461 |
|
1462 |
|mySize| |
|
1463 |
||
1464 |
mySize := self size. |
|
11462 | 1465 |
anInteger > mySize ifTrue: [^ self]. |
1466 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1467 |
anInteger to:mySize do:[:i | |
1468 |
self swap: anInteger with: i. |
|
11462 | 1469 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1470 |
self swap: anInteger with: i |
1471 |
] |
|
1472 |
||
1473 |
" |
|
1474 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1475 |
|
1476 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1477 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1478 |
" |
1479 |
||
1480 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1481 |
! |
1482 |
||
1483 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1484 |
"a helper for permutationsDo:" |
|
1485 |
||
1486 |
|mySize| |
|
1487 |
||
1488 |
mySize := self size. |
|
1489 |
anInteger > mySize ifTrue: [^ self]. |
|
1490 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1491 |
anInteger to:mySize do:[:i | |
|
1492 |
self swap: anInteger with:i. |
|
1493 |
anotherCollection swap: anInteger with:i. |
|
1494 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1495 |
self swap:anInteger with: i. |
|
1496 |
anotherCollection swap:anInteger with:i. |
|
1497 |
] |
|
1498 |
||
1499 |
" |
|
1500 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1501 |
||
1502 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1503 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1504 |
" |
|
1505 |
||
1506 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1507 |
! |
|
1508 |
||
1509 |
permutationsWith:anotherCollection do:aBlock |
|
1510 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1511 |
Reorder the copy so that aBlock is presented all (self size factorial) possible permutations. |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1512 |
|
11462 | 1513 |
Notice, that the swapping occurs within a buffered copy, so the block will receive |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1514 |
the identical same collection (but with different contents) every time. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1515 |
This non-functional kludge was done for performance, as we had to generate huge amounts |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1516 |
of permuted copies. The user of this function must be careful in the block, |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1517 |
to copy the argument, if it has to be stored somewhere." |
11462 | 1518 |
|
1519 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1520 |
||
1521 |
" |
|
1522 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1523 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1524 |
" |
|
1525 |
||
1526 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1527 |
! ! |
1528 |
||
602 | 1529 |
!SequenceableCollection methodsFor:'comparing'! |
1530 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1531 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1532 |
"compare two sequenceable collections" |
8874 | 1533 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1534 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1535 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1536 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1537 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1538 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1539 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1540 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1541 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1542 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1543 |
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
|
1544 |
]. |
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 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1547 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1548 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1549 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1550 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1551 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1552 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1553 |
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
|
1554 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1555 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1556 |
(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
|
1557 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1558 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1559 |
|
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 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1562 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1563 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1564 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1565 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1566 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1567 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1568 |
a := 'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1569 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1570 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1571 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1572 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1573 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1574 |
] |
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 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1577 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1578 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1579 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1580 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1581 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1582 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1583 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1584 |
] |
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 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1587 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1588 |
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
|
1589 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1590 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1591 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1592 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1593 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1594 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1595 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1596 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1597 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1598 |
! |
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 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1601 |
"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
|
1602 |
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
|
1603 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1604 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1605 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1606 |
|
602 | 1607 |
= aCollection |
1608 |
"return true if the receiver and aCollection represent collections |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1609 |
with equal contents, and if they are of the same species." |
602 | 1610 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1611 |
| stop "{ Class: SmallInteger }" | |
602 | 1612 |
|
1613 |
(aCollection == self) ifTrue:[^true]. |
|
1614 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1615 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1616 |
|
1617 |
stop := self size. |
|
1618 |
stop == (aCollection size) ifFalse:[^false]. |
|
1619 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1620 |
1 to:stop do:[:index| |
9366 | 1621 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1622 |
]. |
1623 |
^ true |
|
1624 |
||
1625 |
" |
|
8874 | 1626 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1627 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1628 |
#($1 $2 $3 $4 $5) = '12345' |
|
1629 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1630 |
" |
1631 |
! |
|
1632 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1633 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1634 |
"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
|
1635 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1636 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1637 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1638 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1639 |
! |
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 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1642 |
"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
|
1643 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1644 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1645 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1646 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1647 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1648 |
|
8874 | 1649 |
commonPrefixWith:aCollection |
6507 | 1650 |
"return the common prefix of myself and the argument, aCollection. |
1651 |
If there is none, an empty collection is returned." |
|
1652 |
||
1653 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1654 |
||
1655 |
" |
|
8874 | 1656 |
'hello' commonPrefixWith:'hello' |
1657 |
'hello' commonPrefixWith:'hElLo' |
|
1658 |
'hello' commonPrefixWith:'hello world' |
|
1659 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1660 |
||
1661 |
'hello world' commonPrefixWith:'hello' |
|
1662 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1663 |
||
1664 |
'abcd' commonPrefixWith:'bcde' |
|
1665 |
||
1666 |
'abcd' commonPrefixWith:'abab' |
|
1667 |
'abcd' commonPrefixWith:'ab' |
|
1668 |
'abcd' commonPrefixWith:'ababab' |
|
1669 |
'abcd' commonPrefixWith:'abcdef' |
|
1670 |
||
1671 |
'abab' commonPrefixWith:'abcd' |
|
1672 |
'ab' commonPrefixWith:'abcd' |
|
1673 |
'ababab' commonPrefixWith:'abcd' |
|
1674 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1675 |
" |
1676 |
! |
|
1677 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1678 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1679 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1680 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1681 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1682 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1683 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1684 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1685 |
1 to:matchLen do:[:idx | |
8874 | 1686 |
|elHere elThere same| |
1687 |
||
1688 |
elHere := self at:idx. |
|
1689 |
elThere := aCollection at:idx. |
|
1690 |
||
1691 |
ignoreCase ifTrue:[ |
|
1692 |
same := elHere sameAs:elThere |
|
1693 |
] ifFalse:[ |
|
1694 |
same := elHere = elThere |
|
1695 |
]. |
|
1696 |
same ifFalse:[ |
|
1697 |
^ self copyTo:(idx - 1). |
|
1698 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1699 |
]. |
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 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1702 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1703 |
" |
8874 | 1704 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1705 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1706 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1707 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1708 |
||
1709 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1710 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1711 |
||
1712 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1713 |
||
1714 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1715 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1716 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1717 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1718 |
||
1719 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1720 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1721 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1722 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1723 |
" |
|
1724 |
! |
|
1725 |
||
1726 |
commonSuffixWith:aCollection |
|
6507 | 1727 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1728 |
If there is none, an empty collection is returned." |
|
1729 |
||
1730 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1731 |
||
1732 |
" |
|
8874 | 1733 |
'hello' commonSuffixWith:'hello' |
1734 |
'hello' commonSuffixWith:'hElLo' |
|
1735 |
'hello' commonSuffixWith:'hello world' |
|
1736 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1737 |
'hello2 world' commonSuffixWith:'hello world' |
|
1738 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1739 |
||
1740 |
'hello world' commonSuffixWith:'world' |
|
1741 |
'hello WoRlD' commonSuffixWith:'world' |
|
1742 |
||
1743 |
'dcba' commonSuffixWith:'edcb' |
|
1744 |
||
1745 |
'dcba' commonSuffixWith:'baba' |
|
1746 |
'dcba' commonSuffixWith:'ba' |
|
1747 |
'dcba' commonSuffixWith:'bababa' |
|
1748 |
'dcba' commonSuffixWith:'fdcba' |
|
1749 |
||
1750 |
'baba' commonSuffixWith:'dcba' |
|
1751 |
'ba' commonSuffixWith:'dcba' |
|
1752 |
'bababa' commonSuffixWith:'dcba' |
|
1753 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1754 |
" |
1755 |
! |
|
1756 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1757 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1758 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1759 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1760 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1761 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1762 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1763 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1764 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1765 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1766 |
1 to:matchLen do:[:idx | |
8874 | 1767 |
|elHere elThere same| |
1768 |
||
1769 |
elHere := self at:(l1 - idx + 1). |
|
1770 |
elThere := aCollection at:(l2 - idx + 1). |
|
1771 |
||
1772 |
ignoreCase ifTrue:[ |
|
1773 |
same := elHere sameAs:elThere |
|
1774 |
] ifFalse:[ |
|
1775 |
same := elHere = elThere |
|
1776 |
]. |
|
1777 |
same ifFalse:[ |
|
1778 |
^ self copyFrom:(l1 - idx + 2). |
|
1779 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1780 |
]. |
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 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1783 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1784 |
" |
8874 | 1785 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1786 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1787 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1788 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1789 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1790 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1791 |
||
1792 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1793 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1794 |
||
1795 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1796 |
||
1797 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1798 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1799 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1800 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1801 |
||
1802 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1803 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1804 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1805 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1806 |
" |
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 |
|
5813 | 1809 |
deepSameContentsAs:aCollection |
1810 |
"return true, if the receiver and the arg have the same contents |
|
1811 |
in both the named instance vars and any indexed instVars. |
|
1812 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1813 |
||
1814 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1815 |
unused space" |
|
1816 |
||
1817 |
|index "{ Class: SmallInteger }" |
|
1818 |
stop "{ Class: SmallInteger }" | |
|
1819 |
||
1820 |
(aCollection == self) ifTrue:[^true]. |
|
1821 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1822 |
||
1823 |
stop := self size. |
|
1824 |
stop == (aCollection size) ifFalse:[^false]. |
|
1825 |
||
1826 |
index := 1. |
|
1827 |
[index <= stop] whileTrue:[ |
|
8874 | 1828 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1829 |
index := index + 1 |
|
5813 | 1830 |
]. |
1831 |
^ true |
|
1832 |
||
1833 |
" |
|
8874 | 1834 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1835 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1836 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1837 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1838 |
" |
1839 |
! |
|
1840 |
||
602 | 1841 |
endsWith:aCollection |
1842 |
"return true, if the receivers last elements match those |
|
1843 |
of aCollection" |
|
1844 |
||
1845 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1846 |
index2 "{ Class: SmallInteger }" |
1847 |
stop "{ Class: SmallInteger }" |
|
602 | 1848 |
sz "{ Class: SmallInteger }"| |
1849 |
||
1850 |
(aCollection == self) ifTrue:[^ true]. |
|
1851 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1852 |
||
1853 |
stop := aCollection size. |
|
77 | 1854 |
sz := self size. |
602 | 1855 |
stop > sz ifTrue:[^false]. |
1856 |
||
1857 |
index1 := sz. |
|
1858 |
index2 := stop. |
|
1859 |
[index2 > 0] whileTrue:[ |
|
8874 | 1860 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1861 |
index1 := index1 - 1. |
|
1862 |
index2 := index2 - 1 |
|
77 | 1863 |
]. |
602 | 1864 |
^ true |
1865 |
||
1866 |
" |
|
1867 |
'abcde' endsWith:#($d $e) |
|
8874 | 1868 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1869 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1870 |
" |
|
1871 |
! |
|
1872 |
||
7278 | 1873 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1874 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1875 |
|
1876 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1877 |
||
1878 |
" |
|
1879 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1880 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1881 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1882 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1883 |
" |
1884 |
! |
|
1885 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1886 |
hasEqualElements: otherCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1887 |
"Answer whether the receiver's size is the same as otherCollection's |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1888 |
size, and each of the receiver's elements equal the corresponding |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1889 |
element of otherCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1890 |
This should probably replace the current definition of #= ." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1891 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1892 |
^ self isSameSequenceAs:otherCollection |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1893 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1894 |
|
602 | 1895 |
hash |
1896 |
"return a hash key for the receiver" |
|
1897 |
||
1898 |
"this hash is stupid - but for larger collections, the hashing |
|
1899 |
time can become much bigger than the time lost in added probing. |
|
1900 |
Time will show ..." |
|
1901 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1902 |
|mySize h| |
3653 | 1903 |
|
1904 |
mySize := self size. |
|
1905 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1906 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1907 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1908 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1909 |
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
|
1910 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1911 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1912 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1913 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1914 |
"/ 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
|
1915 |
"/ ^ ((((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
|
1916 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1917 |
" |
8874 | 1918 |
#(1 2 3 4 5 6) hash |
1919 |
(1 to:6) hash |
|
1920 |
||
1921 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1922 |
#(1 2 3 4 5) hash |
|
602 | 1923 |
" |
3653 | 1924 |
|
1925 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1926 |
! |
1927 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1928 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1929 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1930 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1931 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1932 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1933 |
^ self |
8874 | 1934 |
sameContentsAs:aCollection |
1935 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1936 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1937 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1938 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1939 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1940 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1941 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1942 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1943 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1944 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1945 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1946 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1947 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1948 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1949 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1950 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1951 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1952 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1953 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1954 |
" |
8874 | 1955 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1956 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1957 |
#($1 $2 $3 $4 $5) = '12345' |
|
1958 |
||
1959 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1960 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1961 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1962 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1963 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1964 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1965 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1966 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1967 |
isSameSequenceAs:otherCollection |
8874 | 1968 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1969 |
and each of the receiver's elements equal the corresponding element of otherCollection. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1970 |
This should probably replace the current definition of #= ." |
6118 | 1971 |
|
1972 |
| size | |
|
1973 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1974 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1975 |
otherCollection isSequenceable ifFalse: [^ false]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1976 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1977 |
1 to:size do:[:index | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1978 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false] |
6118 | 1979 |
]. |
1980 |
^ true |
|
1981 |
! |
|
1982 |
||
17780 | 1983 |
sameContentsAs:aCollection |
1984 |
"return true, if the receiver and the arg have the same contents. |
|
1985 |
||
1986 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1987 |
unused space" |
|
1988 |
||
1989 |
|index "{ Class: SmallInteger }" |
|
1990 |
stop "{ Class: SmallInteger }" | |
|
1991 |
||
1992 |
(aCollection == self) ifTrue:[^true]. |
|
1993 |
(aCollection isSequenceable) ifFalse:[ |
|
1994 |
aCollection with:self do:[:e1 :e2 | |
|
1995 |
(e1 = e2) ifFalse:[^false]. |
|
1996 |
]. |
|
1997 |
^ true |
|
1998 |
]. |
|
1999 |
||
2000 |
stop := self size. |
|
2001 |
stop == (aCollection size) ifFalse:[^false]. |
|
2002 |
||
2003 |
index := 1. |
|
2004 |
[index <= stop] whileTrue:[ |
|
2005 |
((self at:index) = (aCollection at:index)) ifFalse:[^false]. |
|
2006 |
index := index + 1 |
|
2007 |
]. |
|
2008 |
^ true |
|
2009 |
||
2010 |
" |
|
2011 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) copy |
|
2012 |
#($1 $2 $3 $4 $5) sameContentsAs: #(1 2 3 4 5) |
|
2013 |
#($1 $2 $3 $4 $5) sameContentsAs: '12345' |
|
2014 |
#($1 $2 $3 $4 $5) sameContentsAs: '54321' asSortedCollection |
|
2015 |
" |
|
2016 |
||
2017 |
"Created: / 09-07-2010 / 12:44:28 / sr" |
|
2018 |
! |
|
2019 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2020 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2021 |
"return true if the receiver and aCollection represent collections |
8874 | 2022 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2023 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2024 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2025 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2026 |
" |
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 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2029 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2030 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2031 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2032 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2033 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 2034 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2035 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2036 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2037 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2038 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2039 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2040 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2041 |
[index <= stop] whileTrue:[ |
8874 | 2042 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
2043 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2044 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2045 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2046 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2047 |
" |
8874 | 2048 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
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.0 2 3 4.0 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] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2052 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2053 |
#('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
|
2054 |
" |
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 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2057 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2058 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2059 |
|
602 | 2060 |
startsWith:aCollection |
2061 |
"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
|
2062 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
2063 |
If the argument is empty, true is returned." |
602 | 2064 |
|
2065 |
|index "{ Class: SmallInteger }" |
|
2066 |
stop "{ Class: SmallInteger }" | |
|
2067 |
||
2068 |
(aCollection == self) ifTrue:[^true]. |
|
2069 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
2070 |
||
2071 |
stop := aCollection size. |
|
2072 |
stop > self size ifTrue:[^false]. |
|
2073 |
||
2074 |
index := 1. |
|
2075 |
[index <= stop] whileTrue:[ |
|
8874 | 2076 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
2077 |
index := index + 1 |
|
602 | 2078 |
]. |
2079 |
^ true |
|
2080 |
||
2081 |
" |
|
2082 |
'abcde' startsWith:#($a $b $c) |
|
2083 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
2084 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 2085 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 2086 |
" |
7279 | 2087 |
! |
2088 |
||
2089 |
startsWithAnyOf:aCollectionOfCollections |
|
2090 |
"return true, if the receiver starts with any in aCollection" |
|
2091 |
||
2092 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
2093 |
||
2094 |
" |
|
2095 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
2096 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 2097 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
2098 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 2099 |
" |
13 | 2100 |
! ! |
2101 |
||
260 | 2102 |
!SequenceableCollection methodsFor:'converting'! |
2103 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2104 |
asCollectionOfSubCollectionsOfSize:pieceSize |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2105 |
"return a collection containing pieces of size pieceSite from the receiver. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2106 |
The last piece may be smaller, if the receivers size is not a multiple of pieceSize." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2107 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2108 |
|pieces |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2109 |
start "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2110 |
stop "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2111 |
mySize "{ Class:SmallInteger }"| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2112 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2113 |
pieces := OrderedCollection new. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2114 |
start := 1. stop := start + pieceSize - 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2115 |
mySize := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2116 |
[stop <= mySize] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2117 |
pieces add:(self copyFrom:start to:stop). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2118 |
start := start + pieceSize. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2119 |
stop := stop + pieceSize. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2120 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2121 |
(start <= mySize) ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2122 |
pieces add:(self copyFrom:start to:mySize). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2123 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2124 |
^ pieces |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2125 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2126 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2127 |
'123123123123123123' asCollectionOfSubCollectionsOfSize:3 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2128 |
'12312312312312312312' asCollectionOfSubCollectionsOfSize:3 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2129 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2130 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2131 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2132 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 2133 |
"return a collection containing the subcollections (separated by anElement) |
2134 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2135 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2136 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2137 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2138 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2139 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2140 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2141 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2142 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2143 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2144 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2145 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2146 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2147 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2148 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2149 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2150 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2151 |
1 to:numberOfCols do:[:lineNr | |
8874 | 2152 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
2153 |
stopIndex == 0 ifTrue:[ |
|
2154 |
stopIndex := self size |
|
2155 |
] ifFalse: [ |
|
2156 |
stopIndex := stopIndex - 1. |
|
2157 |
]. |
|
2158 |
||
2159 |
(stopIndex < startIndex) ifTrue: [ |
|
2160 |
cols add:(myClass new:0) |
|
2161 |
] ifFalse: [ |
|
2162 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
2163 |
]. |
|
2164 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2165 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2166 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2167 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2168 |
" |
8874 | 2169 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2170 |
'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
|
2171 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2172 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2173 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2174 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2175 |
|
9234 | 2176 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
2177 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2178 |
of the receiver by anElement. |
|
2179 |
If anElement occurs multiple times in a row, |
|
2180 |
the block will be invoked with empty collections as argument. |
|
2181 |
This algorithm uses equality-compare to detect the element." |
|
2182 |
||
2183 |
|subCollection |
|
2184 |
endIndex "{ Class:SmallInteger }" |
|
2185 |
startIndex "{ Class:SmallInteger }" |
|
2186 |
stopIndex "{ Class:SmallInteger }" | |
|
2187 |
||
2188 |
startIndex := 0. |
|
2189 |
endIndex := self size. |
|
2190 |
||
2191 |
[startIndex <= endIndex] whileTrue:[ |
|
2192 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2193 |
stopIndex == 0 ifTrue:[ |
|
2194 |
stopIndex := self size |
|
2195 |
] ifFalse: [ |
|
2196 |
stopIndex := stopIndex - 1. |
|
2197 |
]. |
|
2198 |
||
2199 |
(stopIndex < startIndex) ifTrue: [ |
|
2200 |
subCollection := self species new:0 |
|
2201 |
] ifFalse: [ |
|
2202 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2203 |
]. |
|
2204 |
aBlock value:subCollection. |
|
2205 |
startIndex := stopIndex + 1 |
|
2206 |
]. |
|
2207 |
||
2208 |
" |
|
2209 |
'' |
|
2210 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2211 |
||
2212 |
'1 one' |
|
2213 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2214 |
||
2215 |
'1 one:2 two:3 three:4 four:5 five' |
|
2216 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2217 |
||
2218 |
'a::b' |
|
2219 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2220 |
||
2221 |
':' |
|
2222 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2223 |
||
2224 |
':a' |
|
2225 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2226 |
||
2227 |
'a:' |
|
2228 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2229 |
" |
|
2230 |
! |
|
2231 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2232 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2233 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2234 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2235 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2236 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2237 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2238 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2239 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2240 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2241 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2242 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2243 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2244 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2245 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2246 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2247 |
[done] whileFalse:[ |
8874 | 2248 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2249 |
stopIndex == 0 ifTrue:[ |
|
2250 |
stopIndex := self size. |
|
2251 |
done := true. |
|
2252 |
] ifFalse: [ |
|
2253 |
stopIndex := stopIndex - 1. |
|
2254 |
]. |
|
2255 |
||
2256 |
(stopIndex < startIndex) ifTrue: [ |
|
2257 |
items add:(myClass new:0) |
|
2258 |
] ifFalse: [ |
|
2259 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2260 |
]. |
|
2261 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2262 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2263 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2264 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2265 |
" |
8874 | 2266 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2267 |
#(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
|
2268 |
" |
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 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2272 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2273 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2274 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2275 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2276 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2277 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2278 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2279 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2280 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2281 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2282 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2283 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2284 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2285 |
#(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
|
2286 |
" |
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 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2290 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2291 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2292 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2293 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2294 |
|words |
8874 | 2295 |
start "{ Class:SmallInteger }" |
2296 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2297 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2298 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2299 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2300 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2301 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2302 |
[start <= mySize] whileTrue:[ |
8874 | 2303 |
"skip multiple separators" |
2304 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2305 |
start := start + 1 . |
|
2306 |
start > mySize ifTrue:[ |
|
2307 |
^ words |
|
2308 |
]. |
|
2309 |
]. |
|
2310 |
||
2311 |
stop := self findFirst:aBlock startingAt:start. |
|
2312 |
stop == 0 ifTrue:[ |
|
2313 |
words add:(self copyFrom:start to:mySize). |
|
2314 |
^ words |
|
2315 |
]. |
|
2316 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2317 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2318 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2319 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2320 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2321 |
" |
8874 | 2322 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2323 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2324 |
#(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
|
2325 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2326 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2327 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2328 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2329 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2330 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2331 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2332 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2333 |
|
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 |
|
1413 | 2337 |
asStringCollection |
2338 |
"return a new string collection containing the elements; |
|
2339 |
these ought to be strings. (i.e. String or Text instances)" |
|
2340 |
||
2341 |
|newColl sz| |
|
2342 |
||
2343 |
sz := self size. |
|
9162 | 2344 |
newColl := StringCollection withSize:sz. |
1413 | 2345 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2346 |
^ newColl |
|
2347 |
||
2348 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2349 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2350 |
! |
|
2351 |
||
1188 | 2352 |
asStringWith:sepChar |
8874 | 2353 |
"return a string generated by concatenating my elements |
1188 | 2354 |
(which must be strings or nil) and embedding sepChar characters in between. |
2355 |
Nil entries and empty strings are counted as empty lines." |
|
2356 |
||
8874 | 2357 |
^ self |
17780 | 2358 |
from:1 to:(self size) |
2359 |
asStringWith:sepChar |
|
2360 |
compressTabs:false |
|
2361 |
final:nil |
|
1413 | 2362 |
|
2363 |
" |
|
2364 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
17780 | 2365 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:'|' |
2366 |
'hello|world|foo|bar|baz' asCollectionOfSubstringsSeparatedBy:$| |
|
2367 |
" |
|
2368 |
||
2369 |
"Modified: / 10-07-2010 / 22:59:29 / cg" |
|
1188 | 2370 |
! |
2371 |
||
2372 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2373 |
"return part of myself as a string with embedded sepCharacters. |
|
2374 |
My elements must be strings or nil; nil entries and empty strings are |
|
2375 |
taken as empty lines." |
|
2376 |
||
8874 | 2377 |
^ self |
2378 |
from:firstLine to:lastLine |
|
2379 |
asStringWith:sepCharacter |
|
2380 |
compressTabs:false |
|
2381 |
final:nil |
|
1188 | 2382 |
" |
2383 |
creating entries for searchpath: |
|
2384 |
||
8874 | 2385 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2386 |
||
2387 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2388 |
" |
2389 |
||
2390 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2391 |
! |
|
2392 |
||
260 | 2393 |
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
|
2394 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2395 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2396 |
taken as empty lines. |
2397 |
If the argument compressTabs is true, leading spaces are converted |
|
2398 |
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
|
2399 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2400 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2401 |
^ self |
8874 | 2402 |
from:firstLine to:lastLine |
2403 |
asStringWith:sepCharacter |
|
2404 |
compressTabs:compressTabs |
|
2405 |
final:endCharacter |
|
2406 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2407 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2408 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2409 |
! |
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 |
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
|
2412 |
"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
|
2413 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2414 |
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
|
2415 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2416 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2417 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2418 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2419 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2420 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2421 |
any emphasis. If false, a plain string is returned. |
260 | 2422 |
This method is tuned for big collections, in not creating many |
263 | 2423 |
intermediate strings (has linear runtime). For very small collections |
2424 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2425 |
|
5575 | 2426 |
^ self |
8874 | 2427 |
from:firstLine to:lastLine |
2428 |
asStringWith:sepCharacter |
|
2429 |
compressTabs:compressTabs |
|
2430 |
final:endCharacter |
|
2431 |
withEmphasis:withEmphasis |
|
5575 | 2432 |
! |
2433 |
||
2434 |
asStringWithCRs |
|
8874 | 2435 |
"return a string generated by concatenating my elements |
5575 | 2436 |
(which must be strings or nil) and embedding cr characters in between. |
2437 |
Nil entries and empty strings are counted as empty lines." |
|
2438 |
||
2439 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2440 |
||
2441 |
" |
|
8874 | 2442 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2443 |
|
2444 |
(OrderedCollection new |
|
8874 | 2445 |
add:'hello'; |
2446 |
add:'world'; |
|
2447 |
add:'foo'; |
|
2448 |
add:('bar' asText allBold); |
|
2449 |
yourself) asStringWithCRs |
|
5575 | 2450 |
|
2451 |
Transcript showCR: |
|
8874 | 2452 |
(OrderedCollection new |
2453 |
add:'hello'; |
|
2454 |
add:'world'; |
|
2455 |
add:'foo'; |
|
2456 |
add:('bar' asText allBold); |
|
2457 |
yourself) asStringWithCRs |
|
5575 | 2458 |
" |
2459 |
||
2460 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2461 |
! |
|
2462 |
||
2463 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2464 |
"return a string generated by concatenating some of my elements |
5575 | 2465 |
(which must be strings or nil) and embedding cr characters in between. |
2466 |
Nil entries and empty strings are counted as empty lines." |
|
2467 |
||
2468 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2469 |
||
2470 |
" |
|
8874 | 2471 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2472 |
|
2473 |
" |
|
2474 |
||
2475 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2476 |
! |
|
2477 |
||
2478 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2479 |
"return part of myself as a string with embedded cr's. |
|
2480 |
My elements must be strings or nil. |
|
2481 |
If the argument compressTabs is true, leading spaces are converted |
|
2482 |
to tab-characters (8col tabs). |
|
2483 |
Nil entries and empty strings are taken as empty lines." |
|
2484 |
||
2485 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2486 |
! |
|
2487 |
||
2488 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2489 |
"return part of myself as a string with embedded cr's. |
|
2490 |
My elements must be strings or nil; nil entries and empty strings are |
|
2491 |
taken as empty lines. |
|
2492 |
If the argument compressTabs is true, leading spaces are converted |
|
2493 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2494 |
should be followed by a cr or not." |
|
2495 |
||
2496 |
^ self asStringWith:(Character cr) |
|
8874 | 2497 |
from:firstLine |
2498 |
to:lastLine |
|
2499 |
compressTabs:compressTabs |
|
2500 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2501 |
! |
2502 |
||
2503 |
decodeAsLiteralArray |
|
2504 |
"given a literalEncoding in the receiver, |
|
2505 |
create & return the corresponding object. |
|
2506 |
The inverse operation to #literalArrayEncoding." |
|
2507 |
||
9414 | 2508 |
|clsName cls| |
5575 | 2509 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2510 |
self size == 0 ifTrue:[ |
9358 | 2511 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2512 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2513 |
|
9414 | 2514 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2515 |
clsName isSymbol ifFalse:[ |
9358 | 2516 |
^ MissingClassInLiteralArrayErrorSignal |
2517 |
raiseRequestWith:clsName |
|
9414 | 2518 |
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
|
2519 |
]. |
9414 | 2520 |
cls := Smalltalk classNamed:clsName. |
5575 | 2521 |
cls isNil ifTrue:[ |
9414 | 2522 |
^ MissingClassInLiteralArrayErrorSignal |
2523 |
raiseRequestWith:clsName |
|
2524 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2525 |
]. |
9414 | 2526 |
|
5575 | 2527 |
^ cls decodeFromLiteralArray:self. |
2528 |
||
2529 |
" |
|
10476 | 2530 |
#(Array 10 20) decodeAsLiteralArray |
2531 |
#(10 20) literalArrayEncoding |
|
2532 |
" |
|
2533 |
||
2534 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2535 |
! |
2536 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2537 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2538 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2539 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2540 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2541 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2542 |
|
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 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2546 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2547 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2548 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2549 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2550 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2551 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2552 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2553 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2554 |
|
8874 | 2555 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2556 |
"return part of myself as a string with embedded sepCharacters. |
2557 |
My elements must be strings or nil; nil entries and empty strings are |
|
2558 |
taken as empty lines." |
|
2559 |
||
8874 | 2560 |
^ self |
2561 |
from:firstLine |
|
2562 |
to:lastLine |
|
2563 |
asStringWith:sepCharacter |
|
2564 |
compressTabs:false |
|
2565 |
final:nil |
|
5575 | 2566 |
" |
2567 |
creating entries for searchpath: |
|
2568 |
||
8874 | 2569 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2570 |
||
2571 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2572 |
||
2573 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2574 |
" |
2575 |
||
2576 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2577 |
! |
|
2578 |
||
2579 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2580 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2581 |
My elements must be strings or nil; nil entries and empty strings are |
|
2582 |
taken as empty lines. |
|
2583 |
If the argument compressTabs is true, leading spaces are converted |
|
2584 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2585 |
character (if non-nil)." |
|
2586 |
||
2587 |
^ self |
|
8874 | 2588 |
from:firstLine |
2589 |
to:lastLine |
|
2590 |
asStringWith:sepCharacter |
|
2591 |
compressTabs:compressTabs |
|
2592 |
final:endCharacter |
|
2593 |
withEmphasis:true |
|
5575 | 2594 |
|
2595 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2596 |
! |
|
2597 |
||
2598 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2599 |
"return part of myself as a string or text with embedded sepCharacters |
|
2600 |
and followup endCharacter. |
|
2601 |
My elements must be strings or nil; nil entries and empty strings are |
|
2602 |
taken as empty lines. |
|
2603 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2604 |
If the argument compressTabs is true, leading spaces are converted |
|
2605 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2606 |
character (if non-nil). |
|
2607 |
The withEmphais argument controls if the returned string should preserve |
|
2608 |
any emphasis. If false, a plain string is returned. |
|
2609 |
This method is tuned for big collections, in not creating many |
|
2610 |
intermediate strings (has linear runtime). For very small collections |
|
2611 |
and small strings, it may be faster to use the comma , operation." |
|
2612 |
||
293 | 2613 |
|idx1 "{ Class:SmallInteger }" |
2614 |
idx2 "{ Class:SmallInteger }" |
|
2615 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2616 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2617 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2618 |
newString lineString spaces idx nTabs |
8099 | 2619 |
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
|
2620 |
thisLen anyTab| |
260 | 2621 |
|
2622 |
" |
|
2623 |
first accumulate the size of the string, to avoid |
|
2624 |
countless reallocations. If tabs are compressed, |
|
2625 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2626 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2627 |
" |
8099 | 2628 |
needEmphasis := false. |
2629 |
maxBitsPerCharacter := 8. |
|
1015 | 2630 |
|
260 | 2631 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2632 |
sepCharacter isNil ifTrue:[ |
8874 | 2633 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2634 |
] ifFalse:[ |
8874 | 2635 |
sepCharacter isCharacter ifTrue:[ |
2636 |
sepCnt := 1 |
|
2637 |
] ifFalse:[ |
|
2638 |
sepCnt := sepCharacter size |
|
2639 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2640 |
]. |
260 | 2641 |
|
293 | 2642 |
idx1 := firstLine. |
2643 |
idx2 := lastLine. |
|
2644 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2645 |
lineString := self at:lineIndex. |
2646 |
||
2647 |
lineString notNil ifTrue:[ |
|
2648 |
withEmphasis ifTrue:[ |
|
2649 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2650 |
needEmphasis := true |
|
2651 |
]. |
|
2652 |
]. |
|
2653 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2654 |
totalLength := totalLength + lineString size |
|
2655 |
]. |
|
2656 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2657 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2658 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2659 |
|
8099 | 2660 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2661 |
maxBitsPerCharacter > 16 ifTrue:[ |
2662 |
stringClass := Unicode32String. |
|
2663 |
] ifFalse:[ |
|
2664 |
stringClass := Unicode16String. |
|
2665 |
] |
|
8099 | 2666 |
] ifFalse:[ |
8874 | 2667 |
stringClass := String. |
8099 | 2668 |
]. |
2669 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2670 |
endCharacter notNil ifTrue:[ |
8874 | 2671 |
endCharacter isCharacter ifTrue:[ |
2672 |
totalLength := totalLength + 1 |
|
2673 |
] ifFalse:[ |
|
2674 |
totalLength := totalLength + endCharacter size |
|
2675 |
]. |
|
260 | 2676 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2677 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2678 |
|
260 | 2679 |
spaces := ' '. |
1016 | 2680 |
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
|
2681 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2682 |
needEmphasis ifTrue:[ |
8874 | 2683 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2684 |
]. |
260 | 2685 |
|
2686 |
" |
|
2687 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2688 |
Be careful with runArrays: |
8874 | 2689 |
replacing individual elements is VERY expensive. |
2690 |
Therefore, create a new runArray from scratch. |
|
260 | 2691 |
" |
2692 |
pos := 1. |
|
293 | 2693 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2694 |
lineString := self at:lineIndex. |
2695 |
thisLen := lineString size. |
|
2696 |
thisLen ~~ 0 ifTrue:[ |
|
2697 |
withEmphasis ifFalse:[ |
|
2698 |
lineString := lineString string. |
|
2699 |
]. |
|
2700 |
||
2701 |
(anyTab := compressTabs) ifTrue:[ |
|
2702 |
" |
|
2703 |
mhmh: could use withTabs from String-class here, |
|
2704 |
but we should avoid creating too many temporary strings |
|
2705 |
(especially, since this method is typically used when converting |
|
2706 |
big texts such as when saving in the filebrowser ...). |
|
2707 |
Therefore, we convert tabs inline here doing a direct replace |
|
2708 |
in newString." |
|
2709 |
||
2710 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2711 |
nTabs := (idx-1) // 8. |
|
2712 |
anyTab := (nTabs > 0) |
|
2713 |
]. |
|
2714 |
anyTab ifTrue:[ |
|
2715 |
"any tabs" |
|
2716 |
||
2717 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2718 |
||
2719 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2720 |
newRuns notNil ifTrue:[ |
|
2721 |
newRuns add:nil withOccurrences:nTabs |
|
2722 |
]. |
|
2723 |
pos := pos + nTabs. |
|
2724 |
||
2725 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2726 |
newRuns notNil ifTrue:[ |
|
2727 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2728 |
idx to:lineString size do:[:pos | |
|
2729 |
newRuns add:(lineString emphasisAt:pos) |
|
2730 |
] |
|
2731 |
] ifFalse:[ |
|
2732 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2733 |
] |
|
2734 |
]. |
|
2735 |
pos := pos + thisLen - (nTabs * 8). |
|
2736 |
] ifFalse:[ |
|
2737 |
newString replaceFrom:pos with:lineString. |
|
2738 |
||
2739 |
newRuns notNil ifTrue:[ |
|
2740 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2741 |
newRuns addAll:(lineString emphasis) |
|
2742 |
] ifFalse:[ |
|
2743 |
newRuns add:nil withOccurrences:lineString size |
|
2744 |
] |
|
2745 |
]. |
|
2746 |
pos := pos + thisLen. |
|
2747 |
]. |
|
2748 |
]. |
|
2749 |
||
2750 |
lineIndex ~~ lastLine ifTrue:[ |
|
2751 |
c := sepCharacter |
|
2752 |
] ifFalse:[ |
|
2753 |
c := endCharacter |
|
2754 |
]. |
|
2755 |
||
2756 |
c notNil ifTrue:[ |
|
2757 |
c isCharacter ifTrue:[ |
|
2758 |
newString at:pos put:c. |
|
2759 |
newRuns notNil ifTrue:[ |
|
2760 |
newRuns add:nil. |
|
2761 |
]. |
|
2762 |
pos := pos + 1 |
|
2763 |
] ifFalse:[ |
|
2764 |
newString replaceFrom:pos with:c. |
|
2765 |
newRuns notNil ifTrue:[ |
|
2766 |
newRuns add:nil withOccurrences:c size |
|
2767 |
]. |
|
2768 |
pos := pos + c size |
|
2769 |
] |
|
2770 |
]. |
|
260 | 2771 |
]. |
2772 |
||
2773 |
" |
|
2774 |
in case of tab compression, the result has to be |
|
2775 |
cut to size ... sorry |
|
2776 |
" |
|
2777 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2778 |
newString := newString copyTo:(pos - 1) |
260 | 2779 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2780 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2781 |
newRuns notNil ifTrue:[ |
8874 | 2782 |
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
|
2783 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2784 |
|
260 | 2785 |
^ newString |
2786 |
||
2787 |
" |
|
2788 |
creating entries for searchpath: |
|
2789 |
||
8874 | 2790 |
#('foo' 'bar' 'baz' '/foo/bar') |
2791 |
asStringWith:$: |
|
2792 |
from:1 to:4 |
|
2793 |
compressTabs:false |
|
2794 |
final:nil |
|
260 | 2795 |
|
2796 |
with trailing colon: |
|
2797 |
||
8874 | 2798 |
#('foo' 'bar' 'baz' '/foo/bar') |
2799 |
asStringWith:$: |
|
2800 |
from:1 to:4 |
|
2801 |
compressTabs:false |
|
2802 |
final:$: |
|
260 | 2803 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2804 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2805 |
|
8874 | 2806 |
#('foo' 'bar' 'baz') |
2807 |
asStringWith:nil |
|
2808 |
from:1 to:3 |
|
2809 |
compressTabs:false |
|
2810 |
final:nil |
|
260 | 2811 |
|
2812 |
creating a string from a collection of lines: |
|
2813 |
||
8874 | 2814 |
#('foo' 'bar' 'baz') |
2815 |
asStringWith:(Character cr) |
|
2816 |
from:1 to:3 |
|
2817 |
compressTabs:false |
|
2818 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2819 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2820 |
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
|
2821 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2822 |
(Array |
8874 | 2823 |
with:'foo' |
2824 |
with:('bar' asText allBold) |
|
2825 |
with:'baz' |
|
2826 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2827 |
) |
|
2828 |
asStringWith:(Character cr) |
|
2829 |
from:1 to:4 |
|
2830 |
compressTabs:false |
|
2831 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2832 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2833 |
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
|
2834 |
|
8874 | 2835 |
#('foo' 'bar' 'baz') |
2836 |
asStringWith:'__' |
|
2837 |
from:1 to:3 |
|
2838 |
compressTabs:false |
|
2839 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2840 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2841 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2842 |
|
8874 | 2843 |
#('foo' 'bar' 'baz') |
2844 |
asStringWith:'__' |
|
2845 |
from:1 to:3 |
|
2846 |
compressTabs:false |
|
2847 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2848 |
|
260 | 2849 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2850 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2851 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2852 |
"Modified: / 17.6.1998 / 12:31:59 / cg" |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2853 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2854 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2855 |
subCollections:aBlock |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2856 |
"Answser an ordered collection of ordered collections |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2857 |
where each subcollection is delimited by an element of the receiver |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2858 |
for which the given block evaluates to true." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2859 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2860 |
|str answer current e| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2861 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2862 |
str := self readStream. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2863 |
answer := OrderedCollection new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2864 |
current := OrderedCollection new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2865 |
[ str atEnd ] whileFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2866 |
e := str next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2867 |
current add:e. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2868 |
(aBlock value:e) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2869 |
answer add:current. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2870 |
current := OrderedCollection new |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2871 |
] |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2872 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2873 |
current notEmpty ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2874 |
answer add:current |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2875 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2876 |
^ answer |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2877 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2878 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2879 |
#( 1 2 3 nil 4 5 6 nil 7 8 9 nil ) subCollections:[:el | el isNil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2880 |
" |
260 | 2881 |
! ! |
2882 |
||
7253 | 2883 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2884 |
||
8874 | 2885 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2886 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2887 |
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
|
2888 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2889 |
however, physically, no copy is made). |
9291 | 2890 |
Warning: |
2891 |
The slice SHARES the memory for the element-data with the original, |
|
2892 |
this means that any modifications in the original are visible in the slice |
|
2893 |
and vice versa." |
|
7253 | 2894 |
|
2895 |
^ self |
|
9291 | 2896 |
from:startIndex |
2897 |
to:self size |
|
2898 |
by:1 |
|
8874 | 2899 |
|
2900 |
" |
|
2901 |
#(1 2 3 4 5 6 7) from:3 |
|
2902 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2903 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2904 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2905 |
" |
|
2906 |
! |
|
2907 |
||
2908 |
from:startIndex by:step |
|
9291 | 2909 |
"Create a ReindexedCollection from the receiver. |
2910 |
The new collection represents the elements starting at startIndex to the end. |
|
2911 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2912 |
however, physically, no copy is made). |
|
2913 |
Warning: |
|
2914 |
The slice SHARES the memory for the element-data with the original, |
|
2915 |
this means that any modifications in the original are visible in the slice |
|
2916 |
and vice versa." |
|
7253 | 2917 |
|
2918 |
^ self |
|
9291 | 2919 |
from:startIndex |
2920 |
to:(step > 1 |
|
2921 |
ifTrue: [self size] |
|
2922 |
ifFalse: [1]) |
|
2923 |
by:step |
|
8874 | 2924 |
|
2925 |
" |
|
2926 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2927 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2928 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2929 |
( #(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
|
2930 |
" |
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 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2934 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2935 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2936 |
Warning: |
2937 |
The slice SHARES the memory for the element-data with the original, |
|
2938 |
this means that any modifications in the original are visible in the slice |
|
2939 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2940 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2941 |
^ self |
9291 | 2942 |
from:startIndex |
2943 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2944 |
|
2945 |
" |
|
2946 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2947 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2948 |
" |
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 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2952 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2953 |
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
|
2954 |
slice := coll from:3 to:7. |
8874 | 2955 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2956 |
coll at:4 put:40. |
8874 | 2957 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2958 |
" |
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 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2962 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2963 |
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
|
2964 |
slice := coll from:3 to:7. |
8874 | 2965 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2966 |
slice at:1 put:40. |
8874 | 2967 |
slice. |
2968 |
" |
|
2969 |
! |
|
2970 |
||
2971 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2972 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2973 |
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
|
2974 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2975 |
however, physically, no copy is made). |
9291 | 2976 |
Warning: |
2977 |
The slice SHARES the memory for the element-data with the original, |
|
2978 |
this means that any modifications in the original are visible in the slice |
|
2979 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2980 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2981 |
^ self |
9291 | 2982 |
from:startIndex |
2983 |
to:endIndex |
|
2984 |
by: (startIndex <= endIndex |
|
2985 |
ifTrue: [1] |
|
2986 |
ifFalse: [-1]) |
|
8874 | 2987 |
|
2988 |
" |
|
2989 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
2990 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
2991 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
2992 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
2993 |
" |
|
2994 |
! |
|
2995 |
||
2996 |
from:startIndex to:endIndex by:step |
|
9291 | 2997 |
"Create a ReindexedCollection from the receiver. |
2998 |
The new collection represents the receivers elements up to endIndex. |
|
2999 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
3000 |
however, physically, no copy is made). |
|
3001 |
Warning: |
|
3002 |
The slice SHARES the memory for the element-data with the original, |
|
3003 |
this means that any modifications in the original are visible in the slice |
|
3004 |
and vice versa." |
|
7253 | 3005 |
|
3006 |
^ ReindexedCollection |
|
9291 | 3007 |
on:self |
3008 |
from:startIndex |
|
3009 |
to:endIndex |
|
3010 |
by:step |
|
8874 | 3011 |
|
3012 |
" |
|
3013 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
3014 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
3015 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
3016 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
3017 |
" |
|
3018 |
! |
|
3019 |
||
3020 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3021 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3022 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3023 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3024 |
however, physically, no copy is made). |
9291 | 3025 |
Warning: |
3026 |
The slice SHARES the memory for the element-data with the original, |
|
3027 |
this means that any modifications in the original are visible in the slice |
|
3028 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3029 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3030 |
^ self |
9291 | 3031 |
from:1 |
3032 |
to:endIndex |
|
3033 |
by:1 |
|
8874 | 3034 |
|
3035 |
" |
|
3036 |
#(1 2 3 4 5 6 7) to:4 |
|
3037 |
( #(1 2 3 4 5 6 7) to:4) first |
|
3038 |
( #(1 2 3 4 5 6 7) to:4) last |
|
3039 |
( #(1 2 3 4 5 6 7) to:4) size |
|
3040 |
" |
|
3041 |
! |
|
3042 |
||
3043 |
to:endIndex by:step |
|
9291 | 3044 |
"Create a ReindexedCollection from the receiver. |
3045 |
The new collection represents the receivers elements up to endIndex. |
|
3046 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
3047 |
however, physically, no copy is made). |
|
3048 |
Warning: |
|
3049 |
The slice SHARES the memory for the element-data with the original, |
|
3050 |
this means that any modifications in the original are visible in the slice |
|
3051 |
and vice versa." |
|
7253 | 3052 |
|
3053 |
^ self |
|
9291 | 3054 |
from:(step > 0 |
3055 |
ifTrue: [1] |
|
3056 |
ifFalse: [self size]) |
|
3057 |
to:endIndex |
|
3058 |
by:step |
|
8874 | 3059 |
|
3060 |
" |
|
3061 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
3062 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
3063 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
3064 |
( #(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
|
3065 |
" |
7253 | 3066 |
! ! |
3067 |
||
1 | 3068 |
!SequenceableCollection methodsFor:'copying'! |
3069 |
||
9106 | 3070 |
++ aCollection |
3071 |
"return a new collection formed from concatenating the receiver with |
|
3072 |
the argument. The class of the new collection is determined by the |
|
3073 |
receivers class, so mixing classes is possible, if the second collections |
|
3074 |
elements can be stored into instances of the receivers class." |
|
3075 |
||
3076 |
^ self , aCollection |
|
3077 |
||
3078 |
" |
|
3079 |
#($a $b $c) ++ #(1 2 3) |
|
3080 |
#($a $b $c) ++ '123' |
|
3081 |
'abc' ++ '123' |
|
3082 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
3083 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3084 |
'abc' asArray ++ #(1 2 3 4 5) |
|
3085 |
" |
|
3086 |
! |
|
3087 |
||
1 | 3088 |
, aCollection |
3089 |
"return a new collection formed from concatenating the receiver with |
|
44 | 3090 |
the argument. The class of the new collection is determined by the |
3091 |
receivers class, so mixing classes is possible, if the second collections |
|
3092 |
elements can be stored into instances of the receivers class." |
|
1 | 3093 |
|
8874 | 3094 |
|newCollection |
1 | 3095 |
mySize "{ Class: SmallInteger }" |
3096 |
newSize "{ Class: SmallInteger }" |
|
3097 |
otherSize "{ Class: SmallInteger }" |
|
3098 |
dstIndex "{ Class: SmallInteger }"| |
|
3099 |
||
3100 |
mySize := self size. |
|
3101 |
otherSize := aCollection size. |
|
3102 |
newSize := mySize + otherSize. |
|
202 | 3103 |
|
252 | 3104 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 3105 |
|
3106 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
3107 |
dstIndex := mySize + 1. |
|
202 | 3108 |
aCollection isSequenceable ifTrue:[ |
8874 | 3109 |
" |
3110 |
yes, aCollection has indexed elements |
|
3111 |
" |
|
3112 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 3113 |
] ifFalse:[ |
8874 | 3114 |
" |
3115 |
no, enumerate aCollection |
|
3116 |
" |
|
3117 |
aCollection do:[:element | |
|
3118 |
newCollection at:dstIndex put:element. |
|
3119 |
dstIndex := dstIndex + 1 |
|
3120 |
] |
|
1 | 3121 |
]. |
3122 |
^ newCollection |
|
44 | 3123 |
|
95 | 3124 |
" |
3125 |
#($a $b $c) , #(1 2 3) |
|
3126 |
#($a $b $c) , '123' |
|
3127 |
'abc' , '123' |
|
3128 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
3129 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3130 |
'abc' asArray , #(1 2 3 4 5) |
|
3131 |
" |
|
1 | 3132 |
! |
3133 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3134 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3135 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3136 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3137 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3138 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3139 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3140 |
" |
8874 | 3141 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3142 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3143 |
" |
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 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3147 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3148 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3149 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3150 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3151 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3152 |
" |
8874 | 3153 |
#($a $b $c $d $e $f $g) copyButLast:2 |
3154 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3155 |
" |
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 |
|
602 | 3158 |
copyFirst:count |
3159 |
"return a new collection consisting of the receivers first count |
|
3160 |
elements - this is just a rename of copyTo: - for compatibility." |
|
3161 |
||
3162 |
^ self copyFrom:1 to:count |
|
3163 |
||
3164 |
" |
|
3165 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
3166 |
'1234567890' copyFirst:4 |
|
3167 |
" |
|
3168 |
! |
|
3169 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3170 |
copyFrom:startIndex |
9234 | 3171 |
"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
|
3172 |
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
|
3173 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3174 |
^ self copyFrom:startIndex to:(self size) |
602 | 3175 |
|
3176 |
" |
|
3177 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
3178 |
'1234567890' copyFrom:2 |
|
8874 | 3179 |
(10 to:19) copyFrom:5 |
602 | 3180 |
" |
3181 |
! |
|
3182 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3183 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3184 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3185 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3186 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3187 |
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
|
3188 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3189 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3190 |
^ self |
8874 | 3191 |
copyFrom:startIndex |
3192 |
to:(startIndex + numberOfElements - 1) |
|
3193 |
||
3194 |
" |
|
3195 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3196 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3197 |
" |
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 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3200 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3201 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3202 |
copyFrom:startIndex to:stopIndex |
602 | 3203 |
"return a new collection consisting of receivers elements |
4403 | 3204 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3205 |
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
|
3206 |
Raise an error, if stopIndex is out of range." |
602 | 3207 |
|
3208 |
|newCollection newSize| |
|
3209 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3210 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3211 |
newSize < 0 ifTrue:[ |
8874 | 3212 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3213 |
]. |
602 | 3214 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3215 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 3216 |
^ newCollection |
3217 |
||
3218 |
" |
|
3219 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3220 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3221 |
(1 to:10) copyFrom:2 to:5 |
3222 |
||
3223 |
'1234567890' copyFrom:2 to:15 |
|
3224 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 3225 |
" |
3226 |
! |
|
3227 |
||
3228 |
copyLast:count |
|
3229 |
"return a new collection consisting of the receivers last count |
|
3230 |
elements." |
|
3231 |
||
3232 |
|sz| |
|
3233 |
||
3234 |
sz := self size. |
|
3235 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3236 |
||
3237 |
" |
|
3238 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3239 |
'1234567890' copyLast:4 |
|
3240 |
" |
|
3241 |
! |
|
3242 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3243 |
copyMapping:map |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3244 |
"return a copy, where elements all replaced via the given mapping. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3245 |
If the element is present as key in map, it is replaced by a corresponding value from the map. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3246 |
Otherwise, it is left unchanged." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3247 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3248 |
|s| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3249 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3250 |
s := WriteStream on:self species new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3251 |
self do:[:el | |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3252 |
s nextPut:(map at:el ifAbsent:el) |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3253 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3254 |
^ s contents |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3255 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3256 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3257 |
|map| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3258 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3259 |
map := Dictionary new |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3260 |
at:1 put:'one'; |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3261 |
at:2 put:'two'; |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3262 |
yourself. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3263 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3264 |
#(1 2 3 4 1 2 3 4) copyMapping:map |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3265 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3266 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3267 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3268 |
copyMappingFrom:inMap to:outMap |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3269 |
"return a copy, where elements all replaced via the given mapping. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3270 |
If the element is present in inmap, it is replaced by a corresponding value from outmap. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3271 |
Otherwise, it is left unchanged." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3272 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3273 |
|d| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3274 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3275 |
d := Dictionary new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3276 |
inMap keysAndValuesDo:[:k :i | d at:i put:(outMap at:k)]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3277 |
^ self copyMapping:d |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3278 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3279 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3280 |
#(1 2 3 4 1 2 3 4) copyMappingFrom:#(1 2) to:#('one' 'two') |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3281 |
'hello' copyMappingFrom:'eo' to:'**' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3282 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3283 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3284 |
|
3653 | 3285 |
copyReplaceAll:oldElement with:newElement |
3286 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3287 |
have been replaced by newElement." |
|
3288 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3289 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3290 |
^ self copy replaceAll:oldElement with:newElement |
3291 |
||
3292 |
" |
|
8874 | 3293 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3294 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3295 |
" |
3296 |
||
3297 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3298 |
! |
|
3299 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3300 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3301 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3302 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3303 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3304 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3305 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3306 |
|
5489 | 3307 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3308 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3309 |
"/ '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
|
3310 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3311 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3312 |
self do:[:el | |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3313 |
el = oldObject ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3314 |
s nextPutAll:aCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3315 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3316 |
s nextPut:el |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3317 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3318 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3319 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3320 |
|
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 |
args: oldObject : <object> |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3323 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3324 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3325 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3326 |
" |
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 |
" |
8874 | 3329 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3330 |
#(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
|
3331 |
" |
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 |
|
602 | 3334 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3335 |
"return a copy of the receiver, where the elements from startIndex to |
|
3336 |
endIndex have been replaced by the elements of aCollection" |
|
3337 |
||
3338 |
|newColl sz replSize| |
|
3339 |
||
3340 |
replSize := aCollection size. |
|
3341 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
3342 |
newColl := self copyEmptyAndGrow:sz. |
|
3343 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3344 |
newColl replaceFrom:startIndex with:aCollection. |
|
3345 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3346 |
^ newColl |
|
3347 |
||
3348 |
" |
|
3349 |
#(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 | 3350 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
602 | 3351 |
" |
3352 |
! |
|
3353 |
||
3354 |
copyReplaceFrom:startIndex with:aCollection |
|
3355 |
"return a copy of the receiver, where the elements from startIndex to |
|
3356 |
the end have been replaced by the elements of aCollection" |
|
3357 |
||
3358 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
3359 |
||
3360 |
" |
|
8874 | 3361 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
3362 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 3363 |
" |
3364 |
! |
|
3365 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3366 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3367 |
"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
|
3368 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3369 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3370 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3371 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3372 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3373 |
" |
8874 | 3374 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
3375 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3376 |
" |
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 |
|
602 | 3379 |
copyThrough:element |
3380 |
"return a new collection consisting of the receiver elements |
|
8874 | 3381 |
up-to (AND including) the first occurrence of element." |
602 | 3382 |
|
3383 |
|idx| |
|
3384 |
||
3385 |
idx := self indexOf:element. |
|
3386 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
3387 |
^ self copyFrom:1 to:idx |
|
3388 |
||
3389 |
" |
|
3390 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
3391 |
'1234567890' copyThrough:$5 |
|
3392 |
'1234567890' copyThrough:$a |
|
3393 |
'1234567890' copyThrough:$1 |
|
3394 |
" |
|
3395 |
! |
|
3396 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3397 |
copyTo:stopIndex |
9234 | 3398 |
"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
|
3399 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3400 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3401 |
^ self copyFrom:1 to:stopIndex |
602 | 3402 |
|
3403 |
" |
|
3404 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
3405 |
'1234567890' copyTo:4 |
|
3406 |
" |
|
3407 |
! |
|
3408 |
||
5526 | 3409 |
copyToMax:stop |
3410 |
"return a new collection consisting of receivers elements |
|
8874 | 3411 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 3412 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
3413 |
||
3414 |
^ self copyFrom:1 to:(self size min:stop) |
|
3415 |
||
3416 |
" |
|
3417 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
3418 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
3419 |
" |
|
3420 |
! |
|
3421 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3422 |
copyTransliterating:oldElements to:newElements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3423 |
"return a copy, where elements all transliterated via the given mapping. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3424 |
The transiteraion works like perl's tr operator. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3425 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3426 |
Transliterates all occurrences of the characters found in the search list with the |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3427 |
corresponding character in the replacement list. |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3428 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3429 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3430 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3431 |
Note that tr does not do other character escapes such as \n. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3432 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3433 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3434 |
Note also that the whole range idea is rather unportable between character sets, |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3435 |
and even within character sets they may cause results you probably didn't expect. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3436 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3437 |
case (a-e, A-E), or digits (0-4). |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3438 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3439 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3440 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3441 |
^ self |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3442 |
copyTransliterating:oldElements to:newElements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3443 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3444 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3445 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3446 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3447 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3448 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3449 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3450 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3451 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3452 |
can also be used to remove character: |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3453 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3454 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3455 |
'abcdefg |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3456 |
hijkl1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3457 |
234567890' copyTransliterating:(Array with:Character cr) to:'' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3458 |
|
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3459 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3460 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3461 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3462 |
copyTransliterating:oldElements to:newElements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3463 |
complement:complement squashDuplicates:squashDuplicates |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3464 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3465 |
"return a copy, where elements all transliterated via the given mapping. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3466 |
The transiteraion works like perl's tr operator. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3467 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3468 |
Transliterates all occurrences of the characters found in the search list with the |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3469 |
corresponding character in the replacement list. It returns the number of characters replaced |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3470 |
or deleted. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3471 |
A character range may be specified with a hyphen, so /A-J/0-9/ does the same replacement as |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3472 |
/ACEGIBDFHJ/0246813579/. The hyphen may be escaped with a backslash as in /0\-1). |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3473 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3474 |
Note that tr does not do other character escapes such as \n. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3475 |
If you want to map strings between lower/upper cases, see asUpperCase and asLowerCase. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3476 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3477 |
Note also that the whole range idea is rather unportable between character sets, |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3478 |
and even within character sets they may cause results you probably didn't expect. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3479 |
A sound principle is to use only ranges that begin from and end at either alphabets of equal |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3480 |
case (a-e, A-E), or digits (0-4). |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3481 |
Anything else is unsafe. If in doubt, spell out the character sets in full. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3482 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3483 |
If complement is true, non-matching chars are affected. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3484 |
If squashDuplicates is true, translated duplicates are squashed (but not non-matches) |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3485 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3486 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3487 |
|map s1 s2 range1 range2 c1 c2 outStream prevXlated last inc| |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3488 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3489 |
map := Dictionary new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3490 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3491 |
s1 := oldElements readStream. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3492 |
s2 := newElements readStream. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3493 |
range1 := range2 := nil. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3494 |
[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3495 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3496 |
range1 notNil or:[ s1 atEnd not ] |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3497 |
] whileTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3498 |
range1 notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3499 |
c1 := range1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3500 |
range1 atEnd ifTrue:[range1 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3501 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3502 |
c1 := s1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3503 |
s1 peek == $\ ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3504 |
s1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3505 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3506 |
s1 peek == $- ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3507 |
s1 next. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3508 |
last := s1 next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3509 |
inc := c1 < last ifTrue:[1] ifFalse:[-1]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3510 |
range1 := (c1 to:last by:inc) readStream. |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3511 |
c1 := range1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3512 |
range1 atEnd ifTrue:[range1 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3513 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3514 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3515 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3516 |
range2 notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3517 |
c2 := range2 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3518 |
range2 atEnd ifTrue:[range2 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3519 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3520 |
s2 atEnd ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3521 |
"/ keep c2 |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3522 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3523 |
c2 := s2 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3524 |
s2 peek == $\ ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3525 |
s1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3526 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3527 |
s2 peek == $- ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3528 |
s2 next. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3529 |
last := s2 next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3530 |
inc := c2 < last ifTrue:[1] ifFalse:[-1]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3531 |
range2 := (c2 to:last by:inc) readStream. |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3532 |
c2 := range2 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3533 |
range2 atEnd ifTrue:[range2 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3534 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3535 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3536 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3537 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3538 |
map at:c1 put:c2 |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3539 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3540 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3541 |
outStream := WriteStream on:(self species new). |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3542 |
self do:[:el | |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3543 |
|xLated out| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3544 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3545 |
complement ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3546 |
(map includesKey:el) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3547 |
xLated := out := el. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3548 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3549 |
out := newElements at:1 ifAbsent:nil. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3550 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3551 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3552 |
(map includesKey:el) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3553 |
xLated := out := map at:el. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3554 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3555 |
out := el. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3556 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3557 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3558 |
out notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3559 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3560 |
"/ ignore |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3561 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3562 |
outStream nextPut:out. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3563 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3564 |
prevXlated := out. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3565 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3566 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3567 |
^ outStream contents |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3568 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3569 |
" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3570 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3571 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3572 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3573 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3574 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3575 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3576 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3577 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3578 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3579 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3580 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3581 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3582 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3583 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3584 |
complement:false squashDuplicates:true |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3585 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3586 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3587 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3588 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3589 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3590 |
complement:true squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3591 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3592 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3593 |
complement:true squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3594 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3595 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3596 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3597 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3598 |
- delete all a-zA-Z |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3599 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3600 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3601 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3602 |
- delete all except a-zA-Z |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3603 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3604 |
complement:true squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3605 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3606 |
- squash multiple spaces |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3607 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:' ' to:' ' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3608 |
complement:false squashDuplicates:true |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3609 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3610 |
- squash multiple digits |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3611 |
'abcd efghij kl112234 5678999 0abbb cccc deeeef ghijkl' copyTransliterating:'0-9' to:'0-9' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3612 |
complement:false squashDuplicates:true |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3613 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3614 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3615 |
|
602 | 3616 |
copyUpTo:element |
3617 |
"return a new collection consisting of the receiver elements |
|
8874 | 3618 |
up-to (but excluding) the first occurrence of element; |
602 | 3619 |
or to the end, if element is not included" |
3620 |
||
3621 |
|idx| |
|
3622 |
||
3623 |
idx := self indexOf:element. |
|
3624 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3625 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
3626 |
^ self copyFrom:1 to:(idx-1) |
|
3627 |
||
3628 |
" |
|
3629 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
3630 |
'1234567890' copyUpTo:$5 |
|
3631 |
'1234567890' copyUpTo:$a |
|
3632 |
'1234567890' copyUpTo:$1 |
|
3633 |
'123456789' copyUpTo:$0 |
|
3634 |
" |
|
3635 |
! |
|
3636 |
||
1 | 3637 |
copyWith:newElement |
233 | 3638 |
"return a new collection containing the receivers elements |
8874 | 3639 |
and the single new element, newElement. |
233 | 3640 |
This is different from concatentation, which expects another collection |
3641 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3642 |
|
3643 |
|newCollection mySize newSize| |
|
3644 |
||
3645 |
mySize := self size. |
|
3646 |
newSize := mySize + 1. |
|
252 | 3647 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3648 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3649 |
newCollection at:newSize put:newElement. |
|
3650 |
^newCollection |
|
44 | 3651 |
|
95 | 3652 |
" |
3653 |
#(1 2 3 4 5) copyWith:$a |
|
3654 |
'abcdefg' copyWith:$h |
|
3655 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3656 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3657 |
" |
|
1 | 3658 |
! |
3659 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3660 |
copyWithFirst:newFirstElement |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3661 |
"return a new collection containing the receivers elements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3662 |
and the single new element, newElement up front. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3663 |
This is different from concatentation, which expects two collections |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3664 |
as argument, but equivalent to copy-and-addFirst." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3665 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3666 |
|newCollection mySize newSize| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3667 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3668 |
mySize := self size. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3669 |
newSize := mySize + 1. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3670 |
newCollection := self copyEmptyAndGrow:newSize. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3671 |
newCollection replaceFrom:2 to:mySize+1 with:self startingAt:1. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3672 |
newCollection at:1 put:newFirstElement. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3673 |
^newCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3674 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3675 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3676 |
#(1 2 3 4 5) copyWithFirst:$a |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3677 |
'abcdefg' copyWithFirst:$h |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3678 |
'abcdefg' copyWithFirst:'123' -- will fail: string cannot be stored into string |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3679 |
'abcdefg' copyWithFirst:1 -- will fail: integer cannot be stored into string |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3680 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3681 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3682 |
|
61 | 3683 |
copyWithout:elementToSkip |
3684 |
"return a new collection consisting of a copy of the receiver, with |
|
3685 |
ALL elements equal to elementToSkip are left out. |
|
3686 |
No error is reported, if elementToSkip is not in the collection." |
|
3687 |
||
293 | 3688 |
|n "{ Class: SmallInteger }" |
3689 |
sz "{ Class: SmallInteger }" |
|
3690 |
srcIndex "{ Class: SmallInteger }" |
|
3691 |
dstIndex "{ Class: SmallInteger }" |
|
3692 |
skipIndex "{ Class: SmallInteger }" |
|
3693 |
copy l| |
|
61 | 3694 |
|
8874 | 3695 |
"the code below may look like overkill, |
61 | 3696 |
however, for big collections its better to move data |
3697 |
around in big chunks" |
|
3698 |
||
3699 |
n := self occurrencesOf:elementToSkip. |
|
3700 |
n == 0 ifTrue:[^ self copy]. |
|
3701 |
||
3702 |
sz := self size. |
|
252 | 3703 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3704 |
|
3705 |
srcIndex := 1. |
|
3706 |
dstIndex := 1. |
|
3707 |
||
3708 |
n timesRepeat:[ |
|
8874 | 3709 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3710 |
l := skipIndex - srcIndex. |
|
3711 |
l ~~ 0 ifTrue:[ |
|
3712 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3713 |
with:self startingAt:srcIndex. |
|
3714 |
dstIndex := dstIndex + l |
|
3715 |
]. |
|
3716 |
srcIndex := skipIndex + 1 |
|
61 | 3717 |
]. |
3718 |
l := sz - srcIndex. |
|
3719 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3720 |
with:self startingAt:srcIndex. |
61 | 3721 |
^ copy |
3722 |
||
95 | 3723 |
" |
3724 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3725 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3726 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3727 |
'abcdefghi' copyWithout:$h |
3728 |
'abcdefg' copyWithout:$h |
|
95 | 3729 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3730 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3731 |
#(90 80 70 60 50) copyWithout:70 |
|
3732 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3733 |
" |
|
61 | 3734 |
! |
3735 |
||
602 | 3736 |
copyWithoutFirst:elementToSkip |
3737 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3738 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3739 |
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
|
3740 |
Do not confuse this with copyButFirst:" |
602 | 3741 |
|
3742 |
|copy skipIndex sz| |
|
3743 |
||
3744 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3745 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3746 |
||
3747 |
sz := self size - 1. |
|
3748 |
copy := self copyEmptyAndGrow:sz. |
|
3749 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3750 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3751 |
^ copy |
|
3752 |
||
3753 |
" |
|
3754 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3755 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3756 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3757 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3758 |
" |
|
3759 |
! |
|
3760 |
||
8673 | 3761 |
copyWithoutIdentical:elementToSkip |
3762 |
"return a new collection consisting of a copy of the receiver, with |
|
3763 |
ALL elements identical to elementToSkip are left out. |
|
3764 |
No error is reported, if elementToSkip is not in the collection." |
|
3765 |
||
3766 |
|n "{ Class: SmallInteger }" |
|
3767 |
sz "{ Class: SmallInteger }" |
|
3768 |
srcIndex "{ Class: SmallInteger }" |
|
3769 |
dstIndex "{ Class: SmallInteger }" |
|
3770 |
skipIndex "{ Class: SmallInteger }" |
|
3771 |
copy l| |
|
3772 |
||
8874 | 3773 |
"the code below may look like overkill, |
8673 | 3774 |
however, for big collections its better to move data |
3775 |
around in big chunks" |
|
3776 |
||
3777 |
n := self count:[:el | el == elementToSkip]. |
|
3778 |
n == 0 ifTrue:[^ self copy]. |
|
3779 |
||
3780 |
sz := self size. |
|
3781 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3782 |
||
3783 |
srcIndex := 1. |
|
3784 |
dstIndex := 1. |
|
3785 |
||
3786 |
n timesRepeat:[ |
|
8874 | 3787 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3788 |
l := skipIndex - srcIndex. |
|
3789 |
l ~~ 0 ifTrue:[ |
|
3790 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3791 |
with:self startingAt:srcIndex. |
|
3792 |
dstIndex := dstIndex + l |
|
3793 |
]. |
|
3794 |
srcIndex := skipIndex + 1 |
|
8673 | 3795 |
]. |
3796 |
l := sz - srcIndex. |
|
3797 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3798 |
with:self startingAt:srcIndex. |
8673 | 3799 |
^ copy |
3800 |
||
3801 |
" |
|
8874 | 3802 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3803 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3804 |
" |
3805 |
! |
|
3806 |
||
61 | 3807 |
copyWithoutIndex:omitIndex |
3808 |
"return a new collection consisting of receivers elements |
|
3809 |
without the argument stored at omitIndex" |
|
3810 |
||
3811 |
|copy sz| |
|
3812 |
||
202 | 3813 |
sz := self size - 1. |
252 | 3814 |
copy := self copyEmptyAndGrow:sz. |
293 | 3815 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3816 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3817 |
^ copy |
3818 |
||
95 | 3819 |
" |
3820 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3821 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3822 |
" |
|
1 | 3823 |
! |
3824 |
||
202 | 3825 |
copyWithoutLast:count |
3826 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3827 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3828 |
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
|
3829 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3830 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3831 |
|
9250 | 3832 |
<resource:#obsolete> |
3833 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3834 |
^ self copyTo:(self size - count) |
202 | 3835 |
|
3836 |
" |
|
8874 | 3837 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3838 |
'1234567890' copyWithoutLast:4 |
|
202 | 3839 |
" |
5231 | 3840 |
! |
3841 |
||
17754 | 3842 |
repeatedConcatenation:nTimes |
3843 |
"return a new collection consisting of nTimes the concatenation of the receiver" |
|
3844 |
||
3845 |
|newColl idx| |
|
3846 |
||
3847 |
newColl := self species new:(self size * nTimes). |
|
3848 |
idx := 1. |
|
3849 |
nTimes timesRepeat:[ |
|
3850 |
newColl replaceFrom:idx with:self startingAt:1. |
|
3851 |
idx := idx + self size. |
|
3852 |
]. |
|
3853 |
^ newColl |
|
3854 |
||
3855 |
" |
|
3856 |
'hello' repeatedConcatenation:5 |
|
3857 |
#[1 2 3] repeatedConcatenation:3 |
|
3858 |
#(1 2 3) repeatedConcatenation:1 |
|
3859 |
" |
|
3860 |
! |
|
3861 |
||
5231 | 3862 |
restAfter:anElement |
8366 | 3863 |
"return a new collection consisting of the receivers elements after |
5231 | 3864 |
(but excluding) anElement. |
8366 | 3865 |
If anElement is not in the receiver, the returned collection |
3866 |
will be empty. |
|
3867 |
See also #upTo:." |
|
5231 | 3868 |
|
3869 |
|pos| |
|
3870 |
||
3871 |
pos := self indexOf:anElement. |
|
8366 | 3872 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3873 |
||
5231 | 3874 |
^ self copyFrom:(pos + 1) |
3875 |
||
3876 |
" |
|
8874 | 3877 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3878 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3879 |
'hello world' upTo:Character space |
|
3880 |
'hello world' restAfter:Character space |
|
3881 |
'1234.5678' upTo:$. |
|
3882 |
'1234.5678' restAfter:$. |
|
8366 | 3883 |
" |
5231 | 3884 |
! |
3885 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3886 |
trimForWhich:aCheckBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3887 |
"return a copy of myself without leading and trailing elements for which aCheckBlock returns true. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3888 |
Normally, this is mostly used with string receivers." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3889 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3890 |
|startIndex "{ Class: SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3891 |
endIndex "{ Class: SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3892 |
sz| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3893 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3894 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3895 |
startIndex := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3896 |
endIndex := sz. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3897 |
|
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
3898 |
[(startIndex <= endIndex) and:[aCheckBlock value:(self at:startIndex)]] whileTrue:[ |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3899 |
startIndex := startIndex + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3900 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3901 |
[(endIndex > 1) and:[aCheckBlock value:(self at:endIndex)]] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3902 |
endIndex := endIndex - 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3903 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3904 |
startIndex > endIndex ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3905 |
^ self class new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3906 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3907 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3908 |
^ self |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3909 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3910 |
^ self copyFrom:startIndex to:endIndex |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3911 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3912 |
" |
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
3913 |
' foo ' trimForWhich:[:ch | ch isSeparator] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
3914 |
' ' trimForWhich:[:ch | ch isSeparator] |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3915 |
#(0 0 0 1 2 3 0 0 0) trimForWhich:[:e | e = 0] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3916 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3917 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3918 |
|
5231 | 3919 |
upTo:anElement |
3920 |
"return a new collection consisting of the receivers elements upTo |
|
3921 |
(but excluding) anElement. |
|
3922 |
If anElement is not in the receiver, the returned collection |
|
3923 |
will consist of all elements of the receiver. |
|
9307 | 3924 |
See also #restAfter: , #copyFrom:index." |
5231 | 3925 |
|
3926 |
|pos| |
|
3927 |
||
3928 |
pos := self indexOf:anElement. |
|
3929 |
pos == 0 ifTrue:[^ self copy]. |
|
3930 |
||
3931 |
^ self copyFrom:1 to:(pos - 1) |
|
3932 |
||
3933 |
" |
|
8874 | 3934 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3935 |
'hello world' upTo:Character space |
|
3936 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
3937 |
'1234.5678' upTo:$. |
|
3938 |
'1234' upTo:$. |
|
5231 | 3939 |
|
3940 |
raises an error: |
|
3941 |
||
8874 | 3942 |
(Dictionary new |
9307 | 3943 |
at:#foo put:'foo'; |
3944 |
at:#bar put:'bar'; |
|
3945 |
at:#baz put:'baz'; |
|
3946 |
yourself) upTo:#bar |
|
8874 | 3947 |
|
5231 | 3948 |
" |
3949 |
||
3950 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
3951 |
! |
|
3952 |
||
3953 |
upTo:anElement count:count |
|
3954 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 3955 |
(but excluding) count found anElements, i.e. the first found count-1 |
3956 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 3957 |
procedure is done from the end of the collection backwards. |
3958 |
If anElement is not in the receiver, the returned collection |
|
3959 |
will consist of all elements of the receiver" |
|
3960 |
||
3961 |
|startPos endPos pos found| |
|
3962 |
||
3963 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 3964 |
^ self upTo:anElement |
5231 | 3965 |
]. |
3966 |
||
3967 |
startPos := 1. |
|
3968 |
endPos := self size. |
|
3969 |
found := 0. |
|
3970 |
||
3971 |
count > 1 ifTrue:[ |
|
8874 | 3972 |
pos := 0. |
3973 |
[pos < self size and: [found < count]] |
|
3974 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3975 |
found == count ifTrue:[ |
|
3976 |
endPos := pos - 1 |
|
3977 |
] |
|
3978 |
]. |
|
5231 | 3979 |
|
3980 |
count < 0 ifTrue:[ |
|
8874 | 3981 |
pos := self size + 1. |
3982 |
[pos > 1 and: [found < count abs]] |
|
3983 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
3984 |
found == count abs ifTrue:[ |
|
3985 |
startPos := pos + 1 |
|
3986 |
] |
|
3987 |
]. |
|
5231 | 3988 |
|
3989 |
^ self copyFrom: startPos to: endPos |
|
3990 |
||
3991 |
" |
|
8874 | 3992 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 3993 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
3994 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
3995 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
3996 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
3997 |
" |
|
5563 | 3998 |
! |
3999 |
||
4000 |
upToAny:aCollectionOfObjects |
|
4001 |
"return a new collection consisting of the receivers elements upTo |
|
4002 |
(but excluding) any in aCollectionOfObjects. |
|
4003 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
4004 |
will consist of all elements of the receiver. |
|
4005 |
See also #upTo:" |
|
4006 |
||
4007 |
|pos| |
|
4008 |
||
4009 |
pos := self indexOfAny:aCollectionOfObjects. |
|
4010 |
pos == 0 ifTrue:[^ self copy]. |
|
4011 |
||
4012 |
^ self copyFrom:1 to:(pos - 1) |
|
4013 |
||
4014 |
" |
|
8874 | 4015 |
'hello world' upToAny:#($/ $:) |
4016 |
'hello:world' upToAny:#($/ $:) |
|
4017 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 4018 |
" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4019 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4020 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4021 |
withoutLeadingForWhich:aCheckBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4022 |
"return a copy of myself without leading eleents for which aCheckBlock returns true. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4023 |
Returns an empty collection, if the receiver consist only of matching elements. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4024 |
Normally, this is mostly used with string receivers." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4025 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4026 |
|index sz| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4027 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4028 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4029 |
index := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4030 |
[index <= sz] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4031 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4032 |
index == 1 ifTrue:[^ self]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4033 |
^ self copyFrom:index |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4034 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4035 |
index := index + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4036 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4037 |
^ self class new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4038 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4039 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4040 |
'****foo****' withoutLeadingForWhich:[:ch | ch isLetter not] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4041 |
#( 0 0 0 1 2 3 4 0 0 0) withoutLeadingForWhich:[:e | e = 0] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4042 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4043 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4044 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4045 |
withoutTrailingForWhich:aCheckBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4046 |
"return a copy of myself without trailing characters for which aCheckBlock returns true. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4047 |
Returns an empty collection, if the receiver consist only of matching chars." |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4048 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4049 |
|index sz| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4050 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4051 |
index := sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4052 |
[index ~~ 0] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4053 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4054 |
index == sz ifTrue:[^ self]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4055 |
^ self copyTo:index |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4056 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4057 |
index := index - 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4058 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4059 |
^ self class new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4060 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4061 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4062 |
' foo....' withoutTrailingForWhich:[:ch | ch isLetter not]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4063 |
#( 0 0 0 1 2 3 4 0 0 0) withoutTrailingForWhich:[:e | e = 0] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4064 |
" |
602 | 4065 |
! ! |
4066 |
||
4067 |
!SequenceableCollection methodsFor:'enumerating'! |
|
4068 |
||
4069 |
collect:aBlock |
|
4070 |
"evaluate the argument, aBlock for every element in the collection |
|
4071 |
and return a collection of the results" |
|
4072 |
||
4073 |
|newCollection |
|
4074 |
sz "{ Class:SmallInteger }"| |
|
4075 |
||
4076 |
sz := self size. |
|
4077 |
newCollection := self copyEmptyAndGrow:sz. |
|
4078 |
1 to:sz do:[:index | |
|
8874 | 4079 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 4080 |
]. |
4081 |
^ newCollection |
|
4082 |
||
4083 |
" |
|
8874 | 4084 |
#(one two three four five six) collect:[:element | element asUppercase] |
4085 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
4086 |
(1 to:9) collect:[:element | element * element] |
|
602 | 4087 |
" |
202 | 4088 |
! |
4089 |
||
602 | 4090 |
do:aBlock |
4091 |
"evaluate the argument, aBlock for every element in the collection." |
|
4092 |
||
4093 |
|stop "{ Class:SmallInteger }"| |
|
4094 |
||
4095 |
stop := self size. |
|
4096 |
1 to:stop do:[:index | |
|
8874 | 4097 |
aBlock value:(self at:index). |
602 | 4098 |
] |
2927 | 4099 |
|
602 | 4100 |
" |
1422 | 4101 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 4102 |
" |
4103 |
! |
|
4104 |
||
2927 | 4105 |
do:aBlock separatedBy:sepBlock |
4106 |
"evaluate the argument, aBlock for every element in the collection. |
|
4107 |
Between each element (i.e. not before the first element and not after the |
|
4108 |
last element), evaluate sepBlock. |
|
4109 |
This supports printing of collections with elements separated by some |
|
4110 |
string." |
|
4111 |
||
4112 |
|stop "{ Class:SmallInteger }"| |
|
4113 |
||
4114 |
stop := self size. |
|
4115 |
stop < 1 ifTrue:[^ self]. |
|
4116 |
aBlock value:(self at:1). |
|
4117 |
2 to:stop do:[:index | |
|
8874 | 4118 |
sepBlock value. |
4119 |
aBlock value:(self at:index) |
|
2927 | 4120 |
]. |
4121 |
||
4122 |
" |
|
4123 |
#(one two three four five six) |
|
8874 | 4124 |
do:[:each | Transcript show:each] |
4125 |
separatedBy:[Transcript show:' , '] |
|
2927 | 4126 |
" |
4127 |
! |
|
3003 | 4128 |
|
3044 | 4129 |
doWithIndex:aBlock |
4130 |
"Squeak compatibility; like keysAndValuesDo:, but passes |
|
4131 |
the index as second argument." |
|
4132 |
||
4133 |
self keysAndValuesDo:[:index :value | |
|
8874 | 4134 |
aBlock value:value value:index |
3044 | 4135 |
]. |
4136 |
||
4137 |
"Created: 17.10.1997 / 12:33:10 / cg" |
|
4138 |
! |
|
4139 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4140 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4141 |
"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
|
4142 |
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
|
4143 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4144 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4145 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4146 |
" |
8874 | 4147 |
#(one two three four five six) |
4148 |
from:2 |
|
4149 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4150 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4151 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4152 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4153 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4154 |
|
602 | 4155 |
from:startIndex do:aBlock |
4156 |
"evaluate the argument, aBlock for the elements starting with the |
|
4157 |
element at startIndex to the end." |
|
4158 |
||
4159 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 4160 |
|
95 | 4161 |
" |
8874 | 4162 |
#(one two three four five six) |
4163 |
from:3 |
|
4164 |
do:[:element | Transcript showCR:element] |
|
602 | 4165 |
" |
4166 |
! |
|
4167 |
||
7490 | 4168 |
from:startIndex keysAndValuesDo:aBlock |
4169 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
4170 |
element at startIndex to the end." |
|
4171 |
||
4172 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
4173 |
||
4174 |
" |
|
8874 | 4175 |
#(one two three four five six) |
4176 |
from:3 |
|
4177 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 4178 |
" |
4179 |
! |
|
4180 |
||
7337 | 4181 |
from:index1 to:index2 by:stepArg do:aBlock |
4182 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
4183 |
||
4184 |
|start "{ Class:SmallInteger }" |
|
4185 |
stop "{ Class:SmallInteger }" |
|
4186 |
step "{ Class:SmallInteger }" | |
|
4187 |
||
4188 |
start := index1. "/ these assignments force type checking... |
|
4189 |
stop := index2. "/ and guarantee inline loop code below. |
|
4190 |
step := stepArg. |
|
4191 |
||
4192 |
start to:stop by:step do:[:index | |
|
8874 | 4193 |
aBlock value:(self at:index). |
7337 | 4194 |
] |
4195 |
||
4196 |
" |
|
8874 | 4197 |
#(one two three four five six seven eight nine ten) |
4198 |
from:2 |
|
4199 |
to:10 |
|
4200 |
by:2 |
|
4201 |
do:[:element | Transcript showCR:element] |
|
7337 | 4202 |
" |
4203 |
! |
|
4204 |
||
602 | 4205 |
from:start to:stop collect:aBlock |
4206 |
"evaluate the argument, aBlock for the elements indexed by start |
|
4207 |
to stop in the collection and return a collection of the results" |
|
4208 |
||
4209 |
|newCollection sz |
|
4210 |
idx "{ Class:SmallInteger }"| |
|
4211 |
||
4212 |
sz := stop - start + 1. |
|
4213 |
newCollection := self copyEmptyAndGrow:sz. |
|
4214 |
idx := 1. |
|
4215 |
start to:stop do:[:index | |
|
8874 | 4216 |
newCollection at:idx put:(aBlock value:(self at:index)). |
4217 |
idx := idx + 1 |
|
602 | 4218 |
]. |
4219 |
^ newCollection |
|
4220 |
||
4221 |
" |
|
8874 | 4222 |
#(one two three four five six) |
4223 |
from:2 |
|
4224 |
to:4 |
|
4225 |
collect:[:element | element asUppercase] |
|
602 | 4226 |
" |
4227 |
! |
|
4228 |
||
4229 |
from:index1 to:index2 do:aBlock |
|
4230 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4231 |
index2 in the collection" |
|
4232 |
||
4233 |
|start "{ Class:SmallInteger }" |
|
4234 |
stop "{ Class:SmallInteger }" | |
|
4235 |
||
7337 | 4236 |
start := index1. "/ these assignments force type checking... |
4237 |
stop := index2. "/ and guarantee inline loop code below. |
|
4238 |
||
602 | 4239 |
start to:stop do:[:index | |
17841 | 4240 |
aBlock value:(self at:index). |
602 | 4241 |
] |
4242 |
||
4243 |
" |
|
8874 | 4244 |
#(one two three four five six) |
17841 | 4245 |
from:3 to:5 do:[:element | Transcript showCR:element] |
4246 |
" |
|
4247 |
||
4248 |
"Modified: / 02-06-2011 / 13:23:06 / cg" |
|
602 | 4249 |
! |
4250 |
||
6868 | 4251 |
from:index1 to:index2 keysAndValuesDo:aBlock |
4252 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4253 |
index2 in the collection; pass both index and value." |
|
4254 |
||
4255 |
|start "{ Class:SmallInteger }" |
|
4256 |
stop "{ Class:SmallInteger }" | |
|
4257 |
||
7337 | 4258 |
start := index1. "/ these assignments force type checking... |
4259 |
stop := index2. "/ and guarantee inline loop code below. |
|
4260 |
||
6868 | 4261 |
start to:stop do:[:index | |
8874 | 4262 |
aBlock value:index value:(self at:index). |
6868 | 4263 |
] |
4264 |
||
4265 |
" |
|
8874 | 4266 |
#(one two three four five six) |
4267 |
from:3 |
|
4268 |
to:5 |
|
4269 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 4270 |
" |
4271 |
! |
|
4272 |
||
17841 | 4273 |
from:index1 to:index2 orEndDo:aBlock |
4274 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4275 |
index2 or the end (whichever comes first) in the collection" |
|
4276 |
||
4277 |
self from:index1 to:(index2 min:self size) do:aBlock |
|
4278 |
||
4279 |
" |
|
4280 |
#(one two three four five six) |
|
4281 |
from:3 to:10 orEndDo:[:element | Transcript showCR:element] |
|
4282 |
" |
|
4283 |
||
4284 |
"Created: / 02-06-2011 / 13:21:32 / cg" |
|
4285 |
! |
|
4286 |
||
602 | 4287 |
from:index1 to:index2 reverseDo:aBlock |
4288 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4289 |
index2 in the collection. Step in reverse order" |
|
4290 |
||
4291 |
|start "{ Class:SmallInteger }" |
|
4292 |
stop "{ Class:SmallInteger }" | |
|
4293 |
||
7337 | 4294 |
start := index1. "/ these assignments force type checking... |
4295 |
stop := index2. "/ and guarantee inline loop code below. |
|
4296 |
||
602 | 4297 |
stop to:start by:-1 do:[:index | |
8874 | 4298 |
aBlock value:(self at:index). |
602 | 4299 |
] |
4300 |
||
4301 |
" |
|
8874 | 4302 |
#(one two three four five six) |
4303 |
from:3 |
|
4304 |
to:5 |
|
4305 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 4306 |
" |
61 | 4307 |
! |
4308 |
||
6027 | 4309 |
from:startIndex to:stopIndex select:aBlock |
4310 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
4311 |
and return a collection of those elements for which the block return true." |
|
4312 |
||
4313 |
|element newColl species needCopy |
|
4314 |
n "{ Class:SmallInteger }"| |
|
4315 |
||
4316 |
n := stopIndex - startIndex + 1. |
|
4317 |
species := self species. |
|
4318 |
species growIsCheap ifFalse:[ |
|
8874 | 4319 |
newColl := OrderedCollection new:n. |
4320 |
needCopy := true |
|
6027 | 4321 |
] ifTrue:[ |
8874 | 4322 |
newColl := self copyEmpty:n. |
4323 |
needCopy := false |
|
6027 | 4324 |
]. |
4325 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4326 |
element := self at:index. |
4327 |
(aBlock value:element) ifTrue:[ |
|
4328 |
newColl add:element |
|
4329 |
]. |
|
6027 | 4330 |
]. |
4331 |
needCopy ifTrue:[ |
|
8874 | 4332 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 4333 |
]. |
4334 |
^ newColl |
|
4335 |
||
4336 |
" |
|
8874 | 4337 |
#(faba one two three four five six) |
4338 |
from:2 to:5 select:[:element | element startsWith:'f'] |
|
6027 | 4339 |
" |
4340 |
! |
|
4341 |
||
10150 | 4342 |
inGroupsOf:n collect:anNArgBlock |
4343 |
"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
|
4344 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4345 |
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
|
4346 |
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
|
4347 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4348 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4349 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4350 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4351 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4352 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4353 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4354 |
|
10150 | 4355 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
4356 |
"/ 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
|
4357 |
n == 2 ifTrue:[ |
10150 | 4358 |
1 to:stop by:n do:[:index | |
4359 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4360 |
newCollection at:dstIdx put:rslt. |
|
4361 |
dstIdx := dstIdx + 1. |
|
4362 |
]. |
|
4363 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4364 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4365 |
n == 3 ifTrue:[ |
10150 | 4366 |
1 to:stop by:n do:[:index | |
4367 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4368 |
newCollection at:dstIdx put:rslt. |
|
4369 |
dstIdx := dstIdx + 1. |
|
4370 |
]. |
|
4371 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4372 |
]. |
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 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4375 |
1 to:stop by:n do:[:index | |
10150 | 4376 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4377 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
4378 |
newCollection at:dstIdx put:rslt. |
|
4379 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4380 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4381 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4382 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4383 |
"for groups of 2, this is the same as: |
8874 | 4384 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4385 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 4386 |
|
4387 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4388 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4389 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4390 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4391 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
4392 |
" |
|
4393 |
||
4394 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
4395 |
! |
|
4396 |
||
4397 |
inGroupsOf:n do:anNArgBlock |
|
4398 |
"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
|
4399 |
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
|
4400 |
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
|
4401 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4402 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4403 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4404 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4405 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4406 |
|
10150 | 4407 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
4408 |
"/ 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
|
4409 |
n == 2 ifTrue:[ |
10150 | 4410 |
1 to:stop by:n do:[:index | |
4411 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4412 |
]. |
|
4413 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4414 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4415 |
n == 3 ifTrue:[ |
10150 | 4416 |
1 to:stop by:n do:[:index | |
4417 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4418 |
]. |
|
4419 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4420 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4421 |
n == 4 ifTrue:[ |
10150 | 4422 |
1 to:stop by:n do:[:index | |
4423 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
4424 |
]. |
|
4425 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4426 |
]. |
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 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4429 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4430 |
1 to:stop by:n do:[:index | |
10150 | 4431 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4432 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4433 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4434 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4435 |
"for groups of 2, this is the same as: |
8874 | 4436 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4437 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4438 |
|
4439 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4440 |
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
|
4441 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4442 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4443 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
4444 |
" |
|
4445 |
||
4446 |
"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
|
4447 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4448 |
|
8853 | 4449 |
keysAndValuesCollect:aTwoArgBlock |
4450 |
"evaluate the argument, aBlock for every element in the collection, |
|
4451 |
passing both index and element as arguments. |
|
4452 |
Collect the returned values and return them." |
|
4453 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4454 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4455 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4456 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4457 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4458 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4459 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 4460 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4461 |
|
8853 | 4462 |
^ newCollection |
4463 |
||
4464 |
" |
|
8874 | 4465 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4466 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4467 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 4468 |
|
4469 |
||
4470 |
|d| |
|
4471 |
||
4472 |
d := Dictionary new. |
|
4473 |
d at:'a' put:'A'. |
|
4474 |
d at:'b' put:'B'. |
|
4475 |
d at:'c' put:'C'. |
|
4476 |
d at:'d' put:'D'. |
|
4477 |
d at:'e' put:'E'. |
|
8874 | 4478 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4479 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4480 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4481 |
ifFalse:[element asLowercase]] |
8853 | 4482 |
" |
4483 |
! |
|
4484 |
||
602 | 4485 |
keysAndValuesDo:aTwoArgBlock |
4486 |
"evaluate the argument, aBlock for every element in the collection, |
|
4487 |
passing both index and element as arguments." |
|
4488 |
||
4489 |
|stop "{ Class:SmallInteger }"| |
|
4490 |
||
4491 |
stop := self size. |
|
4492 |
1 to:stop do:[:index | |
|
8874 | 4493 |
aTwoArgBlock value:index value:(self at:index). |
602 | 4494 |
] |
4495 |
" |
|
8874 | 4496 |
#(one two three four five six) |
4497 |
keysAndValuesDo:[:key :element | |
|
4498 |
Transcript show:key; space; showCR:element |
|
4499 |
] |
|
602 | 4500 |
" |
4501 |
! |
|
4502 |
||
1359 | 4503 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 4504 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 4505 |
in the collection, passing both index and element as arguments." |
4506 |
||
4507 |
|stop "{ Class:SmallInteger }"| |
|
4508 |
||
4509 |
stop := self size. |
|
4510 |
stop to:1 by:-1 do:[:index | |
|
8874 | 4511 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 4512 |
] |
4513 |
||
4514 |
" |
|
8874 | 4515 |
#(one two three four five six) |
4516 |
keysAndValuesReverseDo:[:key :element | |
|
4517 |
Transcript show:key; space; showCR:element |
|
4518 |
] |
|
1359 | 4519 |
" |
4520 |
||
4521 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
4522 |
! |
|
4523 |
||
17797 | 4524 |
keysDo:aBlock |
4525 |
"evaluate the argument, aBlock for every key in the collection. |
|
4526 |
That is: enumerate the indices. |
|
4527 |
Here mostly for protocol compatibility." |
|
4528 |
||
4529 |
1 to:self size do:aBlock |
|
4530 |
||
4531 |
" |
|
4532 |
#(one two three four five six) |
|
4533 |
keysDo:[:key | Transcript show:key; space; showCR:element |
|
4534 |
] |
|
4535 |
" |
|
4536 |
||
4537 |
"Created: / 24-08-2010 / 10:11:23 / cg" |
|
4538 |
! |
|
4539 |
||
602 | 4540 |
nonNilElementsDo:aBlock |
4541 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
4542 |
||
4543 |
|stop "{ Class:SmallInteger }" |
|
4544 |
element| |
|
4545 |
||
4546 |
stop := self size. |
|
4547 |
1 to:stop do:[:index | |
|
8874 | 4548 |
(element := self at:index) notNil ifTrue:[ |
4549 |
aBlock value:element. |
|
4550 |
] |
|
602 | 4551 |
] |
4552 |
" |
|
1422 | 4553 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 4554 |
" |
4555 |
! |
|
4556 |
||
10150 | 4557 |
pairWiseCollect:aTwoArgBlock |
4558 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 4559 |
The block is called with 2 arguments for each 2 elements in the receiver. |
4560 |
An error will be reported, if the number of elements in the receiver |
|
4561 |
is not a multiple of 2. |
|
4562 |
Collect the results and return a new collection containing those." |
|
4563 |
||
10150 | 4564 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 4565 |
|
95 | 4566 |
" |
8874 | 4567 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4568 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 4569 |
|
4570 |
||
4571 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4572 |
pairWiseCollect:[:x :y | x@y] |
4573 |
" |
|
4574 |
||
4575 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
4576 |
! |
|
4577 |
||
4578 |
pairWiseDo:aTwoArgBlock |
|
10385 | 4579 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
4580 |
An error will be reported, if the number of elements in the receiver |
|
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4581 |
is not a multiple of 2. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4582 |
CONFUSION ATTACK: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4583 |
this is different from pairWiseDo:. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4584 |
but the Squeak-pairsDo: does the same as our pairWiseDo: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4585 |
(sigh: but we were first, so they should have adapted...)" |
602 | 4586 |
|
10150 | 4587 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4588 |
|
602 | 4589 |
" |
8874 | 4590 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4591 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4592 |
|
4593 |
||
4594 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4595 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
4596 |
" |
|
4597 |
||
10385 | 4598 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 4599 |
! |
4600 |
||
602 | 4601 |
reverseDo:aBlock |
4602 |
"evaluate the argument, aBlock for every element in the collection |
|
4603 |
in reverse order" |
|
4604 |
||
4605 |
|sz "{ Class:SmallInteger }"| |
|
4606 |
||
4607 |
sz := self size. |
|
4608 |
sz to:1 by:-1 do:[:index | |
|
8874 | 4609 |
aBlock value:(self at:index). |
602 | 4610 |
] |
4611 |
||
4612 |
" |
|
1422 | 4613 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 4614 |
" |
4615 |
! |
|
4616 |
||
4617 |
select:aBlock |
|
4618 |
"evaluate the argument, aBlock for every element in the collection |
|
4619 |
and return a collection of all elements for which the block return |
|
4620 |
true" |
|
4621 |
||
4622 |
|element newColl species needCopy |
|
4623 |
sz "{ Class:SmallInteger }"| |
|
4624 |
||
4625 |
sz := self size. |
|
4626 |
species := self species. |
|
4627 |
species growIsCheap ifFalse:[ |
|
8874 | 4628 |
newColl := OrderedCollection new:sz. |
4629 |
needCopy := true |
|
602 | 4630 |
] ifTrue:[ |
8874 | 4631 |
newColl := self copyEmpty:sz. |
4632 |
needCopy := false |
|
602 | 4633 |
]. |
4634 |
1 to:sz do:[:index | |
|
8874 | 4635 |
element := self at:index. |
4636 |
(aBlock value:element) ifTrue:[ |
|
4637 |
newColl add:element |
|
4638 |
]. |
|
602 | 4639 |
]. |
4640 |
needCopy ifTrue:[ |
|
8874 | 4641 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 4642 |
]. |
61 | 4643 |
^ newColl |
4644 |
||
95 | 4645 |
" |
8874 | 4646 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
4647 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
4648 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
4649 |
(1 to:9) select:[:element | element odd] |
|
4650 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 4651 |
" |
186 | 4652 |
! |
4653 |
||
602 | 4654 |
with:aSequenceableCollection do:aTwoArgBlock |
4655 |
"evaluate the argument, aBlock for successive elements from |
|
4656 |
each the receiver and the argument, aSequenceableCollection. |
|
4657 |
The second argument, aBlock must be a two-argument block. |
|
4658 |
The collection argument must implement access via a numeric key." |
|
4659 |
||
4660 |
|stop "{ Class: SmallInteger }" | |
|
4661 |
||
4662 |
stop := self size. |
|
4663 |
1 to:stop do:[:index | |
|
8874 | 4664 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 4665 |
] |
186 | 4666 |
" |
8874 | 4667 |
#(one two three four five six) |
4668 |
with:(1 to:10) |
|
4669 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
186 | 4670 |
" |
9454 | 4671 |
! |
4672 |
||
4673 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
4674 |
"evaluate the argument, aBlock for successive elements from |
|
4675 |
each the receiver and the argument, aSequenceableCollection. |
|
4676 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4677 |
the index as arguments. |
|
4678 |
The collection argument must implement access via a numeric key." |
|
4679 |
||
4680 |
|stop "{ Class: SmallInteger }" | |
|
4681 |
||
4682 |
stop := self size. |
|
4683 |
1 to:stop do:[:index | |
|
4684 |
aThreeArgBlock |
|
4685 |
value:(self at:index) |
|
4686 |
value:(aSequenceableCollection at:index) |
|
4687 |
value:index. |
|
4688 |
] |
|
4689 |
" |
|
4690 |
#(one two three four five six) |
|
4691 |
with:(1 to:10) |
|
4692 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4693 |
" |
|
4694 |
||
4695 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
1 | 4696 |
! ! |
4697 |
||
4154 | 4698 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 4699 |
|
602 | 4700 |
atAllPut:anObject |
4701 |
"replace all elements of the collection by the argument, anObject. |
|
4702 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4703 |
therefore the change may affect all others referencing the receiver." |
|
4704 |
||
4705 |
self from:1 to:(self size) put:anObject |
|
4706 |
||
4707 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4708 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4709 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4710 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4711 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4712 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4713 |
" |
602 | 4714 |
(Array new:10) atAllPut:1 |
4715 |
(String new:10) atAllPut:$a |
|
4716 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4717 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4718 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 4719 |
! |
4720 |
||
1 | 4721 |
from:index1 to:index2 put:anObject |
4722 |
"replace the elements from index1 to index2 of the collection |
|
44 | 4723 |
by the argument, anObject. |
4724 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4725 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4726 |
|
4727 |
|index "{ Class: SmallInteger }" |
|
4728 |
end "{ Class: SmallInteger }"| |
|
4729 |
||
4730 |
index := index1. |
|
4731 |
end := index2. |
|
4732 |
[index <= end] whileTrue:[ |
|
8874 | 4733 |
self at:index put:anObject. |
4734 |
index := index + 1 |
|
1 | 4735 |
] |
44 | 4736 |
|
95 | 4737 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4738 |
args: index1 : <integer> |
8874 | 4739 |
index2 : <integer> |
4740 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4741 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4742 |
returns: self |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4745 |
" |
95 | 4746 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
4747 |
'abcdefghijkl' from:3 to:6 put:$X |
|
4748 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4749 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4750 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 4751 |
! |
4752 |
||
3003 | 4753 |
replaceAll:oldObject by:newObject |
4754 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
4755 |
Pleace do no longer use this one; |
|
4756 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
4757 |
||
5872 | 4758 |
<resource:#obsolete> |
4759 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4760 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 4761 |
^ self replaceAll:oldObject with:newObject |
4762 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4763 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4764 |
args: oldObject : <object> |
8874 | 4765 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4766 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4767 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4768 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4769 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4770 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 4771 |
! |
4772 |
||
4773 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
4774 |
"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
|
4775 |
Please do no longer use this one; |
3003 | 4776 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
4777 |
||
5872 | 4778 |
<resource:#obsolete> |
4779 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4780 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 4781 |
^ 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
|
4782 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4783 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4784 |
args: oldObject : <object> |
8874 | 4785 |
newObject : <object> |
4786 |
startIndex : <integer> |
|
4787 |
stopIndex : <integer> |
|
4788 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4789 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4790 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4791 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4792 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 4793 |
! |
4794 |
||
2927 | 4795 |
replaceAll:oldObject with:newObject |
44 | 4796 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4797 |
|
44 | 4798 |
Notice: This operation modifies the receiver, NOT a copy; |
4799 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4800 |
|
8874 | 4801 |
^ self |
4802 |
replaceAll:oldObject |
|
4803 |
with:newObject |
|
4804 |
from:1 |
|
4805 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4806 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4807 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4808 |
args: oldObject : <object> |
8874 | 4809 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4810 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4811 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4812 |
" |
362 | 4813 |
|
4814 |
" |
|
8874 | 4815 |
'123123abc123' replaceAll:$1 with:$* |
4816 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4817 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4818 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4819 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4820 |
! |
293 | 4821 |
|
2927 | 4822 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4823 |
"replace all oldObjects found between startIndex and endIndex, |
4824 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4825 |
|
362 | 4826 |
Notice: This operation modifies the receiver, NOT a copy; |
4827 |
therefore the change may affect all others referencing the receiver." |
|
4828 |
||
4829 |
|start "{ Class: SmallInteger }" |
|
4830 |
stop "{ Class: SmallInteger }"| |
|
4831 |
||
4832 |
start := startIndex. |
|
4833 |
stop := stopIndex. |
|
4834 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4835 |
(self at:index) = oldObject ifTrue:[ |
4836 |
self at:index put:newObject |
|
4837 |
] |
|
1 | 4838 |
] |
44 | 4839 |
|
95 | 4840 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4841 |
args: oldObject : <object> |
8874 | 4842 |
newObject : <object> |
4843 |
startIndex : <integer> |
|
4844 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4845 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4846 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4847 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4848 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4849 |
" |
2980 | 4850 |
'123123abc123' replaceAll:$1 with:$* |
4851 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
4852 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 4853 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4854 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4855 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 4856 |
! |
4857 |
||
7615 | 4858 |
replaceAllForWhich:aConditionBlock with:newObject |
4859 |
"replace all elements for which aConditionBlock returns true |
|
4860 |
between startIndex and endIndex, by newObject in the receiver. |
|
4861 |
||
4862 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4863 |
therefore the change may affect all others referencing the receiver." |
|
4864 |
||
8874 | 4865 |
self |
4866 |
replaceAllForWhich:aConditionBlock with:newObject |
|
4867 |
from:1 to:(self size) |
|
7615 | 4868 |
|
4869 |
" |
|
4870 |
args: aConditionBlock : <block> |
|
8874 | 4871 |
newObject : <object> |
7615 | 4872 |
|
4873 |
returns: self |
|
4874 |
" |
|
4875 |
! |
|
4876 |
||
4877 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
4878 |
"replace all elements for which aConditionBlock returns true |
|
4879 |
between startIndex and endIndex, by newObject in the receiver. |
|
4880 |
||
4881 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4882 |
therefore the change may affect all others referencing the receiver." |
|
4883 |
||
4884 |
|start "{ Class: SmallInteger }" |
|
4885 |
stop "{ Class: SmallInteger }"| |
|
4886 |
||
4887 |
start := startIndex. |
|
4888 |
stop := stopIndex. |
|
4889 |
startIndex to:stopIndex do:[:index | |
|
8874 | 4890 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
4891 |
self at:index put:newObject |
|
4892 |
] |
|
7615 | 4893 |
] |
4894 |
||
4895 |
" |
|
4896 |
args: aConditionBlock : <block> |
|
8874 | 4897 |
newObject : <object> |
4898 |
startIndex : <integer> |
|
4899 |
stopIndex : <integer> |
|
7615 | 4900 |
|
4901 |
returns: self |
|
4902 |
" |
|
4903 |
! |
|
4904 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4905 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4906 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4907 |
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
|
4908 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4909 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4910 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4911 |
|
8874 | 4912 |
^ self |
4913 |
replaceAllIdentical:oldObject |
|
4914 |
with:newObject |
|
4915 |
from:1 |
|
4916 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4917 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4918 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4919 |
args: oldObject : <object> |
8874 | 4920 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4921 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4922 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4923 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4924 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4925 |
" |
8874 | 4926 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
4927 |
#(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
|
4928 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4929 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4930 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4931 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4932 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4933 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4934 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4935 |
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
|
4936 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4937 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4938 |
therefore the change may affect all others referencing the receiver." |
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 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4941 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4942 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4943 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4944 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4945 |
startIndex to:stopIndex do:[:index | |
8874 | 4946 |
(self at:index) == oldObject ifTrue:[ |
4947 |
self at:index put:newObject |
|
4948 |
] |
|
5320
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 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4951 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4952 |
args: oldObject : <object> |
8874 | 4953 |
newObject : <object> |
4954 |
startIndex : <integer> |
|
4955 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4956 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4957 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4958 |
" |
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 |
" |
8874 | 4961 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
4962 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
4963 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 4964 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4965 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
4966 |
|
3003 | 4967 |
replaceAny:aCollection by:newObject |
4968 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 4969 |
Pleace do no longer use this one; |
3003 | 4970 |
instead use #replaceAny:with: for naming consistence." |
4971 |
||
5872 | 4972 |
<resource:#obsolete> |
4973 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4974 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 4975 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 4976 |
|
4977 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4978 |
args: aCollection : <colleciton of <object> > |
8874 | 4979 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4980 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4981 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4982 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4983 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4984 |
" |
3003 | 4985 |
'123123abc123' replaceAny:#($1 $2) with:$* |
4986 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
4987 |
" |
|
4988 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4989 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 4990 |
! |
4991 |
||
3003 | 4992 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
4993 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 4994 |
Pleace do no longer use this one; |
3003 | 4995 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 4996 |
|
5872 | 4997 |
<resource:#obsolete> |
4998 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4999 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 5000 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 5001 |
|
5002 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5003 |
args: aCollection : <colleciton of <object> > |
8874 | 5004 |
newObject : <object> |
5005 |
startIndex : <integer> |
|
5006 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5007 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5008 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5009 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5010 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5011 |
" |
8874 | 5012 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
5013 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 5014 |
" |
5015 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5016 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 5017 |
! |
5018 |
||
5019 |
replaceAny:aCollection with:newObject |
|
5020 |
"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
|
5021 |
|
2927 | 5022 |
Notice: This operation modifies the receiver, NOT a copy; |
5023 |
therefore the change may affect all others referencing the receiver." |
|
5024 |
||
8874 | 5025 |
^ self |
10439 | 5026 |
replaceAny:aCollection |
5027 |
with:newObject |
|
5028 |
from:1 |
|
5029 |
to:self size |
|
5030 |
||
5031 |
" |
|
5032 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
5033 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
5034 |
" |
|
5035 |
||
5036 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 5037 |
! |
5038 |
||
2927 | 5039 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 5040 |
"replace all elements within a range, |
5041 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5042 |
|
362 | 5043 |
Notice: This operation modifies the receiver, NOT a copy; |
5044 |
therefore the change may affect all others referencing the receiver." |
|
5045 |
||
5046 |
|start "{ Class: SmallInteger }" |
|
5047 |
stop "{ Class: SmallInteger }"| |
|
5048 |
||
5049 |
start := startIndex. |
|
5050 |
stop := stopIndex. |
|
5051 |
startIndex to:stopIndex do:[:index | |
|
8874 | 5052 |
(aCollection includes:(self at:index)) ifTrue:[ |
5053 |
self at:index put:newObject |
|
5054 |
] |
|
95 | 5055 |
] |
5056 |
||
5057 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5058 |
args: aCollection : <colleciton of <object> > |
8874 | 5059 |
newObject : <object> |
5060 |
startIndex : <integer> |
|
5061 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5062 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5063 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5064 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5065 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5066 |
" |
8874 | 5067 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
5068 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 5069 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5070 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5071 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 5072 |
! |
5073 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5074 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5075 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5076 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5077 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5078 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5079 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5080 |
|
8874 | 5081 |
^ self |
5082 |
replaceFrom:startIndex |
|
5083 |
to:(startIndex + numberOfElements - 1) |
|
5084 |
with:replacementCollection |
|
5085 |
startingAt:1 |
|
5086 |
||
5087 |
" |
|
5088 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5089 |
#($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
|
5090 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5091 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5092 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5093 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5094 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5095 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5096 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5097 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5098 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5099 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5100 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5101 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5102 |
^ self |
8874 | 5103 |
replaceFrom:startIndex |
5104 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
5105 |
with:replacementCollection |
|
5106 |
startingAt:repStartIndex |
|
5107 |
||
5108 |
" |
|
5109 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
5110 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
5111 |
#($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
|
5112 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5113 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5114 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5115 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5116 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5117 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5118 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 5119 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5120 |
|
44 | 5121 |
Notice: This operation modifies the receiver, NOT a copy; |
5122 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5123 |
|
8874 | 5124 |
^ self |
5125 |
replaceFrom:startIndex |
|
5126 |
to:stopIndex |
|
5127 |
with:replacementCollection |
|
5128 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5129 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5130 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5131 |
args: startIndex : <integer> |
8874 | 5132 |
stopIndex : <integer> |
5133 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5134 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5135 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5136 |
" |
61 | 5137 |
|
95 | 5138 |
" |
5139 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
5140 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
5141 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5142 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5143 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 5144 |
! |
5145 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5146 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5147 |
"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
|
5148 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 5149 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5150 |
|
44 | 5151 |
Notice: This operation modifies the receiver, NOT a copy; |
5152 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5153 |
|
5154 |
|srcIndex "{ Class: SmallInteger }" |
|
5155 |
dstIndex "{ Class: SmallInteger }" |
|
5156 |
end "{ Class: SmallInteger }" | |
|
5157 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5158 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 5159 |
" must do reverse copy " |
5160 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
5161 |
dstIndex := stopIndex. |
|
5162 |
end := startIndex. |
|
5163 |
[dstIndex >= end] whileTrue:[ |
|
5164 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
5165 |
srcIndex := srcIndex - 1. |
|
5166 |
dstIndex := dstIndex - 1 |
|
5167 |
]. |
|
5168 |
^ self |
|
1 | 5169 |
]. |
5170 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5171 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5172 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5173 |
end := stopIndex. |
1 | 5174 |
[dstIndex <= end] whileTrue:[ |
8874 | 5175 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
5176 |
srcIndex := srcIndex + 1. |
|
5177 |
dstIndex := dstIndex + 1 |
|
1 | 5178 |
] |
61 | 5179 |
|
95 | 5180 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5181 |
args: startIndex : <integer> |
8874 | 5182 |
stopIndex : <integer> |
5183 |
replacementCollection : <colleciton of <object> > |
|
5184 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5185 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5186 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5187 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5188 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5189 |
" |
8874 | 5190 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
5191 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
5192 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 5193 |
|
5194 |
|c| |
|
5195 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5196 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 5197 |
|
5198 |
|c| |
|
5199 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5200 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 5201 |
" |
3234 | 5202 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5203 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 5204 |
! |
5205 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5206 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5207 |
"replace elements in the receiver starting at startIndex, |
602 | 5208 |
with elements taken from replacementCollection starting at 1 |
5209 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5210 |
|
602 | 5211 |
Notice: This operation modifies the receiver, NOT a copy; |
5212 |
therefore the change may affect all others referencing the receiver." |
|
5213 |
||
8874 | 5214 |
^ self |
5215 |
replaceFrom:startIndex |
|
5216 |
to:(startIndex + replacementCollection size - 1) |
|
5217 |
with:replacementCollection |
|
5218 |
startingAt:1 |
|
3468
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5221 |
args: startIndex : <integer> |
8874 | 5222 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5223 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5224 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5225 |
" |
602 | 5226 |
|
5227 |
" |
|
5228 |
'1234567890' replaceFrom:5 with:'abc' |
|
5229 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
5230 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5231 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5232 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 5233 |
! |
5234 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5235 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5236 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5237 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 5238 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5239 |
|
602 | 5240 |
Notice: This operation modifies the receiver, NOT a copy; |
5241 |
therefore the change may affect all others referencing the receiver." |
|
5242 |
||
8874 | 5243 |
^ self |
5244 |
replaceFrom:startIndex |
|
5245 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
5246 |
with:replacementCollection |
|
5247 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5248 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5249 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5250 |
args: startIndex : <integer> |
8874 | 5251 |
replacementCollection : <colleciton of <object> > |
5252 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5253 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5254 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5255 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5256 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5257 |
" |
8874 | 5258 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 5259 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
5260 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5261 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5262 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 5263 |
! |
5264 |
||
61 | 5265 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
5266 |
"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
|
5267 |
|
61 | 5268 |
Notice: This operation modifies the destination collection, NOT a copy; |
5269 |
therefore the change may affect all others referencing this object." |
|
5270 |
||
8874 | 5271 |
destColl |
5272 |
replaceFrom:destStartIndex |
|
5273 |
to:destEndIndex |
|
5274 |
with:self |
|
5275 |
startingAt:sourceStart |
|
61 | 5276 |
|
5277 |
" |
|
5278 |
|s| |
|
5279 |
s := 'abcdefghijklmnop'. |
|
5280 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
5281 |
s'123defghijklmnop' |
|
5282 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5283 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5284 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 5285 |
! ! |
5286 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5287 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5288 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5289 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5290 |
"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
|
5291 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5292 |
Elements on the left are filled with padElement. |
8874 | 5293 |
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
|
5294 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5295 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5296 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5297 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5298 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5299 |
(len < size) ifTrue:[ |
8874 | 5300 |
s := self species new:size withAll:padElement. |
5301 |
s replaceFrom:(size - len + 1) with:self. |
|
5302 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5303 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5304 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5305 |
" |
8874 | 5306 |
'foo' leftPaddedTo:10 with:$. |
5307 |
'fooBar' leftPaddedTo:5 with:$. |
|
5308 |
123 printString leftPaddedTo:10 with:$. |
|
5309 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
5310 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
5311 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
5312 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
5313 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
5314 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
5315 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5316 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5317 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5318 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5319 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5320 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5321 |
plus pad elements up to length. |
8874 | 5322 |
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
|
5323 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5324 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5325 |
|s len| |
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 := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5328 |
len < newSize ifTrue:[ |
8874 | 5329 |
s := self species new:newSize withAll:padElement. |
5330 |
s replaceFrom:1 to:len with:self. |
|
5331 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5332 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5333 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5334 |
" |
8874 | 5335 |
'foo' paddedTo:10 with:$. |
5336 |
123 printString paddedTo:10 with:$* |
|
5337 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
5338 |
(Float pi printString) paddedTo:15 with:$- |
|
5339 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
5340 |
#[1 2 3 4] paddedTo:6 with:0 |
|
5341 |
#[1 2 3 4] paddedTo:10 with:99 |
|
5342 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5343 |
" |
7116 | 5344 |
! |
5345 |
||
5346 |
paddedToMultipleOf:sizeModulu with:padElement |
|
5347 |
|mySize mod numPad| |
|
5348 |
||
5349 |
mySize := self size. |
|
5350 |
mod := (mySize \\ sizeModulu). |
|
5351 |
mod == 0 ifTrue:[^ self]. |
|
5352 |
||
5353 |
numPad := sizeModulu - mod. |
|
5354 |
^ self , (self species new:numPad withAll:padElement). |
|
5355 |
||
5356 |
" |
|
8874 | 5357 |
#[] paddedToMultipleOf:3 with:0 |
5358 |
#[1] paddedToMultipleOf:3 with:0 |
|
5359 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
5360 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 5361 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
5362 |
||
5363 |
#() paddedToMultipleOf:3 with:nil |
|
5364 |
#(1) paddedToMultipleOf:3 with:nil |
|
5365 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
5366 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
5367 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
5368 |
||
5369 |
'' paddedToMultipleOf:3 with:Character space |
|
5370 |
'a' paddedToMultipleOf:3 with:Character space |
|
5371 |
'ab' paddedToMultipleOf:3 with:Character space |
|
5372 |
'abc' paddedToMultipleOf:3 with:Character space |
|
5373 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
5374 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5375 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5376 |
|
7260 | 5377 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 5378 |
|
5358 | 5379 |
mergeFirst: first middle: middle last: last into: dst by: aBlock |
8874 | 5380 |
"Private!! |
5358 | 5381 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
5382 |
||
5383 |
| i1 i2 val1 val2 out | |
|
5384 |
||
5385 |
i1 := first. |
|
5386 |
i2 := middle + 1. |
|
5387 |
val1 := self at: i1. |
|
5388 |
val2 := self at: i2. |
|
5389 |
out := first - 1. "will be pre-incremented" |
|
5390 |
||
5391 |
"select 'lower' half of the elements based on comparator" |
|
5392 |
[(i1 <= middle) and: [i2 <= last]] whileTrue: [ |
|
8874 | 5393 |
(aBlock value: val1 value: val2) |
5394 |
ifTrue: [ |
|
5395 |
dst at: (out := out + 1) put: val1. |
|
5396 |
val1 := self at: (i1 := i1 + 1)] |
|
5397 |
ifFalse: [ |
|
5398 |
dst at: (out := out + 1) put: val2. |
|
5399 |
i2 := i2 + 1. |
|
5400 |
i2 <= last ifTrue: [val2 := self at: i2]]]. |
|
5358 | 5401 |
|
5402 |
"copy the remaining elements" |
|
5403 |
i1 <= middle |
|
8874 | 5404 |
ifTrue: [ |
5405 |
dst replaceFrom: out + 1 to: last with: self startingAt: i1] |
|
5406 |
ifFalse: [ |
|
5407 |
dst replaceFrom: out + 1 to: last with: self startingAt: i2]. |
|
5358 | 5408 |
|
5409 |
! |
|
5410 |
||
5411 |
mergeSortFrom: first to: last src: src dst: dst by: aBlock |
|
5412 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
|
5413 |
||
5414 |
| middle | |
|
5415 |
||
5416 |
first = last ifTrue: [^ self]. |
|
5417 |
middle := (first + last) // 2. |
|
5418 |
self mergeSortFrom: first to: middle src: dst dst: src by: aBlock. |
|
5419 |
self mergeSortFrom: middle + 1 to: last src: dst dst: src by: aBlock. |
|
5420 |
src mergeFirst: first middle: middle last: last into: dst by: aBlock. |
|
5421 |
||
5422 |
||
5423 |
! |
|
5424 |
||
2069 | 5425 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5426 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5427 |
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
|
5428 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5429 |
|
8874 | 5430 |
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
|
5431 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5432 |
|
5433 |
|begin "{ Class: SmallInteger }" |
|
5434 |
end "{ Class: SmallInteger }" |
|
5435 |
b "{ Class: SmallInteger }" |
|
5436 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5437 |
middleElement temp1 temp2 stack | |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5438 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5439 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5440 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5441 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5442 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5443 |
[stack notEmpty] whileTrue:[ |
8874 | 5444 |
end := stack removeLast. |
5445 |
begin := stack removeLast. |
|
5446 |
||
5447 |
b := begin. |
|
5448 |
e := end. |
|
5449 |
middleElement := self at:((b + e) // 2). |
|
5450 |
||
5451 |
[b < e] whileTrue:[ |
|
5452 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5453 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5454 |
||
5455 |
(b <= e) ifTrue:[ |
|
5456 |
(b == e) ifFalse:[ |
|
5457 |
temp1 := self at:b. temp2 := self at:e. |
|
5458 |
self at:b put:temp2. self at:e put:temp1 |
|
5459 |
]. |
|
5460 |
b := b + 1. |
|
5461 |
e := e - 1 |
|
5462 |
] |
|
5463 |
]. |
|
5464 |
(begin < e) ifTrue:[ |
|
5465 |
stack add:begin. |
|
5466 |
stack add:e. |
|
5467 |
]. |
|
5468 |
(b < end) ifTrue:[ |
|
5469 |
stack add:b. |
|
5470 |
stack add:end. |
|
5471 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5472 |
] |
2069 | 5473 |
! |
5474 |
||
5475 |
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
|
5476 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5477 |
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
|
5478 |
with degenerated collections." |
2069 | 5479 |
|
5480 |
|begin "{ Class: SmallInteger }" |
|
5481 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5482 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5483 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5484 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5485 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5486 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5487 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5488 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5489 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5490 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5491 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5492 |
[stack notEmpty] whileTrue:[ |
8874 | 5493 |
end := stack removeLast. |
5494 |
begin := stack removeLast. |
|
5495 |
||
5496 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5497 |
elB := self at:begin. |
|
5498 |
elE := self at:end. |
|
5499 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5500 |
temp := elB. elB := elE. elE := temp. |
|
5501 |
self at:begin put:elB. |
|
5502 |
self at:end put:elE. |
|
5503 |
]. |
|
5504 |
||
5505 |
nToSort > 2 ifTrue:[ |
|
5506 |
m := (begin + end) // 2. |
|
5507 |
elM := self at:m. |
|
5508 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5509 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5510 |
temp := self at:end. |
|
5511 |
self at:end put:(self at:m). |
|
5512 |
self at:m put:temp. |
|
5513 |
elM := elE |
|
5514 |
] |
|
5515 |
] ifFalse:[ |
|
5516 |
temp := self at:begin. |
|
5517 |
self at:begin put:(self at:m). |
|
5518 |
self at:m put:temp. |
|
5519 |
elM := elB |
|
5520 |
]. |
|
5521 |
nToSort > 3 ifTrue:[ |
|
5522 |
bRun := begin. |
|
5523 |
eRun := end. |
|
5524 |
[ |
|
5525 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5526 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5527 |
bRun <= eRun |
|
5528 |
] whileTrue:[ |
|
5529 |
temp := self at:bRun. |
|
5530 |
self at:bRun put:(self at:eRun). |
|
5531 |
self at:eRun put:temp. |
|
5532 |
]. |
|
5533 |
stack add:begin. |
|
5534 |
stack add:eRun. |
|
5535 |
||
5536 |
stack add:bRun. |
|
5537 |
stack add:end. |
|
5538 |
] |
|
5539 |
] |
|
5540 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5541 |
] |
2069 | 5542 |
! |
5543 |
||
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5544 |
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
|
5545 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5546 |
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
|
5547 |
with degenerated collections." |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5548 |
|
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5549 |
|begin "{ Class: SmallInteger }" |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5550 |
end "{ Class: SmallInteger }" |
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5551 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5552 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5553 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5554 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5555 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5556 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5557 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5558 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5559 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5560 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5561 |
[stack notEmpty] whileTrue:[ |
8874 | 5562 |
end := stack removeLast. |
5563 |
begin := stack removeLast. |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5564 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5565 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
8874 | 5566 |
elB := self at:begin. |
5567 |
elE := self at:end. |
|
5568 |
(sortBlock value:p value:elB value:elE) ifFalse:[ |
|
5569 |
temp := elB. elB := elE. elE := temp. |
|
5570 |
self at:begin put:elB. |
|
5571 |
self at:end put:elE. |
|
5572 |
]. |
|
5573 |
||
5574 |
nToSort > 2 ifTrue:[ |
|
5575 |
m := (begin + end) // 2. |
|
5576 |
elM := self at:m. |
|
5577 |
(sortBlock value:p value:elB value:elM) ifTrue:[ |
|
5578 |
(sortBlock value:p value:elM value:elE) ifFalse:[ |
|
5579 |
temp := self at:end. |
|
5580 |
self at:end put:(self at:m). |
|
5581 |
self at:m put:temp. |
|
5582 |
elM := elE |
|
5583 |
] |
|
5584 |
] ifFalse:[ |
|
5585 |
temp := self at:begin. |
|
5586 |
self at:begin put:(self at:m). |
|
5587 |
self at:m put:temp. |
|
5588 |
elM := elB |
|
5589 |
]. |
|
5590 |
nToSort > 3 ifTrue:[ |
|
5591 |
bRun := begin. |
|
5592 |
eRun := end. |
|
5593 |
[ |
|
5594 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:p value:elM value: (self at:eRun)]] whileTrue. |
|
5595 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:p value:(self at:bRun) value:elM]] whileTrue. |
|
5596 |
bRun <= eRun |
|
5597 |
] whileTrue:[ |
|
5598 |
temp := self at:bRun. |
|
5599 |
self at:bRun put:(self at:eRun). |
|
5600 |
self at:eRun put:temp. |
|
5601 |
]. |
|
5602 |
stack add:begin. |
|
5603 |
stack add:eRun. |
|
5604 |
||
5605 |
stack add:bRun. |
|
5606 |
stack add:end. |
|
5607 |
] |
|
5608 |
] |
|
5609 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5610 |
] |
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5611 |
! |
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
5612 |
|
2069 | 5613 |
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
|
5614 |
"actual quicksort worker for sort:-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5615 |
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
|
5616 |
with degenerated collections." |
2069 | 5617 |
|
5618 |
|begin "{ Class: SmallInteger }" |
|
5619 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5620 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5621 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5622 |
m "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5623 |
nToSort "{ Class: SmallInteger }" |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5624 |
elB elM elE temp stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5625 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5626 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5627 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5628 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5629 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5630 |
[stack notEmpty] whileTrue:[ |
8874 | 5631 |
end := stack removeLast. |
5632 |
begin := stack removeLast. |
|
5633 |
||
5634 |
(nToSort := end + 1 - begin) > 1 ifTrue:[ |
|
5635 |
||
5636 |
elB := self at:begin. |
|
5637 |
elE := self at:end. |
|
5638 |
(sortBlock value:elB value:elE) ifFalse:[ |
|
5639 |
temp := elB. elB := elE. elE := temp. |
|
5640 |
self at:begin put:elB. |
|
5641 |
self at:end put:elE. |
|
5642 |
||
5643 |
temp := aCollection at:end. |
|
5644 |
aCollection at:end put:(aCollection at:begin). |
|
5645 |
aCollection at:begin put:temp. |
|
5646 |
]. |
|
5647 |
||
5648 |
nToSort > 2 ifTrue:[ |
|
5649 |
m := (begin + end) // 2. |
|
5650 |
elM := self at:m. |
|
5651 |
(sortBlock value:elB value:elM) ifTrue:[ |
|
5652 |
(sortBlock value:elM value:elE) ifFalse:[ |
|
5653 |
temp := self at:end. |
|
5654 |
self at:end put:(self at:m). |
|
5655 |
self at:m put:temp. |
|
5656 |
||
5657 |
temp := aCollection at:end. |
|
5658 |
aCollection at:end put:(aCollection at:m). |
|
5659 |
aCollection at:m put:temp. |
|
5660 |
||
5661 |
elM := elE |
|
5662 |
] |
|
5663 |
] ifFalse:[ |
|
5664 |
temp := self at:begin. |
|
5665 |
self at:begin put:(self at:m). |
|
5666 |
self at:m put:temp. |
|
5667 |
||
5668 |
temp := aCollection at:begin. |
|
5669 |
aCollection at:begin put:(aCollection at:m). |
|
5670 |
aCollection at:m put:temp. |
|
5671 |
elM := elB |
|
5672 |
]. |
|
5673 |
nToSort > 3 ifTrue:[ |
|
5674 |
bRun := begin. |
|
5675 |
eRun := end. |
|
5676 |
[ |
|
5677 |
[eRun := eRun - 1. bRun <= eRun and:[sortBlock value:elM value: (self at:eRun)]] whileTrue. |
|
5678 |
[bRun := bRun + 1. bRun <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
5679 |
bRun <= eRun |
|
5680 |
] whileTrue:[ |
|
5681 |
temp := self at:bRun. |
|
5682 |
self at:bRun put:(self at:eRun). |
|
5683 |
self at:eRun put:temp. |
|
5684 |
||
5685 |
temp := aCollection at:bRun. |
|
5686 |
aCollection at:bRun put:(aCollection at:eRun). |
|
5687 |
aCollection at:eRun put:temp. |
|
5688 |
]. |
|
5689 |
stack add:begin. |
|
5690 |
stack add:eRun. |
|
5691 |
||
5692 |
stack add:bRun. |
|
5693 |
stack add:end. |
|
5694 |
] |
|
5695 |
] |
|
5696 |
] |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5697 |
] |
2069 | 5698 |
! |
5699 |
||
5700 |
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
|
5701 |
"actual quicksort worker for sortWith-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5702 |
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
|
5703 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5704 |
|
8874 | 5705 |
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
|
5706 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 5707 |
|
5708 |
|begin "{ Class: SmallInteger }" |
|
5709 |
end "{ Class: SmallInteger }" |
|
5710 |
b "{ Class: SmallInteger }" |
|
5711 |
e "{ Class: SmallInteger }" |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5712 |
middleElement temp1 temp2 stack| |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5713 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5714 |
stack := OrderedCollection new. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5715 |
stack add:inBegin. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5716 |
stack add:inEnd. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5717 |
|
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5718 |
[stack notEmpty] whileTrue:[ |
8874 | 5719 |
end := stack removeLast. |
5720 |
begin := stack removeLast. |
|
5721 |
||
5722 |
b := begin. |
|
5723 |
e := end. |
|
5724 |
middleElement := self at:((b + e) // 2). |
|
5725 |
||
5726 |
[b < e] whileTrue:[ |
|
5727 |
[b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5728 |
[e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5729 |
||
5730 |
(b <= e) ifTrue:[ |
|
5731 |
(b == e) ifFalse:[ |
|
5732 |
temp1 := self at:b. temp2 := self at:e. |
|
5733 |
self at:b put:temp2. self at:e put:temp1. |
|
5734 |
temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5735 |
aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5736 |
]. |
|
5737 |
b := b + 1. |
|
5738 |
e := e - 1 |
|
5739 |
] |
|
5740 |
]. |
|
5741 |
(begin < e) ifTrue:[ |
|
5742 |
stack add:begin. |
|
5743 |
stack add:e. |
|
5744 |
]. |
|
5745 |
(b < end) ifTrue:[ |
|
5746 |
stack add:b. |
|
5747 |
stack add:end. |
|
5748 |
] |
|
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5749 |
] |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5750 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5751 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5752 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5753 |
"actual randomizedQuicksort worker for sort:with:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5754 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5755 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5756 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5757 |
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
|
5758 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5759 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5760 |
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
|
5761 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5762 |
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
|
5763 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5764 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5765 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5766 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5767 |
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
|
5768 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5769 |
"/ 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
|
5770 |
^ 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
|
5771 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5772 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5773 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5774 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5775 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5776 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5777 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5778 |
"/ middleElement temp1 temp2 | |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5779 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5780 |
"/ 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
|
5781 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5782 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5783 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5784 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5785 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5786 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5787 |
"/ "/ 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
|
5788 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5789 |
"/ 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
|
5790 |
"/ 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
|
5791 |
"/ 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
|
5792 |
"/ aCollection notNil ifTrue:[ |
8874 | 5793 |
"/ 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
|
5794 |
"/ 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
|
5795 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5796 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5797 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5798 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5799 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5800 |
"/ 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
|
5801 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5802 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5803 |
"/ [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
|
5804 |
"/ [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
|
5805 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5806 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5807 |
"/ (b == e) ifFalse:[ |
8874 | 5808 |
"/ 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
|
5809 |
"/ 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
|
5810 |
"/ aCollection notNil ifTrue:[ |
8874 | 5811 |
"/ 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
|
5812 |
"/ 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
|
5813 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5814 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5815 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5816 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5817 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5818 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5819 |
"/ (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
|
5820 |
"/ (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
|
5821 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5822 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5823 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5824 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5825 |
This exchanges a random element within the partition, to avoid |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5826 |
the worst case O-square situation of quickSort. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5827 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5828 |
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
|
5829 |
runTime, due to the random number generation. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5830 |
The worst case of quickSort is encountered, if the choosen pivot |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5831 |
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
|
5832 |
creates partitions of size 1 and (n-1). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5833 |
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
|
5834 |
to be encountered." |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
5835 |
|
8180
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5836 |
<resource:#obsolete> |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5837 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5838 |
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
|
5839 |
"/ 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
|
5840 |
^ 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
|
5841 |
|
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5842 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5843 |
"/ |begin "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5844 |
"/ end "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5845 |
"/ b "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5846 |
"/ e "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5847 |
"/ rnd "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5848 |
"/ middleElement temp1 temp2 | |
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 := 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
|
5851 |
"/ end := inEnd. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5852 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5853 |
"/ b := begin. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5854 |
"/ e := end. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5855 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5856 |
"/ "/ randomize that partition |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5857 |
"/ "/ 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
|
5858 |
"/ "/ element. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5859 |
"/ 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
|
5860 |
"/ 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
|
5861 |
"/ 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
|
5862 |
"/ aCollection notNil ifTrue:[ |
8874 | 5863 |
"/ 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
|
5864 |
"/ 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
|
5865 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5866 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5867 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5868 |
"/ "/ now proceed as usual |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5869 |
"/ "/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5870 |
"/ 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
|
5871 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5872 |
"/ [b < e] whileTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5873 |
"/ [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
|
5874 |
"/ [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
|
5875 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5876 |
"/ (b <= e) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5877 |
"/ (b == e) ifFalse:[ |
8874 | 5878 |
"/ 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
|
5879 |
"/ 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
|
5880 |
"/ aCollection notNil ifTrue:[ |
8874 | 5881 |
"/ 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
|
5882 |
"/ 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
|
5883 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5884 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5885 |
"/ b := b + 1. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5886 |
"/ e := e - 1 |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5887 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5888 |
"/ ]. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5889 |
"/ (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
|
5890 |
"/ (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
|
5891 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
5892 |
"/ "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
|
5893 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
2069 | 5894 |
! ! |
5895 |
||
602 | 5896 |
!SequenceableCollection methodsFor:'queries'! |
5897 |
||
5898 |
firstIndex |
|
5899 |
"return the first elements index" |
|
5900 |
||
5901 |
^ 1 |
|
5902 |
! |
|
5903 |
||
5904 |
isSequenceable |
|
5905 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
5906 |
||
5907 |
^ true |
|
1 | 5908 |
! |
5909 |
||
602 | 5910 |
isSequenceableCollection |
5911 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
5912 |
This method is a historic leftover and will be removed." |
|
5913 |
||
5872 | 5914 |
<resource:#obsolete> |
5915 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5916 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 5917 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5918 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
5919 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 5920 |
! |
5921 |
||
5358 | 5922 |
isSorted |
5923 |
"return true. if my elements are sorted (already)" |
|
5924 |
||
5925 |
|lastEl el| |
|
5926 |
||
5927 |
lastEl := self first. |
|
5928 |
2 to:self size do:[:i | |
|
8874 | 5929 |
el := self at:i. |
5930 |
el >= lastEl ifFalse: [^ false]. |
|
5931 |
lastEl := el |
|
5358 | 5932 |
]. |
5933 |
^ true |
|
5934 |
||
5935 |
" |
|
8874 | 5936 |
#(1 2 3 5 10 100) isSorted |
5937 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 5938 |
" |
5939 |
! |
|
5940 |
||
5941 |
isSortedBy:aBlock |
|
5942 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
5943 |
||
5944 |
|lastEl el| |
|
5945 |
||
5946 |
lastEl := self first. |
|
5947 |
2 to:self size do:[:i | |
|
8874 | 5948 |
el := self at:i. |
5949 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
5950 |
lastEl := el |
|
5358 | 5951 |
]. |
5952 |
^ true |
|
5953 |
||
5954 |
" |
|
8874 | 5955 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
5956 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
5957 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
5958 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 5959 |
" |
5960 |
||
5961 |
! |
|
5962 |
||
602 | 5963 |
keys |
5964 |
"return a collection with all keys in the Smalltalk dictionary" |
|
5965 |
||
5966 |
|sz| |
|
5967 |
||
1 | 5968 |
sz := self size. |
602 | 5969 |
sz == 0 ifTrue:[ |
8874 | 5970 |
^ #() |
61 | 5971 |
]. |
602 | 5972 |
^ 1 to:sz |
1 | 5973 |
! |
5974 |
||
602 | 5975 |
lastIndex |
5976 |
"return the last elements index" |
|
5977 |
||
5978 |
^ self size |
|
61 | 5979 |
! |
5980 |
||
602 | 5981 |
size |
5982 |
"return the number of elements in the collection. |
|
5983 |
concrete implementations must define this" |
|
5984 |
||
5985 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5986 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5987 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5988 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5989 |
"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
|
5990 |
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
|
5991 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5992 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5993 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5994 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
5995 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 5996 |
! ! |
5997 |
||
5998 |
!SequenceableCollection methodsFor:'searching'! |
|
5999 |
||
3887 | 6000 |
detect:aBlock startingAt:startIndex |
6001 |
"find the first element, for which evaluation of the argument, aBlock |
|
6002 |
return true, starting the search with startIndex. |
|
6003 |
If none does so, report an error" |
|
6004 |
||
6005 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
6006 |
||
6007 |
" |
|
8874 | 6008 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
6009 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 6010 |
" |
6011 |
||
6012 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
6013 |
! |
|
6014 |
||
6015 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
6016 |
"find the first element, for which evaluation of the argument, aBlock |
|
6017 |
return true, starting the search with startIndex. |
|
5717 | 6018 |
If none does so, return the evaluation of exceptionBlock" |
6019 |
||
6020 |
|stop| |
|
3887 | 6021 |
|
6022 |
stop := self size. |
|
5717 | 6023 |
self from:startIndex to:stop do:[:el | |
8874 | 6024 |
(aBlock value:el) ifTrue:[ |
6025 |
^ el |
|
6026 |
]. |
|
3887 | 6027 |
]. |
6028 |
^ exceptionBlock value |
|
6029 |
||
6030 |
" |
|
8874 | 6031 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
6032 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 6033 |
" |
6034 |
||
6035 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
6036 |
! |
|
6037 |
||
11705 | 6038 |
findFirst:aBlock ifNone:exceptionalValue |
10453 | 6039 |
"find the index of the first element, for which evaluation of the argument, aBlock |
11705 | 6040 |
returns true; return its index or the value from exceptionalValue if none detected. |
3887 | 6041 |
This is much like #detect, however, here an INDEX is returned, |
6042 |
while #detect returns the element." |
|
602 | 6043 |
|
6044 |
|stop "{ Class: SmallInteger }" | |
|
6045 |
||
32 | 6046 |
stop := self size. |
602 | 6047 |
1 to:stop do:[:index | |
10453 | 6048 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 6049 |
]. |
11705 | 6050 |
^ exceptionalValue value |
6051 |
||
6052 |
" |
|
6053 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
6054 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
6055 |
" |
|
1 | 6056 |
! |
6057 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6058 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6059 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 6060 |
returns true; return its index or 0 if none detected. |
6061 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
6062 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6063 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6064 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6065 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6066 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6067 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6068 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6069 |
start to:stop do:[:index | |
8874 | 6070 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6071 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6072 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6073 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6074 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6075 |
#(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
|
6076 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6077 |
" |
3887 | 6078 |
|
6079 |
"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
|
6080 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6081 |
|
602 | 6082 |
findLast:aBlock |
6083 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 6084 |
returns true. |
6085 |
Return its index or 0 if none detected." |
|
602 | 6086 |
|
6087 |
|start "{ Class: SmallInteger }"| |
|
6088 |
||
6089 |
start := self size. |
|
6090 |
start to:1 by:-1 do:[:index | |
|
8874 | 6091 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 6092 |
]. |
6093 |
^ 0 |
|
6094 |
||
6095 |
" |
|
602 | 6096 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
6097 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 6098 |
" |
6099 |
! |
|
6100 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6101 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6102 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 6103 |
returns true. Start the search at startIndex. |
6104 |
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
|
6105 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6106 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6107 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6108 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6109 |
start to:1 by:-1 do:[:index | |
8874 | 6110 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
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 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6113 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6114 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6115 |
#(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
|
6116 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6117 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6118 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6119 |
|
8193 | 6120 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
6121 |
"find the last element, for which evaluation of the argument, aBlock |
|
6122 |
returns true. Start the search at startIndex. |
|
6123 |
End the search at endIndex or when an element is found. |
|
6124 |
Return its index or 0 if none detected." |
|
6125 |
||
6126 |
|start "{ Class: SmallInteger }"| |
|
6127 |
||
6128 |
start := startIndex. |
|
6129 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 6130 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 6131 |
]. |
6132 |
^ 0 |
|
6133 |
||
6134 |
" |
|
6135 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
6136 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
6137 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
6138 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
6139 |
" |
|
6140 |
! |
|
6141 |
||
159 | 6142 |
indexOfAny:aCollection |
6143 |
"search the collection for an element in aCollection. |
|
6144 |
if found, return the index otherwise return 0. |
|
8874 | 6145 |
The comparison is done using = |
159 | 6146 |
(i.e. equality test - not identity test). |
6147 |
||
6148 |
Notice, that for big collections, the runtime of this search |
|
6149 |
grows proportional to size(receiver) * size(aCollection). |
|
6150 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6151 |
||
6152 |
^ self indexOfAny:aCollection startingAt:1 |
|
6153 |
||
6154 |
" |
|
6155 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
6156 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
6157 |
" |
|
6158 |
! |
|
6159 |
||
6160 |
indexOfAny:aCollection startingAt:start |
|
6161 |
"search the collection for an element in aCollection, |
|
6162 |
starting the search at index start; |
|
6163 |
if found, return the index otherwise return 0. |
|
8874 | 6164 |
The comparison is done using = |
159 | 6165 |
(i.e. equality test - not identity test). |
6166 |
||
6167 |
Notice, that for big collections, the runtime of this search |
|
6168 |
grows proportional to size(receiver) * size(aCollection). |
|
6169 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6170 |
||
6171 |
||
6172 |
|startIndex "{ Class: SmallInteger }" |
|
6173 |
stop "{ Class: SmallInteger }" | |
|
6174 |
||
6175 |
startIndex := start. |
|
6176 |
stop := self size. |
|
6177 |
startIndex to:stop do:[:index | |
|
8874 | 6178 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 6179 |
]. |
6180 |
^ 0 |
|
6181 |
" |
|
8874 | 6182 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
6183 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 6184 |
" |
1 | 6185 |
! |
6186 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6187 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6188 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6189 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6190 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 6191 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6192 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6193 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6194 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6195 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6196 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6197 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6198 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6199 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6200 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6201 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6202 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6203 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6204 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6205 |
" |
8874 | 6206 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
6207 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
6208 |
#(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
|
6209 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6210 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6211 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6212 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6213 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6214 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6215 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6216 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6217 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6218 |
#(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
|
6219 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) |
17815 | 6220 |
'abcdefg' indexOfAnyOf:(CharacterSet newFrom:'cef') |
6221 |
" |
|
6222 |
||
6223 |
"Modified: / 28-01-2011 / 18:03:14 / cg" |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6224 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6225 |
|
8874 | 6226 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6227 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6228 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6229 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6230 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6231 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6232 |
#(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
|
6233 |
#(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
|
6234 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6235 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6236 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6237 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6238 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6239 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6240 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6241 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6242 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6243 |
|
368 | 6244 |
indexOfSubCollection:aCollection |
9234 | 6245 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 6246 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6247 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 6248 |
|
6249 |
" |
|
8874 | 6250 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
6251 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
6252 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
6253 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
6254 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
6255 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
6256 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
6257 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
17841 | 6258 |
'foobarbaz' indexOfSubCollection:'bar' |
6259 |
" |
|
6260 |
||
6261 |
"Modified: / 20-04-2011 / 19:11:20 / cg" |
|
368 | 6262 |
! |
6263 |
||
602 | 6264 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
6265 |
"find a subcollection. If found, return the index; |
|
6266 |
if not found, return the result of evaluating exceptionBlock." |
|
6267 |
||
6268 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
6269 |
! |
|
6270 |
||
368 | 6271 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6272 |
"find a subcollection starting at index. |
368 | 6273 |
If found, return the index; if not found, return 0." |
6274 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6275 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 6276 |
! |
6277 |
||
6278 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
6279 |
"find a subcollection, starting at index. If found, return the index; |
|
6280 |
if not found, return the result of evaluating exceptionBlock. |
|
6281 |
This is a q&d hack - not very efficient" |
|
6282 |
||
8874 | 6283 |
|same first |
368 | 6284 |
mySize "{Class: SmallInteger }" |
6285 |
checkIndex "{Class: SmallInteger }" |
|
6286 |
cmpIndex "{Class: SmallInteger }" |
|
6287 |
sz "{Class: SmallInteger }"| |
|
6288 |
||
6289 |
mySize := self size. |
|
6290 |
sz := aCollection size. |
|
6291 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
6292 |
first := aCollection at:1. |
|
6293 |
||
8874 | 6294 |
checkIndex := startIndex - 1. |
368 | 6295 |
[true] whileTrue:[ |
8874 | 6296 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
6297 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6298 |
||
6299 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
6300 |
^ 0 |
|
6301 |
]. |
|
6302 |
same := true. |
|
6303 |
cmpIndex := 1. |
|
6304 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6305 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
6306 |
same := false |
|
6307 |
] ifTrue:[ |
|
6308 |
cmpIndex := cmpIndex + 1 |
|
6309 |
] |
|
6310 |
]. |
|
6311 |
same ifTrue:[^ checkIndex]. |
|
6312 |
] |
|
6313 |
||
6314 |
" |
|
6315 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
6316 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
6317 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
6318 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
6319 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
6320 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 6321 |
" |
602 | 6322 |
! |
6323 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6324 |
lastIndexOfSubCollection:aCollection |
8874 | 6325 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6326 |
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
|
6327 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6328 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6329 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6330 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6331 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6332 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6333 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6334 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6335 |
"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
|
6336 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6337 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6338 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6339 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6340 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6341 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6342 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6343 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6344 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6345 |
"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
|
6346 |
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
|
6347 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6348 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6349 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6350 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6351 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6352 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6353 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6354 |
"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
|
6355 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6356 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6357 |
|
8874 | 6358 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6359 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6360 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6361 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6362 |
sz "{Class: SmallInteger }"| |
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 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6365 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6366 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6367 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6368 |
|
8874 | 6369 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6370 |
checkIndex > mySize ifTrue:[ |
8874 | 6371 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6372 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6373 |
[true] whileTrue:[ |
8874 | 6374 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
6375 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6376 |
||
6377 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
6378 |
^ 0 |
|
6379 |
]. |
|
6380 |
same := true. |
|
6381 |
cmpIndex := 1. |
|
6382 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6383 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
6384 |
same := false |
|
6385 |
] ifTrue:[ |
|
6386 |
cmpIndex := cmpIndex + 1 |
|
6387 |
] |
|
6388 |
]. |
|
6389 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
6390 |
] |
|
6391 |
||
6392 |
" |
|
6393 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
6394 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
6395 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
6396 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
6397 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
6398 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
6399 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
6400 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
6401 |
#(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
|
6402 |
" |
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 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6405 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6406 |
|
7264 | 6407 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6408 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6409 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6410 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6411 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6412 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6413 |
|
10194 | 6414 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6415 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6416 |
" |
8874 | 6417 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6418 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6419 |
returns: true - if found |
10194 | 6420 |
false - if not found |
8874 | 6421 |
" |
6422 |
||
6423 |
" |
|
6424 |
#(10 20 30 40 50 60 70) includes:99 |
|
6425 |
#(10 20 30 40 50 60 70) includes:40 |
|
6426 |
#(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
|
6427 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6428 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6429 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6430 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6431 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6432 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6433 |
"search the collection for anElement; |
602 | 6434 |
if found, return the index otherwise return 0. |
8874 | 6435 |
The comparison is done using = |
602 | 6436 |
(i.e. equality test - not identity test)." |
6437 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6438 |
^ self indexOf:anElement startingAt:1 |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6441 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6442 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6443 |
returns: elementIndex - if found |
8874 | 6444 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6445 |
" |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6448 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6449 |
#(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
|
6450 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6451 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6452 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6453 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6454 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6455 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6456 |
"search the collection for anElement; |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
6457 |
if found, return the index; otherwise return the value of the exceptionBlock. |
8874 | 6458 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6459 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6460 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6461 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6462 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6463 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6464 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6465 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6466 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6467 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6468 |
args: anElement : <object> |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
6469 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
6472 |
exceptionBlock value - if not found |
8874 | 6473 |
" |
6474 |
||
6475 |
" |
|
6476 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
6477 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
6478 |
#(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
|
6479 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6480 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6481 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6484 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 6485 |
"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
|
6486 |
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
|
6487 |
and return the index. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6488 |
If not found, return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6489 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6490 |
|idx| |
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 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6493 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
8874 | 6494 |
self at:idx put:replacement. |
6495 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6496 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6497 |
^ 0 |
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 |
" |
8874 | 6500 |
args: elementToFind : <object> |
6501 |
replacement : <integer> |
|
6502 |
start : <integer> |
|
6503 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6504 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6505 |
returns: elementIndex - if found (and replaced) |
8874 | 6506 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6507 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6508 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6509 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6510 |
|a| |
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 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6513 |
(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
|
6514 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6515 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6516 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6517 |
"Modified: / 20.5.1998 / 14:59:30 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6518 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6519 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6520 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6521 |
"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
|
6522 |
if found, return the index otherwise return 0. |
8874 | 6523 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6524 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6525 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6526 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6527 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6528 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6529 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6530 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6531 |
startIndex to:stop do:[:index | |
8874 | 6532 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6533 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6534 |
^ 0 |
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 |
" |
8874 | 6537 |
args: anElement : <object> |
6538 |
start : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 6541 |
0 - if not found |
6542 |
" |
|
6543 |
||
6544 |
" |
|
6545 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
6546 |
#(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
|
6547 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6548 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6549 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6552 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6553 |
"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
|
6554 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6555 |
If found (within the range), return the index, otherwise return 0. |
8874 | 6556 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6557 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6558 |
|
602 | 6559 |
|startIndex "{ Class: SmallInteger }" |
6560 |
stopIndex "{ Class: SmallInteger }" | |
|
6561 |
||
6562 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6563 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6564 |
startIndex to:stopIndex do:[:index | |
8874 | 6565 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6568 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6569 |
" |
8874 | 6570 |
args: anElement : <object> |
6571 |
start : <integer> |
|
6572 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6573 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6574 |
returns: elementIndex - if found |
8874 | 6575 |
exceptionBlock value - if not found |
6576 |
" |
|
6577 |
||
6578 |
" |
|
6579 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
6580 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
6581 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
6582 |
#(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
|
6583 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6584 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6585 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6586 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6587 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6588 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6589 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6590 |
"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
|
6591 |
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
|
6592 |
exceptionBlock. |
8874 | 6593 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6594 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6595 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6596 |
|index| |
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 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6599 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6600 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6601 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6602 |
" |
8874 | 6603 |
args: anElement : <object> |
6604 |
start : <integer> |
|
6605 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6606 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6607 |
returns: elementIndex - if found |
8874 | 6608 |
exceptionBlock value - if not found |
6609 |
" |
|
6610 |
||
6611 |
" |
|
6612 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
6613 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
6614 |
#(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
|
6615 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6616 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6617 |
"Modified: / 20.5.1998 / 14:58:05 / 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 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6621 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6622 |
if found, return the index otherwise return 0. |
8874 | 6623 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6624 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6625 |
|
8874 | 6626 |
^ self lastIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6627 |
|
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 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6630 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6631 |
returns: elementIndex - if found |
8874 | 6632 |
0 - if not found |
6633 |
" |
|
6634 |
||
6635 |
" |
|
6636 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
6637 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
6638 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
6639 |
#(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
|
6640 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6641 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6642 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6643 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6644 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6645 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6646 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6647 |
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
|
6648 |
exceptionBlock. |
8874 | 6649 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6650 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6651 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6652 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6653 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6654 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6655 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6656 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6657 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6658 |
" |
8874 | 6659 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
6660 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
6661 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
6662 |
#(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
|
6663 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6664 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6665 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6666 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6667 |
"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
|
6668 |
if found, return the index otherwise return 0. |
8874 | 6669 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6670 |
(i.e. equality test - not identity test)." |
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 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6673 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6674 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6675 |
startIndex to:1 by:-1 do:[:index | |
8874 | 6676 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 6677 |
]. |
6678 |
^ 0 |
|
6679 |
||
6680 |
" |
|
8874 | 6681 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
6682 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
6683 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
6684 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
6685 |
#(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
|
6686 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6687 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6688 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6689 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 6690 |
"search the collection backwards for anElement starting the search at |
6691 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6692 |
otherwise return the value of the exceptionBlock. |
8874 | 6693 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6694 |
(i.e. equality test - not identity test)." |
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 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6697 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6698 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6699 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6700 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6701 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6702 |
" |
8874 | 6703 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
6704 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
6705 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
6706 |
#(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
|
6707 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6708 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6709 |
|
4788 | 6710 |
lastIndexOfAny:aCollection |
6711 |
"search the collection backwards for any in aCollection; |
|
6712 |
if found, return the index, otherwise return 0. |
|
8874 | 6713 |
The comparison is done using = |
4788 | 6714 |
(i.e. equality test - not identity test)." |
6715 |
||
8874 | 6716 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 6717 |
|
6718 |
" |
|
6719 |
args: aCollection : <collection> |
|
6720 |
||
6721 |
returns: elementIndex - if found |
|
8874 | 6722 |
0 - if not found |
6723 |
" |
|
6724 |
||
6725 |
" |
|
6726 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 6727 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 6728 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
6729 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 6730 |
" |
6731 |
||
6732 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
6733 |
! |
|
6734 |
||
6735 |
lastIndexOfAny:aCollection startingAt:start |
|
6736 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
6737 |
if found, return the index, otherwise return 0. |
|
8874 | 6738 |
The comparison is done using = |
4788 | 6739 |
(i.e. equality test - not identity test)." |
6740 |
||
6741 |
|startIndex "{ Class: SmallInteger }"| |
|
6742 |
||
6743 |
startIndex := start. |
|
6744 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 6745 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 6746 |
]. |
6747 |
^ 0 |
|
6748 |
||
6749 |
" |
|
8874 | 6750 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
6751 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 6752 |
" |
6753 |
! |
|
6754 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6755 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6756 |
"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
|
6757 |
stopping at:stop; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6758 |
if found, return the index otherwise return nil. |
8874 | 6759 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6760 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6761 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6762 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6763 |
although this is somewhat strange (the indexOf-search |
6764 |
methods return 0), this is done for VW compatibility." |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6765 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6766 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6767 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6768 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6769 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6770 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6771 |
startIndex to:stop do:[:index | |
8874 | 6772 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6773 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6774 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6775 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6776 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6777 |
args: anElement : <object> |
8874 | 6778 |
start : <integer> |
6779 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6780 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6781 |
returns: elementIndex - if found |
8874 | 6782 |
nil - if not found |
6783 |
" |
|
6784 |
||
6785 |
" |
|
6786 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
6787 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
6788 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 6789 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6790 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6791 |
"Modified: / 20.5.1998 / 15:11:32 / cg" |
602 | 6792 |
! |
6793 |
||
6794 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6795 |
"search the collection for anElement, starting the search at index start |
|
6796 |
and stopping at stop; |
|
6797 |
if found, return the index otherwise return the value of the |
|
6798 |
exceptionBlock. |
|
8874 | 6799 |
The comparison is done using = |
602 | 6800 |
(i.e. equality test - not identity test)." |
6801 |
||
6802 |
|index| |
|
6803 |
||
6804 |
index := self nextIndexOf:anElement from:start to:stop. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6805 |
index isNil ifTrue:[^ exceptionBlock value]. |
602 | 6806 |
^ index |
6807 |
||
6808 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6809 |
args: anElement : <object> |
8874 | 6810 |
start : <integer> |
6811 |
stop : <integer> |
|
6812 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6813 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6814 |
returns: elementIndex - if found |
8874 | 6815 |
valueImplementor value - if not found |
6816 |
" |
|
6817 |
||
6818 |
" |
|
6819 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
6820 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
6821 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 6822 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6823 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6824 |
"Modified: / 20.5.1998 / 15:12:36 / cg" |
2275 | 6825 |
! |
6826 |
||
6827 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
6828 |
"search the collection for anElement, starting the search at index start; |
|
6829 |
ending at stop, going in reverse direction. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6830 |
If found (within the range), return the index, otherwise return nil. |
8874 | 6831 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6832 |
(i.e. equality test - not identity test). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6833 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6834 |
NOTICE: nil is returned here if the element is not found, |
8874 | 6835 |
although this is somewhat strange (the indexOf-search |
6836 |
methods return 0), this is done for VW compatibility." |
|
2275 | 6837 |
|
6838 |
|startIndex "{ Class: SmallInteger }" |
|
6839 |
stopIndex "{ Class: SmallInteger }" | |
|
6840 |
||
6841 |
startIndex := startSearchIndex. |
|
6842 |
stopIndex := endSearchIndex max:1. |
|
6843 |
startIndex to:stopIndex by:-1 do:[:index | |
|
8874 | 6844 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 6845 |
]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6846 |
^ nil |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6847 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6848 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6849 |
args: anElement : <object> |
8874 | 6850 |
start : <integer> |
6851 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6852 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6853 |
returns: elementIndex - if found |
8874 | 6854 |
nil - if not found |
6855 |
" |
|
6856 |
||
6857 |
" |
|
6858 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
6859 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
6860 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 6861 |
" |
6862 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6863 |
"Modified: / 20.5.1998 / 15:13:27 / cg" |
2275 | 6864 |
! |
6865 |
||
6866 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
6867 |
"search the collection for anElement, starting the search at index start |
|
6868 |
and stopping at stop, doing a reverse search; |
|
6869 |
if found, return the index otherwise return the value of the |
|
6870 |
exceptionBlock. |
|
8874 | 6871 |
The comparison is done using = |
2275 | 6872 |
(i.e. equality test - not identity test)." |
6873 |
||
6874 |
|index| |
|
6875 |
||
6876 |
index := self prevIndexOf:anElement from:start to:stop. |
|
6877 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
6878 |
^ index |
|
6879 |
||
6880 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6881 |
args: anElement : <object> |
8874 | 6882 |
start : <integer> |
6883 |
stop : <integer> |
|
6884 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6885 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6886 |
returns: elementIndex - if found |
8874 | 6887 |
valueImplementor value - if not found |
6888 |
" |
|
6889 |
||
6890 |
" |
|
6891 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
6892 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
6893 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 6894 |
" |
6895 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6896 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6897 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6898 |
|
7264 | 6899 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6900 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6901 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6902 |
"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
|
6903 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6904 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6905 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6906 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6907 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6908 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6909 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6910 |
returns: elementIndex - if found |
8874 | 6911 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6912 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6913 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6914 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6915 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6916 |
#(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
|
6917 |
#(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
|
6918 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6919 |
be careful: |
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 |
#(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
|
6922 |
#(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
|
6923 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6924 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6925 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6926 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6927 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6928 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6929 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6930 |
"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
|
6931 |
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
|
6932 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6933 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6934 |
|index| |
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 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6937 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6938 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6939 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6940 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6941 |
args: anElement : <object> |
8874 | 6942 |
exceptionBlock : <valueImplementor> |
3468
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 |
returns: elementIndex - if found |
8874 | 6945 |
exceptionBlock value - if not found |
6946 |
" |
|
6947 |
||
6948 |
" |
|
6949 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
6950 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
6951 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
6952 |
#(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
|
6953 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6954 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6955 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
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 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6958 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6959 |
"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
|
6960 |
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
|
6961 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6962 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6963 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6964 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6965 |
args: anElement : <object> |
8874 | 6966 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6967 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6968 |
returns: elementIndex - if found |
8874 | 6969 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6970 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6971 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6972 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6973 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6974 |
"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
|
6975 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6976 |
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
|
6977 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6978 |
|startIndex "{ Class: SmallInteger }" |
8874 | 6979 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6980 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6981 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6982 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6983 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6984 |
startIndex to:stop do:[:index | |
8874 | 6985 |
el := self at:index. |
6986 |
anElement == el ifTrue:[^ index]. |
|
6987 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6988 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6989 |
^ 0 |
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 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6992 |
args: anElement : <object> |
8874 | 6993 |
anotherElement : <object> |
6994 |
start : <integer> |
|
5520
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 |
returns: elementIndex - if found |
8874 | 6997 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6998 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
6999 |
|
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 |
#(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
|
7002 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7003 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7004 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7005 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7006 |
"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
|
7007 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7008 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7009 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7010 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7011 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7012 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7013 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7014 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7015 |
startIndex to:stop do:[:index | |
8874 | 7016 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7017 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7018 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7019 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7020 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7021 |
args: anElement : <object> |
8874 | 7022 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7023 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7024 |
returns: elementIndex - if found |
8874 | 7025 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7026 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7027 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7028 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7029 |
#(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
|
7030 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 7031 |
#(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
|
7032 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7033 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7034 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7037 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7038 |
"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
|
7039 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7040 |
If found (within the range), return the index, otherwise return 0. |
8874 | 7041 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7042 |
(i.e. equality test - not identity test)." |
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 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7045 |
stopIndex "{ Class: SmallInteger }" | |
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 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7048 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7049 |
startIndex to:stopIndex do:[:index | |
8874 | 7050 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7053 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7054 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7055 |
args: anElement : <object> |
8874 | 7056 |
start : <integer> |
7057 |
stop : <integer> |
|
3468
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 |
returns: elementIndex - if found |
8874 | 7060 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7061 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7062 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7063 |
"Created: / 12.4.1996 / 18:23:07 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7064 |
"Modified: / 20.5.1998 / 15:00:50 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7065 |
! |
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 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7068 |
"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
|
7069 |
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
|
7070 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7071 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7072 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7073 |
|index| |
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 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7076 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7077 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7078 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7079 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7080 |
args: anElement : <object> |
8874 | 7081 |
start : <integer> |
7082 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7083 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7084 |
returns: elementIndex - if found |
8874 | 7085 |
exceptionBlock value - if not found |
7086 |
" |
|
7087 |
||
7088 |
" |
|
7089 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
7090 |
#(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
|
7091 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7092 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7093 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7094 |
! |
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 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7097 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7098 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7099 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7100 |
|
10194 | 7101 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
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 |
" |
8874 | 7104 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7105 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7106 |
returns: true - if found |
10194 | 7107 |
false - if not found |
8874 | 7108 |
" |
7109 |
||
7110 |
" |
|
7111 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
7112 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
7113 |
#(10 20 30 40 50 60 70) includes:40 |
|
7114 |
#(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
|
7115 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7116 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7117 |
|
8874 | 7118 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
7119 |
#(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
|
7120 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7121 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7122 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7123 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7124 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7125 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7126 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7127 |
if found, return the index otherwise return 0. |
8874 | 7128 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7129 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7130 |
|
8874 | 7131 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7132 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7133 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7134 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7135 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7136 |
returns: elementIndex - if found |
8874 | 7137 |
0 - if not found |
7138 |
" |
|
7139 |
||
7140 |
" |
|
7141 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
7142 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
7143 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
7144 |
#(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
|
7145 |
" |
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 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7148 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7149 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7150 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7151 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7152 |
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
|
7153 |
exceptionBlock. |
8874 | 7154 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7155 |
(i.e. identity test - not equality test)." |
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 |
|index| |
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 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7160 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7161 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7162 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7163 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7164 |
args: anElement : <object> |
8874 | 7165 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7166 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7167 |
returns: elementIndex - if found |
8874 | 7168 |
exceptionBlock value - if not found |
7169 |
" |
|
7170 |
||
7171 |
" |
|
7172 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
7173 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
7174 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
7175 |
#(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
|
7176 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7177 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7178 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7179 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7180 |
! |
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 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7183 |
"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
|
7184 |
if found, return the index otherwise return 0. |
8874 | 7185 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7186 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7187 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7188 |
|startIndex "{ Class: SmallInteger }"| |
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 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7191 |
startIndex to:1 by:-1 do:[:index | |
8874 | 7192 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7193 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7194 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7195 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7196 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7197 |
args: anElement : <object> |
8874 | 7198 |
start : <integer> |
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 |
returns: elementIndex - if found |
8874 | 7201 |
0 - if not found |
7202 |
" |
|
7203 |
||
7204 |
" |
|
7205 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
7206 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
7207 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
7208 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
7209 |
#(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
|
7210 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7211 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7212 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7213 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7214 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7215 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 7216 |
"search the collection backwards for anElement starting the search at |
7217 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7218 |
otherwise return the value of the exceptionBlock. |
8874 | 7219 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7220 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7221 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7222 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7223 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7224 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7225 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7226 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7227 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7228 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7229 |
args: anElement : <object> |
8874 | 7230 |
start : <integer> |
7231 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7232 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7233 |
returns: elementIndex - if found |
8874 | 7234 |
exceptionBlock value - if not found |
7235 |
" |
|
7236 |
||
7237 |
" |
|
7238 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
7239 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
7240 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
7241 |
#(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
|
7242 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7243 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7244 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7245 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 7246 |
! ! |
7247 |
||
7248 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
7249 |
||
6958 | 7250 |
detectFirstInOrder:sortBlock |
7251 |
"find the first element of the collection sorted with sortBlock" |
|
7252 |
||
7253 |
|selectedForNow start| |
|
7254 |
||
7255 |
start := true. |
|
7256 |
self do:[:element| |
|
8874 | 7257 |
start ifTrue:[ |
7258 |
start := false. |
|
7259 |
selectedForNow := element. |
|
7260 |
] ifFalse:[ |
|
7261 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
7262 |
selectedForNow := element. |
|
7263 |
]. |
|
7264 |
]. |
|
6958 | 7265 |
]. |
7266 |
||
7267 |
^ selectedForNow |
|
7268 |
||
7269 |
" |
|
8874 | 7270 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
7271 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
7272 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
7273 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 7274 |
" |
7275 |
! |
|
7276 |
||
5358 | 7277 |
mergeSort |
8874 | 7278 |
"sort the collection using a mergeSort algorithm. |
7279 |
The elements are compared using'<' |
|
5358 | 7280 |
i.e. they should offer a magnitude-like protocol. |
7281 |
||
7282 |
The implementation uses the mergesort algorithm, which may not be |
|
8874 | 7283 |
the best possible for all situations |
5358 | 7284 |
See also #quickSort and #randomizedSort for other sort algorithms |
7285 |
with different worst- and average case behavior)" |
|
7286 |
||
17763 | 7287 |
self mergeSort:[:a :b | a < b] |
5358 | 7288 |
|
7289 |
" |
|
7290 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
17763 | 7291 |
#(1 16 7 98 7 3 19 4 0) mergeSort |
5358 | 7292 |
|
7293 |
|data| |
|
7294 |
data := Random new next:100000. |
|
7295 |
'merge random ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
7296 |
'merge sorted ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
|
8874 | 7297 |
data reverse. |
5358 | 7298 |
'merge reverse ' print. (Time millisecondsToRun:[data mergeSort]) printNL. |
7299 |
||
7300 |
data := Random new next:100000. |
|
7301 |
'quick random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7302 |
'quick sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7303 |
data reverse. |
5358 | 7304 |
'quick reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7305 |
||
7306 |
data := Random new next:100000. |
|
7307 |
'quickr random ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
7308 |
'quickr sorted ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
|
8874 | 7309 |
data reverse. |
5358 | 7310 |
'quickr reverse ' print. (Time millisecondsToRun:[data randomizedSort]) printNL. |
7311 |
" |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7312 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7313 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7314 |
mergeSort:sortBlock |
8874 | 7315 |
"sort the collection using a mergeSort algorithm. |
7316 |
The elements are compared using'<' |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7317 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7318 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7319 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 7320 |
the best possible for all situations |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7321 |
See also #quickSort and #randomizedSort for other sort algorithms |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7322 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7323 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7324 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7325 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7326 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7327 |
(stop > 1) ifTrue:[ |
8874 | 7328 |
self mergeSort:sortBlock from:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7329 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7330 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7331 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7332 |
#(1 16 7 98 3 19 4 0) mergeSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7333 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7334 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7335 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7336 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7337 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7338 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7339 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7340 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7341 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7342 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data sort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7343 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data sort]). |
8874 | 7344 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7345 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data sort]). |
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 |
data := Random new next:200000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7348 |
Transcript show:'quickr random '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7349 |
Transcript show:'quickr sorted '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
8874 | 7350 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7351 |
Transcript show:'quickr reverse '; showCR:(Time millisecondsToRun:[data randomizedSort]). |
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 |
mergeSort:aBlock from: startIndex to: stopIndex |
8874 | 7356 |
"Sort the given range of indices using the mergesort algorithm. |
7357 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
5358 | 7358 |
as many comparisons as heapsort or quicksort." |
7359 |
||
8874 | 7360 |
"Details: recursively split the range to be sorted into two halves, |
7361 |
mergesort each half, then merge the two halves together. |
|
7362 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
7363 |
and forth between the receiver and this copy. |
|
7364 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 7365 |
resulting in the receiver being completely sorted." |
7366 |
||
7367 |
| temp | |
|
7368 |
||
7369 |
self size <= 1 ifTrue: [^ self]. "nothing to do" |
|
7370 |
startIndex = stopIndex ifTrue: [^ self]. |
|
7371 |
(startIndex >= 1 and: [startIndex < stopIndex]) |
|
8874 | 7372 |
ifFalse: [self error: 'bad start index']. |
5358 | 7373 |
stopIndex <= self size |
8874 | 7374 |
ifFalse: [self error: 'bad stop index']. |
5358 | 7375 |
temp := self clone. |
7376 |
self mergeSortFrom: startIndex to: stopIndex src: temp dst: self by: aBlock. |
|
7377 |
||
7378 |
" |
|
8874 | 7379 |
#(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
|
7380 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7381 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7382 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7383 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7384 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7385 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7386 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
8874 | 7387 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7388 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7389 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7390 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7391 |
|data| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7392 |
data := Random new next:100000. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7393 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7394 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
8874 | 7395 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7396 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7397 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7398 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7399 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7400 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7401 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7402 |
'<' i.e. they should offer a magnitude-like protocol. |
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 |
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
|
7405 |
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
|
7406 |
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
|
7407 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7408 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7409 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7410 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7411 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7412 |
(stop > 1) ifTrue:[ |
8874 | 7413 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7414 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7415 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7416 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7417 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 7418 |
|
7419 |
|data| |
|
7420 |
data := Random new next:100000. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7421 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7422 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
8874 | 7423 |
data reverse. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7424 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7425 |
" |
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 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7428 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7429 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7430 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7431 |
"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
|
7432 |
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
|
7433 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7434 |
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
|
7435 |
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
|
7436 |
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
|
7437 |
worstCase behavior (but worse average & bestCase behavior)" |
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 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7440 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7441 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7442 |
(stop > 1) ifTrue:[ |
8874 | 7443 |
sortBlock numArgs == 3 ifTrue:[ |
7444 |
"/ TODO: pass a collating policy to aBlock |
|
7445 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
7446 |
] ifFalse:[ |
|
7447 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
7448 |
] |
|
7636
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7451 |
" |
8874 | 7452 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7453 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7454 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7455 |
#(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
|
7456 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7457 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7458 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7459 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7460 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7461 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7462 |
"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
|
7463 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7464 |
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
|
7465 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7466 |
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
|
7467 |
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
|
7468 |
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
|
7469 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7470 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7471 |
|stop| |
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 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7474 |
(stop > 1) ifTrue:[ |
8874 | 7475 |
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
|
7476 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7477 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7478 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7479 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7480 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7481 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7482 |
c1 sort:[:a :b | a > b] with:c2. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7483 |
c1 printNL. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7484 |
c2 printNL |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7485 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7486 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7487 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
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 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7490 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7491 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7492 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7493 |
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
|
7494 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7495 |
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
|
7496 |
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
|
7497 |
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
|
7498 |
worstCase behavior (but worse average & bestCase behavior)" |
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 |
|stop| |
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 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7503 |
(stop > 1) ifTrue:[ |
8874 | 7504 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7505 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7506 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7507 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
5358 | 7508 |
! |
7509 |
||
11272 | 7510 |
randomShuffle |
7511 |
"random shuffle my elements in place. |
|
7512 |
This is a destructive algorithm. |
|
7513 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
7514 |
See 'The Art of Computer Programming'." |
|
7515 |
||
7516 |
self size to:2 by:-1 do:[:endIndex | |
|
7517 |
|rndIndex t| |
|
7518 |
||
7519 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
7520 |
"/ exchange |
|
7521 |
t := self at:rndIndex. |
|
7522 |
self at:rndIndex put:(self at:endIndex). |
|
7523 |
self at:endIndex put:t. |
|
7524 |
]. |
|
7525 |
||
7526 |
" |
|
7527 |
(1 to:10) asOrderedCollection randomShuffle |
|
7528 |
||
7529 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7530 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7531 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7532 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7533 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7534 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7535 |
" |
|
7536 |
||
11273 | 7537 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
7538 |
||
7539 |
|a score| |
|
7540 |
||
7541 |
a := 'abcd'. |
|
7542 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
7543 |
||
7544 |
1000000 times do:[ |
|
7545 |
|sorted| |
|
7546 |
sorted := a copy randomShuffle. |
|
7547 |
score at:sorted put:(score at:sorted)+1. |
|
7548 |
]. |
|
7549 |
||
7550 |
score keys asSortedCollection do:[:k | |
|
7551 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
7552 |
]. |
|
7553 |
" |
|
7554 |
||
7555 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
11272 | 7556 |
! |
7557 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7558 |
randomizedSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7559 |
"sort the collection inplace. The elements are compared using |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7560 |
'<' i.e. they should offer a magnitude-like protocol. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7561 |
|
8874 | 7562 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7563 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7564 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7565 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7566 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7567 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7568 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7569 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7570 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7571 |
"/ 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
|
7572 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7573 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7574 |
"/ 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
|
7575 |
^ self sort |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7576 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7577 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7578 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7579 |
randomizedSort:sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7580 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7581 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7582 |
|
8874 | 7583 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7584 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7585 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7586 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7587 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7588 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7589 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7590 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7591 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7592 |
"/ 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
|
7593 |
"/ ] |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7594 |
|
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7595 |
"/ 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
|
7596 |
^ self sort:sortBlock |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7597 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7598 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7599 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7600 |
randomizedSort:sortBlock with:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7601 |
"sort the collection inplace using the 2-arg block sortBlock |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7602 |
for comparison. Also reorder the elements in aCollection. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7603 |
Use this, when you have a key collection to sort some other collection with. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7604 |
|
8874 | 7605 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7606 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7607 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7608 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7609 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7610 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7611 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7612 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7613 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7614 |
"/ 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
|
7615 |
"/ ] |
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 |
"/ 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
|
7618 |
^ self sort:sortBlock with:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7619 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7620 |
! |
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 |
randomizedSortWith:aCollection |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7623 |
"sort the receiver collection inplace, using '<' to compare elements. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7624 |
Also, the elements of aCollection are reordered with it. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7625 |
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
|
7626 |
|
8874 | 7627 |
This uses the randomized quicksort algorithm, |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7628 |
which has a better worstCase behavior than quickSort |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7629 |
(bit worse bestCase & averageCase behavior). |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7630 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7631 |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7632 |
"/ |stop| |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7633 |
"/ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7634 |
"/ stop := self size. |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7635 |
"/ (stop > 1) ifTrue:[ |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7636 |
"/ 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
|
7637 |
"/ ] |
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 |
"/ 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
|
7640 |
^ self sortWith:aCollection |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
7641 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7642 |
! |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7643 |
|
602 | 7644 |
reverse |
7335 | 7645 |
"destructively reverse the order of the elements inplace. |
11264 | 7646 |
WARNING: this is a destructive operation, which modifies the receiver. |
7647 |
Please use reversed (with a d) for a functional version." |
|
602 | 7648 |
|
7649 |
|lowIndex "{ Class: SmallInteger }" |
|
7650 |
hiIndex "{ Class: SmallInteger }" |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
7651 |
t1 t2| |
602 | 7652 |
|
7653 |
hiIndex := self size. |
|
7654 |
lowIndex := 1. |
|
7655 |
[lowIndex < hiIndex] whileTrue:[ |
|
11264 | 7656 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
7657 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
7658 |
||
7659 |
lowIndex := lowIndex + 1. |
|
7660 |
hiIndex := hiIndex - 1 |
|
602 | 7661 |
] |
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7662 |
|
69 | 7663 |
" |
602 | 7664 |
#(4 5 6 7 7) reverse |
7665 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
69 | 7666 |
" |
1 | 7667 |
! |
7668 |
||
5004 | 7669 |
reversed |
7670 |
"return a copy with elements in reverse order" |
|
7671 |
||
7672 |
^ self copy reverse |
|
7673 |
||
7674 |
" |
|
8874 | 7675 |
#(4 5 6 7 7) reversed |
5004 | 7676 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
7677 |
" |
|
7678 |
! |
|
7679 |
||
1 | 7680 |
sort |
7681 |
"sort the collection inplace. The elements are compared using |
|
2069 | 7682 |
'<' i.e. they should offer a magnitude-like protocol. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7683 |
|
159 | 7684 |
The implementation uses the quicksort algorithm, which may not be |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7685 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7686 |
case behavior). See also #randomizedSort for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7687 |
worstCase behavior (but worse average & bestCase behavior)" |
13 | 7688 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7689 |
self quickSort |
1 | 7690 |
|
95 | 7691 |
" |
7692 |
#(1 16 7 98 3 19 4 0) sort |
|
7693 |
||
7694 |
|data| |
|
7695 |
data := Random new next:100000. |
|
7696 |
'random ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
7697 |
'sorted ' print. (Time millisecondsToRun:[data sort]) printNL. |
|
8874 | 7698 |
data reverse. |
95 | 7699 |
'reverse ' print. (Time millisecondsToRun:[data sort]) printNL. |
7700 |
" |
|
2069 | 7701 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7702 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
1 | 7703 |
! |
7704 |
||
7705 |
sort:sortBlock |
|
7706 |
"sort the collection inplace using the 2-arg block sortBlock |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7707 |
for comparison. This allows any sort criteria to be implemented. |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7708 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7709 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7710 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7711 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7712 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7713 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7714 |
self quickSort:sortBlock |
1 | 7715 |
|
95 | 7716 |
" |
8874 | 7717 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
7718 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7719 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7720 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
95 | 7721 |
" |
2069 | 7722 |
|
3055
0832fba4c740
support 3-arg sort, passing a collationPolicy
Claus Gittinger <cg@exept.de>
parents:
3044
diff
changeset
|
7723 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
1 | 7724 |
! |
7725 |
||
7726 |
sort:sortBlock with:aCollection |
|
7727 |
"sort the collection inplace using the 2-arg block sortBlock |
|
159 | 7728 |
for comparison. Also reorder the elements in aCollection. |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7729 |
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
|
7730 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7731 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7732 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7733 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7734 |
worstCase behavior (but worse average & bestCase behavior)" |
1 | 7735 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7736 |
self quickSort:sortBlock with:aCollection |
1 | 7737 |
|
69 | 7738 |
" |
7739 |
|c1 c2| |
|
1 | 7740 |
c1 := #(1 16 7 9). |
7741 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
7742 |
c1 sort:[:a :b | a > b] with:c2. |
|
95 | 7743 |
c1 printNL. |
7744 |
c2 printNL |
|
69 | 7745 |
" |
2069 | 7746 |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7747 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
1 | 7748 |
! |
7749 |
||
17747 | 7750 |
sortByApplying:aBlock |
7751 |
"Sort my contents inplace based on the value of what aBlock returns for each element. |
|
7752 |
Similar to, but even more flexible than sortBySelector." |
|
7753 |
||
7754 |
^ self sort:[:a :b | (aBlock value:a) < (aBlock value:b)] |
|
7755 |
||
7756 |
" |
|
7757 |
replace all uses of sort as in: |
|
7758 |
... sort:[:a :b | a foo < b foo] |
|
7759 |
by: |
|
7760 |
... sortByApplying:[:each | each foo] |
|
7761 |
" |
|
7762 |
! |
|
7763 |
||
11154 | 7764 |
sortBySelector:aSelector |
7765 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
7766 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
7767 |
||
7768 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
7769 |
||
7770 |
" |
|
7771 |
replace all uses of sort as in: |
|
7772 |
... sort:[:a :b | a foo < b foo] |
|
7773 |
by: |
|
7774 |
... sortBySelector:#foo |
|
7775 |
||
7776 |
find these by searching for code matching (code-search in the browsers method list): |
|
7777 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
7778 |
" |
|
7779 |
! |
|
7780 |
||
602 | 7781 |
sortWith:aCollection |
2069 | 7782 |
"sort the receiver collection inplace, using '<' to compare elements. |
7783 |
Also, the elements of aCollection are reordered with it. |
|
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7784 |
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
|
7785 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7786 |
The implementation uses the quicksort algorithm, which may not be |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7787 |
the best possible for all situations (quickSort has O-square worst |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7788 |
case behavior). See also #randomizedSort: for a version with better |
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7789 |
worstCase behavior (but worse average & bestCase behavior)" |
602 | 7790 |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
7791 |
self quickSortWith:aCollection |
424 | 7792 |
! |
7793 |
||
11279 | 7794 |
sortedBy:aBlock |
9072 | 7795 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
7796 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
7797 |
||
7798 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
11279 | 7799 |
|
7800 |
"Modified: / 22-10-2008 / 21:25:07 / cg" |
|
9072 | 7801 |
! |
7802 |
||
602 | 7803 |
topologicalSort:sortBlock |
7804 |
"sort the collection inplace using a sloooow sort algorithm. |
|
7805 |
This algorithm has O-square runtime behavior and should be used only |
|
7806 |
in special situations. |
|
7807 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
7808 |
a>c (for example, to sort classes by inheritance) |
|
7809 |
||
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7810 |
In all other situations, use #sort; which implements the quicksort algorithm. |
10812 | 7811 |
|
7812 |
If there are cycles, this algorithm may loop endless!! |
|
7813 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
77 | 7814 |
" |
602 | 7815 |
|
7816 |
|smallestIndex "{ Class: SmallInteger }" |
|
7817 |
end "{ Class: SmallInteger }" |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7818 |
index2 "{ Class: SmallInteger }" |
602 | 7819 |
smallest thisOne| |
7820 |
||
7821 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
7822 |
||
7823 |
end := self size. |
|
862 | 7824 |
1 to:end-1 do:[:index | |
10812 | 7825 |
smallest := self at:index. |
7826 |
smallestIndex := index. |
|
7827 |
||
7828 |
index2 := index + 1. |
|
7829 |
[ |
|
7830 |
thisOne := self at:index2. |
|
7831 |
(index2 ~~ smallestIndex |
|
7832 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
7833 |
smallestIndex := index2. |
|
7834 |
smallest := thisOne. |
|
7835 |
index2 := index + 1. |
|
7836 |
] ifFalse:[ |
|
7837 |
index2 := index2 + 1. |
|
7838 |
]. |
|
7839 |
] doWhile:[index2 <= end]. |
|
862 | 7840 |
|
7841 |
"/ (index + 1) to:end do:[:index2 | |
|
7842 |
"/ thisOne := self at:index2. |
|
7843 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
7844 |
"/ smallestIndex := index2. |
|
7845 |
"/ smallest := thisOne |
|
7846 |
"/ ]. |
|
7847 |
"/ ]. |
|
1495
f0226967f2a2
added a Class: Smallinteger hint
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
7848 |
|
10812 | 7849 |
(smallestIndex ~~ index) ifTrue:[ |
7850 |
thisOne := self at:index. |
|
7851 |
self at:index put:smallest. |
|
7852 |
self at:smallestIndex put:thisOne |
|
7853 |
]. |
|
1 | 7854 |
] |
95 | 7855 |
|
77 | 7856 |
" |
8874 | 7857 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
7858 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
10812 | 7859 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
8874 | 7860 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
7861 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
77 | 7862 |
" |
2881
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7863 |
|
452f9154d0dc
comments; added #randomizedQuickSort.
Claus Gittinger <cg@exept.de>
parents:
2860
diff
changeset
|
7864 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
1 | 7865 |
! ! |
602 | 7866 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7867 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7868 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7869 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7870 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7871 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 7872 |
however, having a common protocol with indexed collections |
7873 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7874 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7875 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7876 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7877 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7878 |
" |
8874 | 7879 |
#(1 2 3) includesKey:4 |
7880 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7881 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7882 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
7883 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7884 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7885 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7886 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7887 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7888 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7889 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
7890 |
|
2069 | 7891 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 7892 |
|
7893 |
version |
|
17841 | 7894 |
^ '$Id: SequenceableCollection.st 10643 2011-06-08 21:53:07Z vranyj1 $' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
7895 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
7896 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
7897 |
version_CVS |
17841 | 7898 |
^ 'Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.304 2011/06/02 11:23:18 cg Exp ' |
17761
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17754
diff
changeset
|
7899 |
! |
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17754
diff
changeset
|
7900 |
|
b0e5971141bc
Added Lookup and BuiltinLookup classes
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17754
diff
changeset
|
7901 |
version_SVN |
17841 | 7902 |
^ '$Id: SequenceableCollection.st 10643 2011-06-08 21:53:07Z vranyj1 $' |
628 | 7903 |
! ! |
6820 | 7904 |
|
4197
8ae90a572914
raise a special signal, when a literalencodings class
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
7905 |
SequenceableCollection initialize! |
17763 | 7906 |
|
17767 | 7907 |
|
17772
6e0b3c06c364
Method reverted to r10532
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17767
diff
changeset
|
7908 |
|
17780 | 7909 |
|
17795 | 7910 |
|
17797 | 7911 |
|
17807 | 7912 |
|
17814 | 7913 |
|
17815 | 7914 |
|
17834 | 7915 |
|
17841 | 7916 |