author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Fri, 07 Sep 2012 17:24:32 +0100 | |
branch | jv |
changeset 17966 | 8b5df02e171f |
parent 17955 | f5ee690b1a27 |
child 18011 | deb0c3355881 |
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 |
||
17944 | 657 |
!SequenceableCollection methodsFor:'Compatibility-V''Age'! |
658 |
||
659 |
replaceFrom:start to:end withObject:anObject |
|
660 |
"Replace the elements from start to end with anObject. |
|
661 |
Return the receiver." |
|
662 |
||
663 |
self from:start to:end put:anObject. |
|
664 |
^ self. |
|
665 |
||
666 |
" |
|
667 |
|a| |
|
668 |
||
669 |
a := Array new: 10. |
|
670 |
a replaceFrom:3 to:7 withObject:999. |
|
671 |
a |
|
672 |
" |
|
673 |
||
674 |
"Created: / 16-05-2012 / 11:13:55 / cg" |
|
675 |
! ! |
|
676 |
||
7261 | 677 |
!SequenceableCollection methodsFor:'Compatibility-VW'! |
6566 | 678 |
|
679 |
replaceElementsFrom:start to:stop withArray:anArray startingAt:repStart |
|
680 |
^ self replaceFrom:start to:stop with:anArray startingAt:repStart |
|
681 |
! ! |
|
682 |
||
17966 | 683 |
|
1 | 684 |
!SequenceableCollection methodsFor:'accessing'! |
685 |
||
1366 | 686 |
after:anObject |
687 |
"return the element, after anObject. |
|
5231 | 688 |
If anObject is not in the receiver, report an error; |
689 |
if anObject is the last in the receiver, return nil. |
|
1366 | 690 |
This depends on #after:ifAbsent: being implemented in a concrete class." |
691 |
||
692 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
693 |
||
694 |
" |
|
8874 | 695 |
#(4 3 2 1) asOrderedCollection after:3. |
696 |
#(4 3 2 1) asOrderedCollection after:5 |
|
697 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1366 | 698 |
" |
699 |
||
700 |
"Created: 10.5.1996 / 13:59:29 / cg" |
|
701 |
"Modified: 10.5.1996 / 14:03:33 / cg" |
|
702 |
! |
|
703 |
||
704 |
after:anObject ifAbsent:exceptionBlock |
|
8874 | 705 |
"return the element, after anObject. |
5231 | 706 |
If anObject is the last in the receiver, return nil. |
707 |
If there is no such element (anObject is not found), |
|
1366 | 708 |
return the value from exceptionBlock." |
709 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
710 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
711 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
712 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
713 |
idx ~~ 0 ifTrue:[ |
8874 | 714 |
idx == self size ifTrue:[^ nil]. |
715 |
^ self at:(idx + 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
716 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
717 |
^ exceptionBlock value |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
718 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
719 |
" |
8874 | 720 |
#(4 3 2 1) after:3 ifAbsent:nil. |
721 |
(2 to:10 by:2) after:4 ifAbsent:nil. |
|
722 |
#(4 3 2 1) asOrderedCollection after:5 ifAbsent:nil. |
|
723 |
(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
|
724 |
" |
1366 | 725 |
! |
726 |
||
6967 | 727 |
anElement |
8874 | 728 |
"return any element from the collection, |
6967 | 729 |
report an error if there is none" |
730 |
||
731 |
^ self first |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
732 |
|
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
733 |
" |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
734 |
#() first -> Error |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
735 |
#(1 2 3) first -> 1 |
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
736 |
" |
6967 | 737 |
! |
738 |
||
32 | 739 |
at:index ifAbsent:exceptionBlock |
8874 | 740 |
"return the element at index if valid. |
741 |
If the index is invalid, return the result of evaluating |
|
302 | 742 |
the exceptionblock. |
8874 | 743 |
NOTICE: |
744 |
in ST-80, this message is only defined for Dictionaries, |
|
745 |
however, having a common protocol with indexed collections |
|
746 |
often simplifies things." |
|
32 | 747 |
|
8579 | 748 |
(index between:1 and:self size) ifFalse:[ |
8874 | 749 |
^ exceptionBlock value |
32 | 750 |
]. |
751 |
^ self at:index |
|
752 |
||
95 | 753 |
" |
754 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
8874 | 755 |
#(1 2 3) asOrderedCollection at:4 ifAbsent:['no such index'] |
756 |
#(1 2 3) at:4 ifAbsent:['no such index'] |
|
302 | 757 |
|
95 | 758 |
(Dictionary with:(#foo -> #bar) |
8874 | 759 |
with:(#frob -> #baz)) |
760 |
at:#foobar ifAbsent:['no such index'] |
|
95 | 761 |
" |
362 | 762 |
! |
763 |
||
17795 | 764 |
atAllIndices:indexCollection |
765 |
"return the elements at each index from indexCollection." |
|
766 |
||
767 |
^ indexCollection collect:[:eachIdx | self at:eachIdx]. |
|
768 |
||
769 |
" |
|
770 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:#( 8 5 12 12 15) |
|
771 |
'abcdefghijklmnopqrstuvwxyz' atAllIndices:( 5 to: 10) |
|
772 |
" |
|
773 |
||
774 |
"Created: / 08-08-2010 / 01:15:06 / cg" |
|
775 |
! |
|
776 |
||
777 |
atIndex:index |
|
778 |
"return an element at a given index. This allows for seqentialCollections |
|
779 |
and orderedDictionaries to be both accessed via a numeric index." |
|
780 |
||
781 |
^ self at:index |
|
782 |
||
783 |
"Created: / 08-08-2010 / 00:50:10 / cg" |
|
784 |
! |
|
785 |
||
786 |
atIndex:index put:newValue |
|
787 |
"return an element at a given index. This allows for seqentialCollections |
|
788 |
and orderedDictionaries to be both accessed via a numeric index." |
|
789 |
||
790 |
^ self at:index put:newValue |
|
791 |
||
792 |
"Created: / 08-08-2010 / 00:50:27 / cg" |
|
793 |
! |
|
794 |
||
1366 | 795 |
before:anObject |
796 |
"return the element before the argument, anObject. |
|
5231 | 797 |
If anObject is not in the receiver, report an error; |
798 |
if anObject is the first in the receiver, return nil. |
|
1366 | 799 |
This depends on #before:ifAbsent: being implemented in a concrete class." |
800 |
||
801 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject] |
|
802 |
||
803 |
" |
|
8874 | 804 |
#(4 3 2 1) asOrderedCollection before:3. |
805 |
#(4 3 2 1) asOrderedCollection before:4 |
|
806 |
#(4 3 2 1) asOrderedCollection before:0 |
|
1366 | 807 |
" |
808 |
||
809 |
"Created: 10.5.1996 / 14:03:27 / cg" |
|
810 |
"Modified: 10.5.1996 / 14:03:44 / cg" |
|
811 |
! |
|
812 |
||
813 |
before:anObject ifAbsent:exceptionBlock |
|
8874 | 814 |
"return the element, before anObject. |
5231 | 815 |
If anObject is the first in the receiver, return nil. |
816 |
If there is no such element (anObject is not found), |
|
1366 | 817 |
return the value from exceptionBlock." |
818 |
||
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
819 |
|idx| |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
820 |
|
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
821 |
idx := self indexOf:anObject. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
822 |
idx ~~ 0 ifTrue:[ |
8874 | 823 |
idx == 1 ifTrue:[^ nil]. |
824 |
^ self at:(idx - 1). |
|
5501
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
825 |
]. |
3609f87cb0e6
after:ifAbsent: & before:ifAbsent: added
Claus Gittinger <cg@exept.de>
parents:
5489
diff
changeset
|
826 |
^ exceptionBlock value |
1366 | 827 |
|
5231 | 828 |
" |
8874 | 829 |
#(4 3 2 1) asOrderedCollection before:3 ifAbsent:nil. |
830 |
#(4 3 2 1) asOrderedCollection before:5 ifAbsent:nil. |
|
831 |
#(4 3 2 1) asOrderedCollection before:5. |
|
832 |
#(4 3 2 1) asOrderedCollection before:4 ifAbsent:nil. |
|
833 |
#(4 3 2 1) asOrderedCollection before:4. |
|
834 |
(2 to:10 by:2) before:4 ifAbsent:nil. |
|
835 |
(2 to:10 by:2) before:5 ifAbsent:nil. |
|
5231 | 836 |
" |
837 |
||
1366 | 838 |
! |
839 |
||
602 | 840 |
first |
8874 | 841 |
"return the first element; |
5231 | 842 |
report an error, if the collection is empty." |
602 | 843 |
|
8859
42a82dadb147
error raised in first and anElement
Claus Gittinger <cg@exept.de>
parents:
8853
diff
changeset
|
844 |
self isEmpty ifTrue:[^ self emptyCollectionError]. |
602 | 845 |
^ self at:1 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
846 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
847 |
" |
8874 | 848 |
args: |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
849 |
returns: firstElement <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
850 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
851 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
852 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
362 | 853 |
! |
854 |
||
5887 | 855 |
first:n |
856 |
"return the n first elements of the collection. |
|
857 |
Raises an error if there are not enough elements in the receiver." |
|
858 |
||
859 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
860 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
861 |
||
862 |
^ self copyFirst:n |
|
863 |
||
864 |
" |
|
8874 | 865 |
#(1 2 3 4 5) first:3 |
866 |
#(1 2 3 4 5) first:6 |
|
867 |
#(1 2 3 4 5) asSet first:3 |
|
868 |
'hello world' first:5 |
|
5887 | 869 |
" |
870 |
! |
|
871 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
872 |
indexOfNth:n occurrenceOf:what |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
873 |
"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
|
874 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
875 |
|idx cnt| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
876 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
877 |
n > self size ifTrue:[^ 0]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
878 |
cnt := 0. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
879 |
idx := 0. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
880 |
[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
881 |
idx := self indexOf:what startingAt:idx+1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
882 |
idx == 0 ifTrue:[^ 0]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
883 |
cnt := cnt + 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
884 |
cnt = n ifTrue:[ ^ idx]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
885 |
] loop. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
886 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
887 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
888 |
#(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
|
889 |
#(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
|
890 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
891 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
892 |
|
362 | 893 |
keyAtEqualValue:value |
894 |
"return the index of a value. |
|
895 |
This is normally not used (use indexOf:), but makes the |
|
896 |
protocol more compatible with dictionaries." |
|
897 |
||
898 |
^ self indexOf:value |
|
899 |
! |
|
900 |
||
901 |
keyAtEqualValue:value ifAbsent:exceptionBlock |
|
902 |
"return the index of a value. |
|
903 |
This is normally not used (use indexOf:), but makes the |
|
904 |
protocol more compatible with dictionaries." |
|
905 |
||
906 |
^ self indexOf:value ifAbsent:exceptionBlock |
|
602 | 907 |
! |
908 |
||
909 |
keyAtValue:value |
|
910 |
"return the index of a value. |
|
911 |
This is normally not used (use indexOf:), but makes the |
|
912 |
protocol more compatible with dictionaries." |
|
913 |
||
914 |
^ self identityIndexOf:value |
|
915 |
! |
|
916 |
||
917 |
keyAtValue:value ifAbsent:exceptionBlock |
|
918 |
"return the index of a value. |
|
919 |
This is normally not used (use indexOf:), but makes the |
|
920 |
protocol more compatible with dictionaries." |
|
921 |
||
922 |
^ self identityIndexOf:value ifAbsent:exceptionBlock |
|
923 |
! |
|
924 |
||
925 |
last |
|
5231 | 926 |
"return the last element; |
927 |
report an error, if the collection is empty." |
|
602 | 928 |
|
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
929 |
|sz| |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
930 |
|
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
931 |
(sz := self size) == 0 ifFalse:[ |
8874 | 932 |
^ self at:sz |
3408
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
933 |
]. |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
934 |
"error if collection is empty" |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
935 |
^ self emptyCollectionError |
411024a56ffd
check for being empty in #last
Claus Gittinger <cg@exept.de>
parents:
3234
diff
changeset
|
936 |
|
5231 | 937 |
" |
938 |
#(1 2 3 4 5) last |
|
939 |
#() last |
|
940 |
" |
|
1370
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
941 |
! |
b051117cc302
moved #upTo: down into SeqColl
Claus Gittinger <cg@exept.de>
parents:
1369
diff
changeset
|
942 |
|
5887 | 943 |
last:n |
944 |
"return the n last elements of the collection. |
|
945 |
Raises an error if there are not enough elements in the receiver." |
|
946 |
||
947 |
n < 0 ifTrue:[self error:'bad (negative) argument']. |
|
948 |
n > self size ifTrue:[^ self notEnoughElementsError]. |
|
949 |
||
950 |
^ self copyLast:n |
|
951 |
||
952 |
" |
|
8874 | 953 |
#(1 2 3 4 5) last:3 |
954 |
#(1 2 3 4 5) last:6 |
|
955 |
#(1 2 3 4 5) asSet last:3 |
|
956 |
'hello world' last:5 |
|
5887 | 957 |
" |
958 |
! |
|
959 |
||
4986 | 960 |
nth:n |
5231 | 961 |
"return the nth element; |
962 |
report an error, if the collections size is smaller than n." |
|
4986 | 963 |
|
964 |
^ self at:n |
|
965 |
||
966 |
" |
|
8874 | 967 |
args: |
4986 | 968 |
returns: nth element <object> |
969 |
" |
|
970 |
||
5231 | 971 |
" |
8874 | 972 |
#(10 20 30 40 50) nth:4 |
5231 | 973 |
#(10 20 30 40 50) nth:6 |
974 |
#() nth:1 |
|
975 |
" |
|
976 |
||
4986 | 977 |
! |
978 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
979 |
order |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
980 |
"the order is the set of keys " |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
981 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
982 |
^ 1 to:self size |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
983 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
984 |
|
4855 | 985 |
second |
5231 | 986 |
"return the second element; |
987 |
report an error, if the collections size is smaller than 2." |
|
4855 | 988 |
|
989 |
^ self at:2 |
|
990 |
||
991 |
" |
|
8874 | 992 |
args: |
4855 | 993 |
returns: secondElement <object> |
994 |
" |
|
995 |
||
996 |
"Modified: / 20.5.1998 / 14:50:25 / cg" |
|
997 |
! |
|
998 |
||
3037
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
999 |
swap:index1 with:index2 |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1000 |
"exchange two elements" |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1001 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1002 |
|t| |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1003 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1004 |
t := self at:index1. |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1005 |
self at:index1 put:(self at:index2). |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1006 |
self at:index2 put:t |
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1007 |
|
1293e34a06cf
added #swap:with: for compatibility
Claus Gittinger <cg@exept.de>
parents:
3003
diff
changeset
|
1008 |
"Modified: 15.10.1997 / 19:27:08 / cg" |
1 | 1009 |
! ! |
1010 |
||
602 | 1011 |
!SequenceableCollection methodsFor:'adding & removing'! |
1012 |
||
1013 |
add:anObject |
|
1014 |
"append the argument, anObject to the collection. |
|
1015 |
Return the argument, anObject. |
|
1016 |
||
1017 |
Notice, that this is modifies the receiver NOT a copy. |
|
1018 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1019 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1020 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1021 |
||
1022 |
|newSize| |
|
1023 |
||
1024 |
newSize := self size + 1. |
|
1025 |
self grow:newSize. |
|
1026 |
self at:newSize put:anObject. |
|
1027 |
^ anObject |
|
1028 |
||
1029 |
" |
|
1030 |
|a| |
|
1031 |
a := #(1 2 3 4 5 6 7 8). |
|
1032 |
a add:'hello'. |
|
1033 |
a |
|
1034 |
" |
|
1035 |
" |
|
1036 |
|c| |
|
1037 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1038 |
c add:'hello'. |
|
1039 |
c |
|
1040 |
" |
|
1041 |
! |
|
1042 |
||
1043 |
add:anElement beforeIndex:index |
|
1044 |
"insert the first argument, anObject into the collection before slot index. |
|
1045 |
Return the receiver (sigh - ST-80 compatibility). |
|
1046 |
||
1047 |
Notice, that this is modifies the receiver NOT a copy. |
|
1048 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1049 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1050 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1051 |
||
1052 |
|newSize| |
|
1053 |
||
1054 |
newSize := self size + 1. |
|
1055 |
self grow:newSize. |
|
1056 |
self replaceFrom:index + 1 to:newSize with:self startingAt:index. |
|
1057 |
self at:index put:anElement |
|
1058 |
||
1059 |
" |
|
1060 |
#(1 2 3 4 5 6 7 8) add:'hello' beforeIndex:5 |
|
1061 |
" |
|
202 | 1062 |
! |
1063 |
||
602 | 1064 |
addFirst:anObject |
1065 |
"prepend the argument, anObject to the collection. |
|
1066 |
Return the argument, anObject. |
|
1067 |
||
1068 |
Notice, that this is modifies the receiver NOT a copy. |
|
1069 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1070 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1071 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1072 |
||
1073 |
|newSize| |
|
1074 |
||
1075 |
newSize := self size + 1. |
|
1076 |
self grow:newSize. |
|
1077 |
self replaceFrom:2 to:newSize with:self startingAt:1. |
|
1078 |
self at:1 put:anObject. |
|
1079 |
^ anObject |
|
1080 |
||
1081 |
" |
|
8874 | 1082 |
|a| |
1083 |
a:= #(1 2 3 4 5 6 7 8). |
|
1084 |
a addFirst:'hello'. |
|
1085 |
a |
|
602 | 1086 |
" |
1087 |
" |
|
1088 |
|c| |
|
1089 |
c := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1090 |
c addFirst:'hello'. |
|
1091 |
c |
|
1092 |
" |
|
69 | 1093 |
! |
1094 |
||
602 | 1095 |
remove:anElement ifAbsent:aBlock |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1096 |
"search for an object which is equal to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1097 |
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
|
1098 |
Use equality compare (=) to search for an occurrence. |
602 | 1099 |
|
1100 |
Notice, that this is modifies the receiver NOT a copy. |
|
1101 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1102 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1103 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1104 |
||
8874 | 1105 |
|any |
602 | 1106 |
dstIndex "{ Class: SmallInteger }" |
1107 |
sz "{ Class: SmallInteger }"| |
|
1108 |
||
1109 |
dstIndex := 1. |
|
1110 |
any := false. |
|
1111 |
sz := self size. |
|
1112 |
1 to:sz do:[:srcIndex | |
|
8874 | 1113 |
(anElement = (self at:srcIndex)) ifTrue:[ |
1114 |
any := true |
|
1115 |
] ifFalse:[ |
|
1116 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1117 |
self at:dstIndex put:(self at:srcIndex) |
|
1118 |
]. |
|
1119 |
dstIndex := dstIndex + 1 |
|
1120 |
] |
|
602 | 1121 |
]. |
1122 |
any ifTrue:[ |
|
8874 | 1123 |
self grow:dstIndex - 1. |
1124 |
^ anElement |
|
602 | 1125 |
]. |
1126 |
^ aBlock value |
|
1127 |
||
1128 |
" |
|
1422 | 1129 |
#(1 2 3 4 5 6 7 8 9 0) remove:3 ifAbsent:[Transcript showCR:'no'] |
602 | 1130 |
#(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
|
1131 |
#(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
|
1132 |
#(1.0 2.0 3.0 4.0 5.0) removeIdentical:5 ifAbsent:[#oops] |
602 | 1133 |
" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1134 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1135 |
"Modified: 1.2.1997 / 11:49:01 / cg" |
602 | 1136 |
! |
1137 |
||
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1138 |
removeAllSuchThat:aBlock |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1139 |
"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
|
1140 |
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
|
1141 |
for which it returns true, are removed. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1142 |
Return a collection containing the removed elements." |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1143 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1144 |
"/ 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
|
1145 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1146 |
|runIndex removed element| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1147 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1148 |
removed := self species new. |
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 |
runIndex := 1. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1151 |
[runIndex <= self size] whileTrue:[ |
8874 | 1152 |
element := self at:runIndex. |
1153 |
(aBlock value:element) ifTrue:[ |
|
1154 |
removed add:element. |
|
1155 |
self removeAtIndex:runIndex |
|
1156 |
] ifFalse:[ |
|
1157 |
runIndex := runIndex + 1 |
|
1158 |
] |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1159 |
]. |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1160 |
^ removed |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1161 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1162 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1163 |
|coll| |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1164 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1165 |
coll := OrderedCollection withAll:(1 to:10). |
1422 | 1166 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
1167 |
Transcript showCR:coll |
|
1369
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1168 |
" |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1169 |
|
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1170 |
"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
|
1171 |
! |
5e7b6fabd248
raised #atAll:put: up in the hierarchy
Claus Gittinger <cg@exept.de>
parents:
1366
diff
changeset
|
1172 |
|
602 | 1173 |
removeAtIndex:anIndex |
1174 |
"remove the element stored at anIndex. Return the removed object. |
|
1175 |
||
1176 |
Notice, that this is modifies the receiver NOT a copy. |
|
1177 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1178 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1179 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1180 |
||
1181 |
|element| |
|
1182 |
||
1183 |
element := self at:anIndex. |
|
1184 |
self removeFromIndex:anIndex toIndex:anIndex. |
|
1185 |
^ element |
|
1186 |
||
1187 |
" |
|
1188 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeAtIndex:3 |
|
1189 |
#(1 2 3 4 5) asOrderedCollection removeAtIndex:6 |
|
1190 |
#($a $b $c $d $e $f $g) removeAtIndex:3 |
|
1191 |
" |
|
13 | 1192 |
! |
1193 |
||
602 | 1194 |
removeFirst |
1195 |
"remove the first element of the receiver and return it. |
|
1196 |
||
1197 |
Notice, that this is modifies the receiver NOT a copy. |
|
1198 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1199 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1200 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1201 |
||
1202 |
^ self removeAtIndex:1 |
|
1203 |
||
1204 |
" |
|
1205 |
|a| |
|
1206 |
a := #(1 2 3 4 5 6). |
|
1207 |
a removeFirst. |
|
8874 | 1208 |
a |
602 | 1209 |
" |
13 | 1210 |
! |
1211 |
||
2294 | 1212 |
removeFromIndex:startIndex |
1213 |
"remove the elements stored at indexes from startIndex to the end. |
|
1214 |
Return the receiver. |
|
1215 |
Returning the receiver is a historic leftover - it may at one |
|
1216 |
time return a collection of the removed elements. |
|
1217 |
||
8874 | 1218 |
Notice, that this is modifies the receiver - NOT a copy; |
2294 | 1219 |
therefore any other users of the receiver will also see this change. |
1220 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1221 |
due to the grow:-message, which is inefficient for fixed size |
2294 | 1222 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1223 |
||
1224 |
^ self removeFromIndex:startIndex toIndex:(self size) |
|
1225 |
||
1226 |
" |
|
8874 | 1227 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 |
2294 | 1228 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 |
1229 |
" |
|
1230 |
||
1231 |
"Modified: 28.1.1997 / 12:35:20 / cg" |
|
1232 |
! |
|
1233 |
||
602 | 1234 |
removeFromIndex:startIndex toIndex:endIndex |
1235 |
"remove the elements stored at indexes between startIndex and endIndex. |
|
1236 |
Return the receiver. |
|
1237 |
Returning the receiver is a historic leftover - it may at one |
|
1238 |
time return a collection of the removed elements. |
|
1239 |
||
8874 | 1240 |
Notice, that this is modifies the receiver - NOT a copy; |
602 | 1241 |
therefore any other users of the receiver will also see this change. |
1242 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1243 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1244 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1245 |
||
1246 |
|size newSize| |
|
1247 |
||
1248 |
(endIndex >= (size := self size)) ifTrue:[ |
|
8874 | 1249 |
self grow:(startIndex - 1) |
602 | 1250 |
] ifFalse:[ |
8874 | 1251 |
newSize := size - endIndex + startIndex - 1. |
1252 |
self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1253 |
self grow:newSize |
|
602 | 1254 |
] |
1255 |
||
1256 |
"/ |newSize| |
|
1257 |
"/ |
|
1258 |
"/ newSize := self size - endIndex + startIndex - 1. |
|
1259 |
"/ newSize <= 0 ifTrue:[ |
|
1260 |
"/ self grow:0 |
|
1261 |
"/ ] ifFalse:[ |
|
1262 |
"/ self replaceFrom:startIndex to:newSize with:self startingAt:(endIndex + 1). |
|
1263 |
"/ self grow:newSize |
|
1264 |
"/ ] |
|
1265 |
||
1266 |
" |
|
8874 | 1267 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:3 toIndex:5 |
1268 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:3 toIndex:5 |
|
1269 |
#(1 2 3 4 5 6 7 8 9 0) asOrderedCollection removeFromIndex:1 toIndex:10 |
|
1270 |
#(1 2 3 4 5 6 7 8 9 0) removeFromIndex:1 toIndex:10 |
|
602 | 1271 |
" |
1272 |
! |
|
1273 |
||
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1274 |
removeIdentical:anElement ifAbsent:aBlock |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1275 |
"search for an object which is identical to anElement; |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1276 |
if found remove and return it; if not, return the value from evaluating aBlock. |
2363 | 1277 |
Uses identity compare (==) to search for an occurrence. |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1278 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1279 |
Notice, that this is modifies the receiver NOT a copy. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1280 |
Also note, that it may be a slow operation for some collections, |
8874 | 1281 |
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
|
1282 |
collections (i.e. for Strings and Arrays it is not recommened)." |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1283 |
|
2363 | 1284 |
|any el |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1285 |
dstIndex "{ Class: SmallInteger }" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1286 |
sz "{ Class: SmallInteger }"| |
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 |
dstIndex := 1. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1289 |
any := false. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1290 |
sz := self size. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1291 |
1 to:sz do:[:srcIndex | |
8874 | 1292 |
el := self at:srcIndex. |
1293 |
(anElement == el) ifTrue:[ |
|
1294 |
any := true |
|
1295 |
] ifFalse:[ |
|
1296 |
(dstIndex ~~ srcIndex) ifTrue:[ |
|
1297 |
self at:dstIndex put:el |
|
1298 |
]. |
|
1299 |
dstIndex := dstIndex + 1 |
|
1300 |
] |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1301 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1302 |
any ifTrue:[ |
8874 | 1303 |
self grow:dstIndex - 1. |
1304 |
^ anElement |
|
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1305 |
]. |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1306 |
^ aBlock value |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1307 |
|
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1308 |
" |
8874 | 1309 |
#(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
|
1310 |
#(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
|
1311 |
" |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1312 |
|
2363 | 1313 |
"Modified: 8.2.1997 / 16:26:49 / cg" |
2346
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1314 |
! |
7ebe79ad61a3
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2294
diff
changeset
|
1315 |
|
602 | 1316 |
removeIndex:index |
1317 |
"remove the argument stored at index. Return the receiver. |
|
1318 |
||
1319 |
Notice, that this is modifies the receiver NOT a copy. |
|
1320 |
Also note, that it may be a slow operation for some collections, |
|
8874 | 1321 |
due to the grow:-message, which is inefficient for fixed size |
602 | 1322 |
collections (i.e. for Strings and Arrays it is not recommened)." |
1323 |
||
1324 |
self removeFromIndex:index toIndex:index |
|
1325 |
||
1326 |
" |
|
1327 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeIndex:3 |
|
1328 |
#(1 2 3 4 5) asOrderedCollection removeIndex:6 |
|
1329 |
#($a $b $c $d $e $f $g) removeIndex:3 |
|
1330 |
" |
|
77 | 1331 |
! |
1332 |
||
602 | 1333 |
removeLast |
1334 |
"remove the last element of the receiver and return it. |
|
1335 |
||
1336 |
Notice, that this is modifies the receiver NOT a copy. |
|
1337 |
Also note, that it may be a slow operation for some collections, |
|
1338 |
due to the grow:-message, which is inefficient for fixed size |
|
1339 |
collections (i.e. for Strings and Arrays it is not recommened)." |
|
1340 |
||
8874 | 1341 |
^ self removeAtIndex:(self size) |
602 | 1342 |
|
1343 |
" |
|
1344 |
|a| |
|
1345 |
a := #(1 2 3 4 5 6). |
|
1346 |
a removeLast. |
|
8874 | 1347 |
a |
602 | 1348 |
" |
1349 |
! ! |
|
1350 |
||
11407 | 1351 |
!SequenceableCollection methodsFor:'combinatoric'! |
1352 |
||
1353 |
combinationsDo: aBlock |
|
11463 | 1354 |
"Repeatly evaluate aBlock with all combinations of elements from the receivers elements. |
1355 |
The receivers elements must be collections of the individuals to be taken for the combinations" |
|
11407 | 1356 |
|
1357 |
self combinationsStartingAt:1 prefix:#() do:aBlock |
|
1358 |
||
1359 |
" |
|
1360 |
(Array |
|
11410 | 1361 |
with:($a to:$d)) |
1362 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1363 |
" |
|
1364 |
" |
|
1365 |
(Array |
|
11407 | 1366 |
with:($a to:$d) |
1367 |
with:(1 to: 4)) |
|
1368 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1369 |
" |
|
1370 |
" |
|
1371 |
will print a1, a2, a3, a4, b1, ... , d1, d2, d3, d4 |
|
1372 |
" |
|
11408 | 1373 |
"all combinations of two letters are generated with: |
1374 |
(Array |
|
1375 |
with:($a to:$z) |
|
1376 |
with:($a to:$z)) |
|
1377 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1378 |
" |
|
11432 | 1379 |
" |
1380 |
(Array |
|
1381 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1382 |
with:#(A)) |
|
1383 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1384 |
" |
|
11407 | 1385 |
! |
1386 |
||
1387 |
combinationsStartingAt:anInteger prefix:prefix do:aBlock |
|
1388 |
"a helper for combinationsDo:" |
|
1389 |
||
11431 | 1390 |
|loopedElement| |
1391 |
||
1392 |
loopedElement := self at:anInteger. |
|
1393 |
||
11407 | 1394 |
anInteger == self size ifTrue:[ |
11431 | 1395 |
loopedElement do:[:el | aBlock value:(prefix copyWith:el)]. |
11407 | 1396 |
^ self. |
11431 | 1397 |
]. |
1398 |
||
1399 |
loopedElement do:[:el | |
|
1400 |
|newPrefix| |
|
1401 |
||
1402 |
newPrefix := (prefix copyWith:el). |
|
1403 |
self combinationsStartingAt:anInteger+1 prefix:newPrefix do:aBlock |
|
11407 | 1404 |
]. |
1405 |
||
1406 |
" |
|
1407 |
(Array |
|
1408 |
with:($a to:$d) |
|
1409 |
with:(1 to: 4)) |
|
1410 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1411 |
" |
|
11431 | 1412 |
" |
1413 |
(Array |
|
1414 |
with:#(1 2 3 4 5 6 7 8 9) |
|
1415 |
with:#(A)) |
|
1416 |
combinationsDo:[:eachCombination | Transcript showCR: eachCombination] |
|
1417 |
" |
|
11407 | 1418 |
! |
1419 |
||
11462 | 1420 |
lexicographicPermutationsDo:aBlock |
1421 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1422 |
Reorder the copy so that aBlock is presented all (self size factorial) possible |
|
1423 |
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
|
1424 |
|
11462 | 1425 |
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
|
1426 |
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
|
1427 |
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
|
1428 |
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
|
1429 |
to copy the argument, if it has to be stored somewhere." |
11462 | 1430 |
|
1431 |
self shallowCopy lexicographicPermutationsStartingAt:1 do:aBlock |
|
1432 |
||
1433 |
" |
|
1434 |
(1 to: 4) asArray lexicographicPermutationsDo:[:each | Transcript showCR: each printString] |
|
1435 |
" |
|
1436 |
! |
|
1437 |
||
1438 |
lexicographicPermutationsStartingAt:anInteger do:aBlock |
|
1439 |
"a helper for lexicographicPermutationsDo:" |
|
1440 |
||
1441 |
|mySize values indices| |
|
1442 |
||
1443 |
mySize := self size. |
|
1444 |
anInteger > mySize ifTrue: [^ self]. |
|
1445 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
1446 |
values := self copyFrom:anInteger to:mySize. |
|
1447 |
indices := (anInteger to:mySize) asArray. |
|
1448 |
values sortWith:indices. |
|
1449 |
indices do:[:i | |
|
1450 |
self swap: anInteger with: i. |
|
1451 |
self lexicographicPermutationsStartingAt:(anInteger + 1) do: aBlock. |
|
1452 |
self swap: anInteger with: i |
|
1453 |
] |
|
1454 |
||
1455 |
" |
|
1456 |
#(1 2 3 4 5) lexicographicPermutationsDo: [:each | Transcript showCR: each] |
|
1457 |
" |
|
1458 |
! |
|
1459 |
||
1460 |
permutationsDo:aBlock |
|
1461 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1462 |
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
|
1463 |
|
11462 | 1464 |
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
|
1465 |
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
|
1466 |
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
|
1467 |
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
|
1468 |
to copy the argument, if it has to be stored somewhere." |
11407 | 1469 |
|
1470 |
self shallowCopy permutationsStartingAt:1 do:aBlock |
|
1471 |
||
1472 |
" |
|
11462 | 1473 |
(1 to: 4) asArray permutationsDo:[:each | Transcript showCR: each printString] |
11407 | 1474 |
" |
1475 |
||
1476 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
1477 |
! |
|
1478 |
||
1479 |
permutationsStartingAt: anInteger do: aBlock |
|
11462 | 1480 |
"a helper for permutationsDo:" |
11407 | 1481 |
|
1482 |
|mySize| |
|
1483 |
||
1484 |
mySize := self size. |
|
11462 | 1485 |
anInteger > mySize ifTrue: [^ self]. |
1486 |
anInteger = mySize ifTrue: [^ aBlock value: self]. |
|
11407 | 1487 |
anInteger to:mySize do:[:i | |
1488 |
self swap: anInteger with: i. |
|
11462 | 1489 |
self permutationsStartingAt:(anInteger + 1) do: aBlock. |
11407 | 1490 |
self swap: anInteger with: i |
1491 |
] |
|
1492 |
||
1493 |
" |
|
1494 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
11462 | 1495 |
|
1496 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1497 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
11407 | 1498 |
" |
1499 |
||
1500 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
11462 | 1501 |
! |
1502 |
||
1503 |
permutationsStartingAt:anInteger with:anotherCollection do:aBlock |
|
1504 |
"a helper for permutationsDo:" |
|
1505 |
||
1506 |
|mySize| |
|
1507 |
||
1508 |
mySize := self size. |
|
1509 |
anInteger > mySize ifTrue: [^ self]. |
|
1510 |
anInteger = mySize ifTrue: [^ aBlock value:self value:anotherCollection]. |
|
1511 |
anInteger to:mySize do:[:i | |
|
1512 |
self swap: anInteger with:i. |
|
1513 |
anotherCollection swap: anInteger with:i. |
|
1514 |
self permutationsStartingAt:(anInteger + 1) with:anotherCollection do:aBlock. |
|
1515 |
self swap:anInteger with: i. |
|
1516 |
anotherCollection swap:anInteger with:i. |
|
1517 |
] |
|
1518 |
||
1519 |
" |
|
1520 |
#(1 2 3 4 5) permutationsDo: [:each | Transcript showCR: each] |
|
1521 |
||
1522 |
notice the copy - Transcript simply buffers incoming strings !!!!!! |
|
1523 |
'hello' permutationsDo: [:each | Transcript showCR: each copy] |
|
1524 |
" |
|
1525 |
||
1526 |
"Modified: / 22-10-2007 / 13:51:12 / cg" |
|
1527 |
! |
|
1528 |
||
1529 |
permutationsWith:anotherCollection do:aBlock |
|
1530 |
"Repeatly evaluate aBlock with a single copy of the receiver. |
|
1531 |
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
|
1532 |
|
11462 | 1533 |
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
|
1534 |
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
|
1535 |
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
|
1536 |
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
|
1537 |
to copy the argument, if it has to be stored somewhere." |
11462 | 1538 |
|
1539 |
self shallowCopy permutationsStartingAt:1 with:anotherCollection shallowCopy do:aBlock |
|
1540 |
||
1541 |
" |
|
1542 |
(1 to: 4) asArray permutationsWith:'abcd' |
|
1543 |
do:[:each1 :each2 | Transcript showCR: each2 copy] |
|
1544 |
" |
|
1545 |
||
1546 |
"Modified: / 13.11.2001 / 13:37:35 / cg" |
|
11407 | 1547 |
! ! |
1548 |
||
602 | 1549 |
!SequenceableCollection methodsFor:'comparing'! |
1550 |
||
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1551 |
< aCollection |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1552 |
"compare two sequenceable collections" |
8874 | 1553 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1554 |
|size "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1555 |
aCollectionSize "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1556 |
min "{ Class: SmallInteger }" |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1557 |
v1 v2| |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1558 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1559 |
size := self size. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1560 |
aCollectionSize := aCollection size. |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1561 |
aCollectionSize == 0 ifTrue:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1562 |
aCollection isSequenceable ifFalse:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1563 |
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
|
1564 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1565 |
]. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1566 |
|
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1567 |
size < aCollectionSize ifTrue:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1568 |
min := size |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1569 |
] ifFalse:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1570 |
min := aCollectionSize |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1571 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1572 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1573 |
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
|
1574 |
v1 := self at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1575 |
v2 := aCollection at: i. |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1576 |
(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
|
1577 |
]. |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1578 |
^ size < aCollectionSize |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1579 |
|
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1580 |
" |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1581 |
#(1 2 3) < #(1) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1582 |
#(1 2 3) < #(2) |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1583 |
#(1 2 3) < #() |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1584 |
#(1 2 3) < #(1 2 3) |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1585 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1586 |
|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 := 'hello world'. |
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 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1596 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1597 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1598 |
a := CharacterArray fromString:'hello world'. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1599 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1600 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1601 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1602 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1603 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1604 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1605 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1606 |
|a b| |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1607 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1608 |
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
|
1609 |
b := a copy. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1610 |
Time millisecondsToRun:[ |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1611 |
1000000 timesRepeat:[ |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1612 |
a < b |
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1613 |
] |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1614 |
] |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1615 |
" |
9478
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1616 |
|
652133ccefef
dont be confused when comparing against a non-collection.
Claus Gittinger <cg@exept.de>
parents:
9454
diff
changeset
|
1617 |
"Modified: / 04-08-2006 / 11:45:10 / cg" |
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1618 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1619 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1620 |
<= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1621 |
"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
|
1622 |
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
|
1623 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1624 |
^ (aCollection < self) not |
8183
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1625 |
! |
93f8b0cc734f
Only use #< for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8180
diff
changeset
|
1626 |
|
602 | 1627 |
= aCollection |
1628 |
"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
|
1629 |
with equal contents, and if they are of the same species." |
602 | 1630 |
|
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1631 |
| stop "{ Class: SmallInteger }" | |
602 | 1632 |
|
1633 |
(aCollection == self) ifTrue:[^true]. |
|
1634 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
9366 | 1635 |
(aCollection species = self species) ifFalse:[^false]. |
602 | 1636 |
|
1637 |
stop := self size. |
|
1638 |
stop == (aCollection size) ifFalse:[^false]. |
|
1639 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1640 |
1 to:stop do:[:index| |
9366 | 1641 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
602 | 1642 |
]. |
1643 |
^ true |
|
1644 |
||
1645 |
" |
|
8874 | 1646 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1647 |
#($1 $2 $3 $4 $5) = #(1 2 3 4 5) |
|
1648 |
#($1 $2 $3 $4 $5) = '12345' |
|
1649 |
#($1 $2 $3 $4 $5) = '54321' asSortedCollection |
|
602 | 1650 |
" |
1651 |
! |
|
1652 |
||
8311
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1653 |
> aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1654 |
"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
|
1655 |
receiver is greater than the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1656 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1657 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1658 |
^ aCollection < self |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1659 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1660 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1661 |
>= aCollection |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1662 |
"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
|
1663 |
receiver is greater than or equal to the argument. |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1664 |
Otherwise return false." |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1665 |
|
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1666 |
^ (self < aCollection) not |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1667 |
! |
bca1157b6a2f
Move #<= and #>= to SequenceableCollection.
Stefan Vogel <sv@exept.de>
parents:
8276
diff
changeset
|
1668 |
|
8874 | 1669 |
commonPrefixWith:aCollection |
6507 | 1670 |
"return the common prefix of myself and the argument, aCollection. |
1671 |
If there is none, an empty collection is returned." |
|
1672 |
||
1673 |
^ self commonPrefixWith:aCollection ignoreCase:false |
|
1674 |
||
1675 |
" |
|
8874 | 1676 |
'hello' commonPrefixWith:'hello' |
1677 |
'hello' commonPrefixWith:'hElLo' |
|
1678 |
'hello' commonPrefixWith:'hello world' |
|
1679 |
'hello' commonPrefixWith:'hElLo WoRlD' |
|
1680 |
||
1681 |
'hello world' commonPrefixWith:'hello' |
|
1682 |
'hello WoRlD' commonPrefixWith:'hElLo' |
|
1683 |
||
1684 |
'abcd' commonPrefixWith:'bcde' |
|
1685 |
||
1686 |
'abcd' commonPrefixWith:'abab' |
|
1687 |
'abcd' commonPrefixWith:'ab' |
|
1688 |
'abcd' commonPrefixWith:'ababab' |
|
1689 |
'abcd' commonPrefixWith:'abcdef' |
|
1690 |
||
1691 |
'abab' commonPrefixWith:'abcd' |
|
1692 |
'ab' commonPrefixWith:'abcd' |
|
1693 |
'ababab' commonPrefixWith:'abcd' |
|
1694 |
'abcdef' commonPrefixWith:'abcd' |
|
6507 | 1695 |
" |
1696 |
! |
|
1697 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1698 |
commonPrefixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1699 |
"return the common prefix of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1700 |
If there is none, an empty collection is returned." |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1701 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1702 |
|matchLen| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1703 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1704 |
matchLen := self size min:aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1705 |
1 to:matchLen do:[:idx | |
8874 | 1706 |
|elHere elThere same| |
1707 |
||
1708 |
elHere := self at:idx. |
|
1709 |
elThere := aCollection at:idx. |
|
1710 |
||
1711 |
ignoreCase ifTrue:[ |
|
1712 |
same := elHere sameAs:elThere |
|
1713 |
] ifFalse:[ |
|
1714 |
same := elHere = elThere |
|
1715 |
]. |
|
1716 |
same ifFalse:[ |
|
1717 |
^ self copyTo:(idx - 1). |
|
1718 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1719 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1720 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1721 |
^ self copyTo:matchLen |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1722 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1723 |
" |
8874 | 1724 |
'hello' commonPrefixWith:'hello' ignoreCase:true |
1725 |
'hello' commonPrefixWith:'hElLo' ignoreCase:true |
|
1726 |
'hello' commonPrefixWith:'hello world' ignoreCase:true |
|
1727 |
'hello' commonPrefixWith:'hElLo WoRlD' ignoreCase:true |
|
1728 |
||
1729 |
'hello world' commonPrefixWith:'hello' ignoreCase:true |
|
1730 |
'hello WoRlD' commonPrefixWith:'hElLo' ignoreCase:true |
|
1731 |
||
1732 |
'abcd' commonPrefixWith:'bcde' ignoreCase:true |
|
1733 |
||
1734 |
'abcd' commonPrefixWith:'abab' ignoreCase:true |
|
1735 |
'abcd' commonPrefixWith:'ab' ignoreCase:true |
|
1736 |
'abcd' commonPrefixWith:'ababab' ignoreCase:true |
|
1737 |
'abcd' commonPrefixWith:'abcdef' ignoreCase:true |
|
1738 |
||
1739 |
'abab' commonPrefixWith:'abcd' ignoreCase:true |
|
1740 |
'ab' commonPrefixWith:'abcd' ignoreCase:true |
|
1741 |
'ababab' commonPrefixWith:'abcd' ignoreCase:true |
|
1742 |
'abcdef' commonPrefixWith:'abcd' ignoreCase:true |
|
1743 |
" |
|
1744 |
! |
|
1745 |
||
1746 |
commonSuffixWith:aCollection |
|
6507 | 1747 |
"return the common suffix (tail) of myself and the argument, aCollection. |
1748 |
If there is none, an empty collection is returned." |
|
1749 |
||
1750 |
^ self commonSuffixWith:aCollection ignoreCase:false |
|
1751 |
||
1752 |
" |
|
8874 | 1753 |
'hello' commonSuffixWith:'hello' |
1754 |
'hello' commonSuffixWith:'hElLo' |
|
1755 |
'hello' commonSuffixWith:'hello world' |
|
1756 |
'hello' commonSuffixWith:'hElLo WoRlD' |
|
1757 |
'hello2 world' commonSuffixWith:'hello world' |
|
1758 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' |
|
1759 |
||
1760 |
'hello world' commonSuffixWith:'world' |
|
1761 |
'hello WoRlD' commonSuffixWith:'world' |
|
1762 |
||
1763 |
'dcba' commonSuffixWith:'edcb' |
|
1764 |
||
1765 |
'dcba' commonSuffixWith:'baba' |
|
1766 |
'dcba' commonSuffixWith:'ba' |
|
1767 |
'dcba' commonSuffixWith:'bababa' |
|
1768 |
'dcba' commonSuffixWith:'fdcba' |
|
1769 |
||
1770 |
'baba' commonSuffixWith:'dcba' |
|
1771 |
'ba' commonSuffixWith:'dcba' |
|
1772 |
'bababa' commonSuffixWith:'dcba' |
|
1773 |
'fdcba' commonSuffixWith:'dcba' |
|
6507 | 1774 |
" |
1775 |
! |
|
1776 |
||
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1777 |
commonSuffixWith:aCollection ignoreCase:ignoreCase |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1778 |
"return the common suffix (tail) of myself and the argument, aCollection. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1779 |
If there is none, an empty collection is returned." |
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 |
|matchLen l1 l2| |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1782 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1783 |
l1 := self size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1784 |
l2 := aCollection size. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1785 |
matchLen := l1 min:l2. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1786 |
1 to:matchLen do:[:idx | |
8874 | 1787 |
|elHere elThere same| |
1788 |
||
1789 |
elHere := self at:(l1 - idx + 1). |
|
1790 |
elThere := aCollection at:(l2 - idx + 1). |
|
1791 |
||
1792 |
ignoreCase ifTrue:[ |
|
1793 |
same := elHere sameAs:elThere |
|
1794 |
] ifFalse:[ |
|
1795 |
same := elHere = elThere |
|
1796 |
]. |
|
1797 |
same ifFalse:[ |
|
1798 |
^ self copyFrom:(l1 - idx + 2). |
|
1799 |
] |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1800 |
]. |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1801 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1802 |
^ self copyFrom:(l1 - matchLen + 1) |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1803 |
|
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1804 |
" |
8874 | 1805 |
'hello' commonSuffixWith:'hello' ignoreCase:true |
1806 |
'hello' commonSuffixWith:'hElLo' ignoreCase:true |
|
1807 |
'hello' commonSuffixWith:'hello world' ignoreCase:true |
|
1808 |
'hello' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1809 |
'hello2 world' commonSuffixWith:'hello world' ignoreCase:true |
|
1810 |
'hello2 world' commonSuffixWith:'hElLo WoRlD' ignoreCase:true |
|
1811 |
||
1812 |
'hello world' commonSuffixWith:'world' ignoreCase:true |
|
1813 |
'hello WoRlD' commonSuffixWith:'world' ignoreCase:true |
|
1814 |
||
1815 |
'dcba' commonSuffixWith:'edcb' ignoreCase:true |
|
1816 |
||
1817 |
'dcba' commonSuffixWith:'baba' ignoreCase:true |
|
1818 |
'dcba' commonSuffixWith:'ba' ignoreCase:true |
|
1819 |
'dcba' commonSuffixWith:'bababa' ignoreCase:true |
|
1820 |
'dcba' commonSuffixWith:'fdcba' ignoreCase:true |
|
1821 |
||
1822 |
'baba' commonSuffixWith:'dcba' ignoreCase:true |
|
1823 |
'ba' commonSuffixWith:'dcba' ignoreCase:true |
|
1824 |
'bababa' commonSuffixWith:'dcba' ignoreCase:true |
|
1825 |
'fdcba' commonSuffixWith:'dcba' ignoreCase:true |
|
6504
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1826 |
" |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1827 |
! |
8001aa813fac
added commonPrefix/commonSuffix
Claus Gittinger <cg@exept.de>
parents:
6183
diff
changeset
|
1828 |
|
5813 | 1829 |
deepSameContentsAs:aCollection |
1830 |
"return true, if the receiver and the arg have the same contents |
|
1831 |
in both the named instance vars and any indexed instVars. |
|
1832 |
This method descends into referenced objects, where #sameContentsAs: does not descend. |
|
1833 |
||
1834 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
1835 |
unused space" |
|
1836 |
||
1837 |
|index "{ Class: SmallInteger }" |
|
1838 |
stop "{ Class: SmallInteger }" | |
|
1839 |
||
1840 |
(aCollection == self) ifTrue:[^true]. |
|
1841 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
1842 |
||
1843 |
stop := self size. |
|
1844 |
stop == (aCollection size) ifFalse:[^false]. |
|
1845 |
||
1846 |
index := 1. |
|
1847 |
[index <= stop] whileTrue:[ |
|
8874 | 1848 |
((self at:index) deepSameContentsAs:(aCollection at:index)) ifFalse:[^false]. |
1849 |
index := index + 1 |
|
5813 | 1850 |
]. |
1851 |
^ true |
|
1852 |
||
1853 |
" |
|
8874 | 1854 |
#(1 2 3 4 5) deepSameContentsAs: #(1 2 3 4 5) |
1855 |
#($1 $2 $3 $4 $5) deepSameContentsAs: #(1 2 3 4 5) |
|
1856 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '12345' |
|
1857 |
#($1 $2 $3 $4 $5) deepSameContentsAs: '54321' asSortedCollection |
|
5813 | 1858 |
" |
1859 |
! |
|
1860 |
||
602 | 1861 |
endsWith:aCollection |
1862 |
"return true, if the receivers last elements match those |
|
1863 |
of aCollection" |
|
1864 |
||
1865 |
|index1 "{ Class: SmallInteger }" |
|
8874 | 1866 |
index2 "{ Class: SmallInteger }" |
1867 |
stop "{ Class: SmallInteger }" |
|
602 | 1868 |
sz "{ Class: SmallInteger }"| |
1869 |
||
1870 |
(aCollection == self) ifTrue:[^ true]. |
|
1871 |
(aCollection isSequenceable) ifFalse:[^ false]. |
|
1872 |
||
1873 |
stop := aCollection size. |
|
77 | 1874 |
sz := self size. |
602 | 1875 |
stop > sz ifTrue:[^false]. |
1876 |
||
1877 |
index1 := sz. |
|
1878 |
index2 := stop. |
|
1879 |
[index2 > 0] whileTrue:[ |
|
8874 | 1880 |
(self at:index1) = (aCollection at:index2) ifFalse:[^ false]. |
1881 |
index1 := index1 - 1. |
|
1882 |
index2 := index2 - 1 |
|
77 | 1883 |
]. |
602 | 1884 |
^ true |
1885 |
||
1886 |
" |
|
1887 |
'abcde' endsWith:#($d $e) |
|
8874 | 1888 |
#[1 2 3 4] endsWith:#(3 4) |
602 | 1889 |
#(1 2 3 4) asOrderedCollection endsWith:#(3 4) |
1890 |
" |
|
1891 |
! |
|
1892 |
||
7278 | 1893 |
endsWithAnyOf:aCollectionOfCollections |
7279 | 1894 |
"return true, if the receiver endswith any in aCollection" |
7278 | 1895 |
|
1896 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self endsWith:eachTriedEnd] |
|
1897 |
||
1898 |
" |
|
1899 |
'abcde' endsWithAnyOf:#('DE' 'de') |
|
1900 |
'abcde' endsWithAnyOf:#('DF' 'df') |
|
8874 | 1901 |
#[1 2 3 4] endsWithAnyOf:#( #(3 5) #(2 4)) |
1902 |
#[1 2 3 4] endsWithAnyOf:#( #(3 4) #(2 4)) |
|
7278 | 1903 |
" |
1904 |
! |
|
1905 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1906 |
hasEqualElements: otherCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1907 |
"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
|
1908 |
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
|
1909 |
element of otherCollection. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1910 |
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
|
1911 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1912 |
^ self isSameSequenceAs:otherCollection |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1913 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
1914 |
|
602 | 1915 |
hash |
1916 |
"return a hash key for the receiver" |
|
1917 |
||
1918 |
"this hash is stupid - but for larger collections, the hashing |
|
1919 |
time can become much bigger than the time lost in added probing. |
|
1920 |
Time will show ..." |
|
1921 |
||
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1922 |
|mySize h| |
3653 | 1923 |
|
1924 |
mySize := self size. |
|
1925 |
mySize == 0 ifTrue:[^ 0]. |
|
7522
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1926 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1927 |
h := (self at:1) hash. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1928 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1929 |
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
|
1930 |
h := h bitAnd:16r01FFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1931 |
h := (h bitShift:5) + self size. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1932 |
^ h bitAnd:16r3FFFFFFF. |
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1933 |
|
7a8022068bb5
avoid Large arithmetic when computing the hashValue.
Claus Gittinger <cg@exept.de>
parents:
7490
diff
changeset
|
1934 |
"/ 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
|
1935 |
"/ ^ ((((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
|
1936 |
|
d81bbfd885d9
hash: changed to be useful for intervals too.
Claus Gittinger <cg@exept.de>
parents:
6744
diff
changeset
|
1937 |
" |
8874 | 1938 |
#(1 2 3 4 5 6) hash |
1939 |
(1 to:6) hash |
|
1940 |
||
1941 |
#(1 2 3 4 5.0) asOrderedCollection hash |
|
1942 |
#(1 2 3 4 5) hash |
|
602 | 1943 |
" |
3653 | 1944 |
|
1945 |
"Modified: / 27.3.1998 / 17:33:49 / cg" |
|
602 | 1946 |
! |
1947 |
||
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1948 |
identicalContentsAs:aCollection |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1949 |
"return true if the receiver and aCollection represent collections |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1950 |
with identical contents. This is much like #=, but compares |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1951 |
elements using #== instead of #=." |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1952 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1953 |
^ self |
8874 | 1954 |
sameContentsAs:aCollection |
1955 |
whenComparedWith:[:a :b | a == b] |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1956 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1957 |
"/ |index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1958 |
"/ stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1959 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1960 |
"/ (aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1961 |
"/ (aCollection size == self size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1962 |
"/ (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1963 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1964 |
"/ stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1965 |
"/ stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1966 |
"/ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1967 |
"/ index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1968 |
"/ [index <= stop] whileTrue:[ |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1969 |
"/ (self at:index) == (aCollection at:index) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1970 |
"/ index := index + 1 |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1971 |
"/ ]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
1972 |
"/ ^ true |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1973 |
|
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1974 |
" |
8874 | 1975 |
#(1 2 3 4 5) = #(1 2 3 4 5) |
1976 |
#(1 2 3 4 5) = #(1.0 2 3 4.0 5) |
|
1977 |
#($1 $2 $3 $4 $5) = '12345' |
|
1978 |
||
1979 |
#(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5) |
|
1980 |
#(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5) |
|
1981 |
#($1 $2 $3 $4 $5) identicalContentsAs: '12345' |
|
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1982 |
" |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1983 |
|
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
1984 |
"Modified: / 31.10.2001 / 11:30:18 / cg" |
5275
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1985 |
! |
87127c0e3167
added identity-element compare: #identicalContentsAs:
Claus Gittinger <cg@exept.de>
parents:
5231
diff
changeset
|
1986 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1987 |
isSameSequenceAs:otherCollection |
8874 | 1988 |
"Answer whether the receiver's size is the same as otherCollection's size, |
6118 | 1989 |
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
|
1990 |
This should probably replace the current definition of #= ." |
6118 | 1991 |
|
1992 |
| size | |
|
1993 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1994 |
(size := self size) = otherCollection size ifFalse: [^ false]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1995 |
otherCollection isSequenceable ifFalse: [^ false]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1996 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1997 |
1 to:size do:[:index | |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
1998 |
(self at: index) = (otherCollection at: index) ifFalse: [^ false] |
6118 | 1999 |
]. |
2000 |
^ true |
|
2001 |
! |
|
2002 |
||
17780 | 2003 |
sameContentsAs:aCollection |
2004 |
"return true, if the receiver and the arg have the same contents. |
|
2005 |
||
2006 |
Redefinded, so that SequenceableCollections are equivalent, especially OrderedCollections with |
|
2007 |
unused space" |
|
2008 |
||
2009 |
|index "{ Class: SmallInteger }" |
|
2010 |
stop "{ Class: SmallInteger }" | |
|
2011 |
||
2012 |
(aCollection == self) ifTrue:[^true]. |
|
2013 |
(aCollection isSequenceable) ifFalse:[ |
|
2014 |
aCollection with:self do:[:e1 :e2 | |
|
2015 |
(e1 = e2) ifFalse:[^false]. |
|
2016 |
]. |
|
2017 |
^ true |
|
2018 |
]. |
|
2019 |
||
2020 |
stop := self size. |
|
2021 |
stop == (aCollection size) ifFalse:[^false]. |
|
2022 |
||
2023 |
index := 1. |
|
2024 |
[index <= stop] whileTrue:[ |
|
2025 |
((self at:index) = (aCollection at:index)) ifFalse:[^false]. |
|
2026 |
index := index + 1 |
|
2027 |
]. |
|
2028 |
^ true |
|
2029 |
||
2030 |
" |
|
2031 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) copy |
|
2032 |
#($1 $2 $3 $4 $5) sameContentsAs: #(1 2 3 4 5) |
|
2033 |
#($1 $2 $3 $4 $5) sameContentsAs: '12345' |
|
2034 |
#($1 $2 $3 $4 $5) sameContentsAs: '54321' asSortedCollection |
|
2035 |
" |
|
2036 |
||
2037 |
"Created: / 09-07-2010 / 12:44:28 / sr" |
|
2038 |
! |
|
2039 |
||
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2040 |
sameContentsAs:aCollection whenComparedWith:compareBlock |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2041 |
"return true if the receiver and aCollection represent collections |
8874 | 2042 |
with the same contents, using compareSelector to compare elements. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2043 |
This is a generic version of #= or #identicalContentsAs:; |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2044 |
i.e. #= is the same as #sameContentsAs:whenComparedUsing:#= |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2045 |
and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==. |
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 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2048 |
|index "{ Class: SmallInteger }" |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2049 |
stop "{ Class: SmallInteger }" | |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2050 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2051 |
(aCollection == self) ifTrue:[^ true]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2052 |
(aCollection size == self size) ifFalse:[^ false]. |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2053 |
(aCollection isSequenceable) ifFalse:[ |
8874 | 2054 |
^ aCollection sameContentsAs:self whenComparedWith:compareBlock |
6130
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2055 |
]. |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2056 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2057 |
stop := self size. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2058 |
stop == (aCollection size) ifFalse:[^ false]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2059 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2060 |
index := 1. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2061 |
[index <= stop] whileTrue:[ |
8874 | 2062 |
(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false]. |
2063 |
index := index + 1 |
|
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2064 |
]. |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2065 |
^ true |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2066 |
|
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2067 |
" |
8874 | 2068 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a = b] |
2069 |
#(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) whenComparedWith:[:a :b | a == b] |
|
2070 |
#(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b] |
|
2071 |
#(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
|
2072 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2073 |
#('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
|
2074 |
" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2075 |
|
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2076 |
"Created: / 31.10.2001 / 11:29:38 / cg" |
645bc4c27f3d
sameContentsAs:whenComparedUsing: -> sameContentsAs:whenComparedWith:
Claus Gittinger <cg@exept.de>
parents:
6129
diff
changeset
|
2077 |
"Modified: / 31.10.2001 / 11:30:32 / cg" |
6129
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2078 |
! |
339ac45a10de
+ sameContentsAs:whenComparedUsing:
Claus Gittinger <cg@exept.de>
parents:
6126
diff
changeset
|
2079 |
|
602 | 2080 |
startsWith:aCollection |
2081 |
"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
|
2082 |
of aCollection |
3655a0fea430
definite (true) return for zero-length arg in #startsWith:
Claus Gittinger <cg@exept.de>
parents:
4382
diff
changeset
|
2083 |
If the argument is empty, true is returned." |
602 | 2084 |
|
2085 |
|index "{ Class: SmallInteger }" |
|
2086 |
stop "{ Class: SmallInteger }" | |
|
2087 |
||
2088 |
(aCollection == self) ifTrue:[^true]. |
|
2089 |
(aCollection isSequenceable) ifFalse:[^false]. |
|
2090 |
||
2091 |
stop := aCollection size. |
|
2092 |
stop > self size ifTrue:[^false]. |
|
2093 |
||
2094 |
index := 1. |
|
2095 |
[index <= stop] whileTrue:[ |
|
8874 | 2096 |
(self at:index) = (aCollection at:index) ifFalse:[^false]. |
2097 |
index := index + 1 |
|
602 | 2098 |
]. |
2099 |
^ true |
|
2100 |
||
2101 |
" |
|
2102 |
'abcde' startsWith:#($a $b $c) |
|
2103 |
#[1 2 3 4] startsWith:#(1 2 3) |
|
2104 |
#(1 2 3 4) asOrderedCollection startsWith:#(1 2 3) |
|
8874 | 2105 |
#(1 2 3 4) asOrderedCollection startsWith:#() |
602 | 2106 |
" |
7279 | 2107 |
! |
2108 |
||
2109 |
startsWithAnyOf:aCollectionOfCollections |
|
2110 |
"return true, if the receiver starts with any in aCollection" |
|
2111 |
||
2112 |
^ aCollectionOfCollections contains:[:eachTriedEnd | self startsWith:eachTriedEnd] |
|
2113 |
||
2114 |
" |
|
2115 |
'abcde' startsWithAnyOf:#('AB' 'ab') |
|
2116 |
'abcde' startsWithAnyOf:#('AC' 'ac') |
|
8874 | 2117 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 4)) |
2118 |
#[1 2 3 4] startsWithAnyOf:#( #(1 3) #(1 2)) |
|
7279 | 2119 |
" |
17910 | 2120 |
! |
2121 |
||
2122 |
with:aCollection findFirst:aBlock |
|
2123 |
"return the index of the first element for which aTwoArgBlock returns true |
|
2124 |
against corresponding elements of aCollection. |
|
2125 |
If any collection is smaller, the size of the smaller plus one is returned" |
|
2126 |
||
2127 |
|mySize otherSize minSize| |
|
2128 |
||
2129 |
mySize := self size. |
|
2130 |
otherSize := aCollection size. |
|
2131 |
minSize := mySize min:otherSize. |
|
2132 |
||
2133 |
self from:1 to:minSize with:aCollection doWithIndex:[:el1 :el2 :i | |
|
2134 |
(aBlock value:el1 value:el2) ifTrue:[ ^ i]. |
|
2135 |
]. |
|
2136 |
||
2137 |
mySize ~= otherSize ifTrue:[ |
|
2138 |
^ minSize + 1 |
|
2139 |
]. |
|
2140 |
^ 0 |
|
2141 |
||
2142 |
" |
|
2143 |
'hello' with:'helLo' findFirst:[:a :b | a ~= b] |
|
2144 |
'hello' with:'hello' findFirst:[:a :b | a ~= b] |
|
2145 |
'hello' with:'hello1' findFirst:[:a :b | a ~= b] |
|
2146 |
'hello1' with:'hello' findFirst:[:a :b | a ~= b] |
|
2147 |
" |
|
2148 |
||
2149 |
"Created: / 08-01-2012 / 17:12:00 / cg" |
|
13 | 2150 |
! ! |
2151 |
||
260 | 2152 |
!SequenceableCollection methodsFor:'converting'! |
2153 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2154 |
asCollectionOfSubCollectionsOfSize:pieceSize |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2155 |
"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
|
2156 |
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
|
2157 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2158 |
|pieces |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2159 |
start "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2160 |
stop "{ Class:SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2161 |
mySize "{ Class:SmallInteger }"| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2162 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2163 |
pieces := OrderedCollection new. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2164 |
start := 1. stop := start + pieceSize - 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2165 |
mySize := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2166 |
[stop <= mySize] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2167 |
pieces add:(self copyFrom:start to:stop). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2168 |
start := start + pieceSize. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2169 |
stop := stop + pieceSize. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2170 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2171 |
(start <= mySize) ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2172 |
pieces add:(self copyFrom:start to:mySize). |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2173 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2174 |
^ pieces |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2175 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2176 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2177 |
'123123123123123123' asCollectionOfSubCollectionsOfSize:3 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2178 |
'12312312312312312312' asCollectionOfSubCollectionsOfSize:3 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2179 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2180 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
2181 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2182 |
asCollectionOfSubCollectionsSeparatedBy:anElement |
8874 | 2183 |
"return a collection containing the subcollections (separated by anElement) |
2184 |
of the receiver. If anElement occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2185 |
the result will contain empty collections. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2186 |
This algorithm uses equality-compare to detect the element." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2187 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2188 |
|cols myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2189 |
numberOfCols "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2190 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2191 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2192 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2193 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2194 |
count first, to avoid regrowing of the OC |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2195 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2196 |
numberOfCols := (self occurrencesOf:anElement) + 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2197 |
cols := OrderedCollection new:numberOfCols. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2198 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2199 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2200 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2201 |
1 to:numberOfCols do:[:lineNr | |
8874 | 2202 |
stopIndex := self indexOf:anElement startingAt:startIndex. |
2203 |
stopIndex == 0 ifTrue:[ |
|
2204 |
stopIndex := self size |
|
2205 |
] ifFalse: [ |
|
2206 |
stopIndex := stopIndex - 1. |
|
2207 |
]. |
|
2208 |
||
2209 |
(stopIndex < startIndex) ifTrue: [ |
|
2210 |
cols add:(myClass new:0) |
|
2211 |
] ifFalse: [ |
|
2212 |
cols add:(self copyFrom:startIndex to:stopIndex) |
|
2213 |
]. |
|
2214 |
startIndex := stopIndex + 2 |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2215 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2216 |
^ cols |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2217 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2218 |
" |
8874 | 2219 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubCollectionsSeparatedBy:$: |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2220 |
'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
|
2221 |
#(a b c d e f g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2222 |
#(a b c d e f d d g h) asCollectionOfSubCollectionsSeparatedBy: #d. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2223 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2224 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2225 |
|
9234 | 2226 |
asCollectionOfSubCollectionsSeparatedBy:anElement do:aBlock |
2227 |
"evaluate aBlock for each subcollection generated by separating elements |
|
2228 |
of the receiver by anElement. |
|
2229 |
If anElement occurs multiple times in a row, |
|
2230 |
the block will be invoked with empty collections as argument. |
|
2231 |
This algorithm uses equality-compare to detect the element." |
|
2232 |
||
2233 |
|subCollection |
|
2234 |
endIndex "{ Class:SmallInteger }" |
|
2235 |
startIndex "{ Class:SmallInteger }" |
|
2236 |
stopIndex "{ Class:SmallInteger }" | |
|
2237 |
||
2238 |
startIndex := 0. |
|
2239 |
endIndex := self size. |
|
2240 |
||
2241 |
[startIndex <= endIndex] whileTrue:[ |
|
2242 |
stopIndex := self indexOf:anElement startingAt:startIndex+1. |
|
2243 |
stopIndex == 0 ifTrue:[ |
|
2244 |
stopIndex := self size |
|
2245 |
] ifFalse: [ |
|
2246 |
stopIndex := stopIndex - 1. |
|
2247 |
]. |
|
2248 |
||
2249 |
(stopIndex < startIndex) ifTrue: [ |
|
2250 |
subCollection := self species new:0 |
|
2251 |
] ifFalse: [ |
|
2252 |
subCollection := self copyFrom:startIndex+1 to:stopIndex |
|
2253 |
]. |
|
2254 |
aBlock value:subCollection. |
|
2255 |
startIndex := stopIndex + 1 |
|
2256 |
]. |
|
2257 |
||
2258 |
" |
|
2259 |
'' |
|
2260 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2261 |
||
2262 |
'1 one' |
|
2263 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2264 |
||
2265 |
'1 one:2 two:3 three:4 four:5 five' |
|
2266 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2267 |
||
2268 |
'a::b' |
|
2269 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2270 |
||
2271 |
':' |
|
2272 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2273 |
||
2274 |
':a' |
|
2275 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2276 |
||
2277 |
'a:' |
|
2278 |
asCollectionOfSubCollectionsSeparatedBy:$: do:[:each | Transcript showCR:each storeString] |
|
2279 |
" |
|
2280 |
! |
|
2281 |
||
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2282 |
asCollectionOfSubCollectionsSeparatedByAll:aSeparatorCollection |
8874 | 2283 |
"return a collection containing the subcollections (separated by aSeparatorCollection) |
2284 |
of the receiver. If aSeparatorCollection occurs multiple times in a row, |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2285 |
the result will contain empty strings. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2286 |
Uses equality-compare when searching for aSeparatorCollection." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2287 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2288 |
|items done myClass |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2289 |
startIndex "{ Class:SmallInteger }" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2290 |
stopIndex "{ Class:SmallInteger }" | |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2291 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2292 |
items := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2293 |
myClass := self species. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2294 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2295 |
startIndex := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2296 |
done := false. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2297 |
[done] whileFalse:[ |
8874 | 2298 |
stopIndex := self indexOfSubCollection:aSeparatorCollection startingAt:startIndex. |
2299 |
stopIndex == 0 ifTrue:[ |
|
2300 |
stopIndex := self size. |
|
2301 |
done := true. |
|
2302 |
] ifFalse: [ |
|
2303 |
stopIndex := stopIndex - 1. |
|
2304 |
]. |
|
2305 |
||
2306 |
(stopIndex < startIndex) ifTrue: [ |
|
2307 |
items add:(myClass new:0) |
|
2308 |
] ifFalse: [ |
|
2309 |
items add:(self copyFrom:startIndex to:stopIndex) |
|
2310 |
]. |
|
2311 |
startIndex := stopIndex + (aSeparatorCollection size) + 1. |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2312 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2313 |
^ items |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2314 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2315 |
" |
8874 | 2316 |
'1::2::3::4::5::' asCollectionOfSubCollectionsSeparatedByAll:'::' |
2317 |
#(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
|
2318 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2319 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2320 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2321 |
asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
8874 | 2322 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2323 |
(separated by any from aCollectionOfSeparators) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2324 |
This allows breaking up strings using a number of elements as separator. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2325 |
Uses equality-compare when searching for separators." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2326 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2327 |
^ self asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:el | aCollectionOfSeparators includes:el] |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2328 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2329 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2330 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:#($:) |
8874 | 2331 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:':' |
2332 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:(Array with:$: with:Character space) |
|
2333 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAny:': ' |
|
2334 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAny:($1 to: $9) |
|
2335 |
#(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
|
2336 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2337 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2338 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2339 |
asCollectionOfSubCollectionsSeparatedByAnyForWhich:aBlock |
8874 | 2340 |
"return a collection containing the subCollection |
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2341 |
(separated by elements for which aBlock evaluates to true) of the receiver. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2342 |
This allows breaking up strings using an arbitrary condition." |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2343 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2344 |
|words |
8874 | 2345 |
start "{ Class:SmallInteger }" |
2346 |
stop "{ Class:SmallInteger }" |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2347 |
mySize "{ Class:SmallInteger }"| |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2348 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2349 |
words := OrderedCollection new. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2350 |
start := 1. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2351 |
mySize := self size. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2352 |
[start <= mySize] whileTrue:[ |
8874 | 2353 |
"skip multiple separators" |
2354 |
[ aBlock value:(self at:start)] whileTrue:[ |
|
2355 |
start := start + 1 . |
|
2356 |
start > mySize ifTrue:[ |
|
2357 |
^ words |
|
2358 |
]. |
|
2359 |
]. |
|
2360 |
||
2361 |
stop := self findFirst:aBlock startingAt:start. |
|
2362 |
stop == 0 ifTrue:[ |
|
2363 |
words add:(self copyFrom:start to:mySize). |
|
2364 |
^ words |
|
2365 |
]. |
|
2366 |
words add:(self copyFrom:start to:(stop - 1)). |
|
2367 |
start := stop |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2368 |
]. |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2369 |
^ words |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2370 |
|
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2371 |
" |
8874 | 2372 |
'hello:world:isnt:this nice' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch = $:] |
2373 |
'h1e2l3l4o' asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:ch | ch isDigit] |
|
8851
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2374 |
#(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
|
2375 |
" |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2376 |
! |
3910c013402b
+asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8831
diff
changeset
|
2377 |
|
5607
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2378 |
asSequenceableCollection |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2379 |
"return myself as a SequenceableCollection. |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2380 |
I am already a SequenceableCollection." |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2381 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2382 |
^ self |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2383 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2384 |
|
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2385 |
! |
94c690ba0dea
Define #asCollection, #asSequenceableCollection
Stefan Vogel <sv@exept.de>
parents:
5605
diff
changeset
|
2386 |
|
1413 | 2387 |
asStringCollection |
2388 |
"return a new string collection containing the elements; |
|
2389 |
these ought to be strings. (i.e. String or Text instances)" |
|
2390 |
||
2391 |
|newColl sz| |
|
2392 |
||
2393 |
sz := self size. |
|
9162 | 2394 |
newColl := StringCollection withSize:sz. |
1413 | 2395 |
newColl replaceFrom:1 to:sz with:self startingAt:1. |
2396 |
^ newColl |
|
2397 |
||
2398 |
"Created: 18.5.1996 / 13:53:55 / cg" |
|
2399 |
"Modified: 18.5.1996 / 14:00:16 / cg" |
|
2400 |
! |
|
2401 |
||
1188 | 2402 |
asStringWith:sepChar |
8874 | 2403 |
"return a string generated by concatenating my elements |
1188 | 2404 |
(which must be strings or nil) and embedding sepChar characters in between. |
2405 |
Nil entries and empty strings are counted as empty lines." |
|
2406 |
||
8874 | 2407 |
^ self |
17780 | 2408 |
from:1 to:(self size) |
2409 |
asStringWith:sepChar |
|
2410 |
compressTabs:false |
|
2411 |
final:nil |
|
1413 | 2412 |
|
2413 |
" |
|
2414 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:$; |
|
17780 | 2415 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWith:'|' |
2416 |
'hello|world|foo|bar|baz' asCollectionOfSubstringsSeparatedBy:$| |
|
2417 |
" |
|
2418 |
||
2419 |
"Modified: / 10-07-2010 / 22:59:29 / cg" |
|
1188 | 2420 |
! |
2421 |
||
2422 |
asStringWith:sepCharacter from:firstLine to:lastLine |
|
2423 |
"return part of myself as a string with embedded sepCharacters. |
|
2424 |
My elements must be strings or nil; nil entries and empty strings are |
|
2425 |
taken as empty lines." |
|
2426 |
||
8874 | 2427 |
^ self |
2428 |
from:firstLine to:lastLine |
|
2429 |
asStringWith:sepCharacter |
|
2430 |
compressTabs:false |
|
2431 |
final:nil |
|
1188 | 2432 |
" |
2433 |
creating entries for searchpath: |
|
2434 |
||
8874 | 2435 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2436 |
||
2437 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$: from:1 to:3 |
|
1188 | 2438 |
" |
2439 |
||
2440 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2441 |
! |
|
2442 |
||
260 | 2443 |
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
|
2444 |
"return part of myself as a string or text with embedded sepCharacters. |
263 | 2445 |
My elements must be strings or nil; nil entries and empty strings are |
260 | 2446 |
taken as empty lines. |
2447 |
If the argument compressTabs is true, leading spaces are converted |
|
2448 |
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
|
2449 |
character (if non-nil)." |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2450 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2451 |
^ self |
8874 | 2452 |
from:firstLine to:lastLine |
2453 |
asStringWith:sepCharacter |
|
2454 |
compressTabs:compressTabs |
|
2455 |
final:endCharacter |
|
2456 |
withEmphasis:true |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2457 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2458 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2459 |
! |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2460 |
|
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2461 |
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
|
2462 |
"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
|
2463 |
and followup endCharacter. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2464 |
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
|
2465 |
taken as empty lines. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2466 |
sepCharacter and endCharacter may be nil, a character or a string. |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2467 |
If the argument compressTabs is true, leading spaces are converted |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2468 |
to tab-characters (8col tabs). The last line is followed by a final |
260 | 2469 |
character (if non-nil). |
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2470 |
The withEmphais argument controls if the returned string should preserve |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2471 |
any emphasis. If false, a plain string is returned. |
260 | 2472 |
This method is tuned for big collections, in not creating many |
263 | 2473 |
intermediate strings (has linear runtime). For very small collections |
2474 |
and small strings, it may be faster to use the comma , operation." |
|
260 | 2475 |
|
5575 | 2476 |
^ self |
8874 | 2477 |
from:firstLine to:lastLine |
2478 |
asStringWith:sepCharacter |
|
2479 |
compressTabs:compressTabs |
|
2480 |
final:endCharacter |
|
2481 |
withEmphasis:withEmphasis |
|
5575 | 2482 |
! |
2483 |
||
2484 |
asStringWithCRs |
|
8874 | 2485 |
"return a string generated by concatenating my elements |
5575 | 2486 |
(which must be strings or nil) and embedding cr characters in between. |
2487 |
Nil entries and empty strings are counted as empty lines." |
|
2488 |
||
2489 |
^ self asStringWithCRsFrom:1 to:(self size) |
|
2490 |
||
2491 |
" |
|
8874 | 2492 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRs |
5575 | 2493 |
|
2494 |
(OrderedCollection new |
|
8874 | 2495 |
add:'hello'; |
2496 |
add:'world'; |
|
2497 |
add:'foo'; |
|
2498 |
add:('bar' asText allBold); |
|
2499 |
yourself) asStringWithCRs |
|
5575 | 2500 |
|
2501 |
Transcript showCR: |
|
8874 | 2502 |
(OrderedCollection new |
2503 |
add:'hello'; |
|
2504 |
add:'world'; |
|
2505 |
add:'foo'; |
|
2506 |
add:('bar' asText allBold); |
|
2507 |
yourself) asStringWithCRs |
|
5575 | 2508 |
" |
2509 |
||
2510 |
"Modified: 18.5.1996 / 16:43:47 / cg" |
|
2511 |
! |
|
2512 |
||
2513 |
asStringWithCRsFrom:firstLine to:lastLine |
|
8874 | 2514 |
"return a string generated by concatenating some of my elements |
5575 | 2515 |
(which must be strings or nil) and embedding cr characters in between. |
2516 |
Nil entries and empty strings are counted as empty lines." |
|
2517 |
||
2518 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:false withCR:true |
|
2519 |
||
2520 |
" |
|
8874 | 2521 |
#('hello' 'world' 'foo' 'bar' 'baz') asStringWithCRsFrom:2 to:4 |
5575 | 2522 |
|
2523 |
" |
|
2524 |
||
2525 |
"Modified: 18.5.1996 / 16:50:55 / cg" |
|
2526 |
! |
|
2527 |
||
2528 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs |
|
2529 |
"return part of myself as a string with embedded cr's. |
|
2530 |
My elements must be strings or nil. |
|
2531 |
If the argument compressTabs is true, leading spaces are converted |
|
2532 |
to tab-characters (8col tabs). |
|
2533 |
Nil entries and empty strings are taken as empty lines." |
|
2534 |
||
2535 |
^ self asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:true |
|
2536 |
! |
|
2537 |
||
2538 |
asStringWithCRsFrom:firstLine to:lastLine compressTabs:compressTabs withCR:withCR |
|
2539 |
"return part of myself as a string with embedded cr's. |
|
2540 |
My elements must be strings or nil; nil entries and empty strings are |
|
2541 |
taken as empty lines. |
|
2542 |
If the argument compressTabs is true, leading spaces are converted |
|
2543 |
to tab-characters (8col tabs). WithCR controls whether the last line |
|
2544 |
should be followed by a cr or not." |
|
2545 |
||
2546 |
^ self asStringWith:(Character cr) |
|
8874 | 2547 |
from:firstLine |
2548 |
to:lastLine |
|
2549 |
compressTabs:compressTabs |
|
2550 |
final:(withCR ifTrue:[Character cr] ifFalse:[nil]) |
|
5575 | 2551 |
! |
2552 |
||
2553 |
decodeAsLiteralArray |
|
2554 |
"given a literalEncoding in the receiver, |
|
2555 |
create & return the corresponding object. |
|
2556 |
The inverse operation to #literalArrayEncoding." |
|
2557 |
||
9414 | 2558 |
|clsName cls| |
5575 | 2559 |
|
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2560 |
self size == 0 ifTrue:[ |
9358 | 2561 |
^ self. |
8799
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2562 |
]. |
4f8e9a7cb68b
decodeAsLiteralArray: allow empty array
Claus Gittinger <cg@exept.de>
parents:
8797
diff
changeset
|
2563 |
|
9414 | 2564 |
clsName := self at:1. |
5850
6eac4271bf65
error handling when literalArraySpec is bad.
Claus Gittinger <cg@exept.de>
parents:
5817
diff
changeset
|
2565 |
clsName isSymbol ifFalse:[ |
9358 | 2566 |
^ MissingClassInLiteralArrayErrorSignal |
2567 |
raiseRequestWith:clsName |
|
9414 | 2568 |
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
|
2569 |
]. |
9414 | 2570 |
cls := Smalltalk classNamed:clsName. |
5575 | 2571 |
cls isNil ifTrue:[ |
9414 | 2572 |
^ MissingClassInLiteralArrayErrorSignal |
2573 |
raiseRequestWith:clsName |
|
2574 |
errorString:('unknown class in literalArray-spec: ' , clsName) |
|
5575 | 2575 |
]. |
9414 | 2576 |
|
5575 | 2577 |
^ cls decodeFromLiteralArray:self. |
2578 |
||
2579 |
" |
|
17966 | 2580 |
#(10 20) literalArrayEncoding |
10476 | 2581 |
#(Array 10 20) decodeAsLiteralArray |
17966 | 2582 |
#() literalArrayEncoding |
2583 |
#(Array) decodeAsLiteralArray |
|
10476 | 2584 |
" |
2585 |
||
2586 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
|
5575 | 2587 |
! |
2588 |
||
10818
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2589 |
decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2590 |
"given an array of literalEncodings in the receiver, |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2591 |
create & return the corresponding collection object." |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2592 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2593 |
^ self collect:[:each | each decodeAsLiteralArray] |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2594 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2595 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2596 |
#( |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2597 |
#(Point 10 20) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2598 |
#(Point 20 30) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2599 |
#(Point 30 40) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2600 |
#(Point 40 50) |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2601 |
) decodeAsLiteralArrayCollection |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2602 |
" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2603 |
|
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2604 |
"Modified: / 26-03-2007 / 13:57:10 / cg" |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2605 |
! |
cbbb7124b553
+decodeAsLiteralArrayCollection
Claus Gittinger <cg@exept.de>
parents:
10812
diff
changeset
|
2606 |
|
8874 | 2607 |
from:firstLine to:lastLine asStringWith:sepCharacter |
5575 | 2608 |
"return part of myself as a string with embedded sepCharacters. |
2609 |
My elements must be strings or nil; nil entries and empty strings are |
|
2610 |
taken as empty lines." |
|
2611 |
||
8874 | 2612 |
^ self |
2613 |
from:firstLine |
|
2614 |
to:lastLine |
|
2615 |
asStringWith:sepCharacter |
|
2616 |
compressTabs:false |
|
2617 |
final:nil |
|
5575 | 2618 |
" |
2619 |
creating entries for searchpath: |
|
2620 |
||
8874 | 2621 |
#('foo' 'bar' 'baz' '/foo/bar') asStringWith:$; |
2622 |
||
2623 |
#('foo' 'bar' 'baz' '/foo/bar') from:1 to:3 asStringWith:$: |
|
2624 |
||
2625 |
(#('foo' 'bar' 'baz' '/foo/bar') copyFrom:1 to:3) asStringWith:$: |
|
5575 | 2626 |
" |
2627 |
||
2628 |
"Modified: 23.2.1996 / 15:28:55 / cg" |
|
2629 |
! |
|
2630 |
||
2631 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter |
|
2632 |
"return part of myself as a string or text with embedded sepCharacters. |
|
2633 |
My elements must be strings or nil; nil entries and empty strings are |
|
2634 |
taken as empty lines. |
|
2635 |
If the argument compressTabs is true, leading spaces are converted |
|
2636 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2637 |
character (if non-nil)." |
|
2638 |
||
2639 |
^ self |
|
8874 | 2640 |
from:firstLine |
2641 |
to:lastLine |
|
2642 |
asStringWith:sepCharacter |
|
2643 |
compressTabs:compressTabs |
|
2644 |
final:endCharacter |
|
2645 |
withEmphasis:true |
|
5575 | 2646 |
|
2647 |
"Modified: / 17.6.1998 / 12:31:19 / cg" |
|
2648 |
! |
|
2649 |
||
2650 |
from:firstLine to:lastLine asStringWith:sepCharacter compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis |
|
2651 |
"return part of myself as a string or text with embedded sepCharacters |
|
2652 |
and followup endCharacter. |
|
2653 |
My elements must be strings or nil; nil entries and empty strings are |
|
2654 |
taken as empty lines. |
|
2655 |
sepCharacter and endCharacter may be nil, a character or a string. |
|
2656 |
If the argument compressTabs is true, leading spaces are converted |
|
2657 |
to tab-characters (8col tabs). The last line is followed by a final |
|
2658 |
character (if non-nil). |
|
2659 |
The withEmphais argument controls if the returned string should preserve |
|
2660 |
any emphasis. If false, a plain string is returned. |
|
2661 |
This method is tuned for big collections, in not creating many |
|
2662 |
intermediate strings (has linear runtime). For very small collections |
|
2663 |
and small strings, it may be faster to use the comma , operation." |
|
2664 |
||
293 | 2665 |
|idx1 "{ Class:SmallInteger }" |
2666 |
idx2 "{ Class:SmallInteger }" |
|
2667 |
totalLength "{ Class:SmallInteger }" |
|
260 | 2668 |
pos "{ Class:SmallInteger }" |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2669 |
sepCnt "{ Class:SmallInteger }" |
8874 | 2670 |
newString lineString spaces idx nTabs |
8099 | 2671 |
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
|
2672 |
thisLen anyTab| |
260 | 2673 |
|
2674 |
" |
|
2675 |
first accumulate the size of the string, to avoid |
|
2676 |
countless reallocations. If tabs are compressed, |
|
2677 |
the size computed is not exact, but gives an upper bound ... |
|
1413 | 2678 |
On the fly, look if a 16bit string or emphasized text is needed. |
260 | 2679 |
" |
8099 | 2680 |
needEmphasis := false. |
2681 |
maxBitsPerCharacter := 8. |
|
1015 | 2682 |
|
260 | 2683 |
totalLength := 0. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2684 |
sepCharacter isNil ifTrue:[ |
8874 | 2685 |
sepCnt := 0 |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2686 |
] ifFalse:[ |
8874 | 2687 |
sepCharacter isCharacter ifTrue:[ |
2688 |
sepCnt := 1 |
|
2689 |
] ifFalse:[ |
|
2690 |
sepCnt := sepCharacter size |
|
2691 |
] |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2692 |
]. |
260 | 2693 |
|
293 | 2694 |
idx1 := firstLine. |
2695 |
idx2 := lastLine. |
|
2696 |
idx1 to:idx2 do:[:lineIndex | |
|
8874 | 2697 |
lineString := self at:lineIndex. |
2698 |
||
2699 |
lineString notNil ifTrue:[ |
|
2700 |
withEmphasis ifTrue:[ |
|
2701 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2702 |
needEmphasis := true |
|
2703 |
]. |
|
2704 |
]. |
|
2705 |
maxBitsPerCharacter := maxBitsPerCharacter max:(lineString bitsPerCharacter). |
|
2706 |
totalLength := totalLength + lineString size |
|
2707 |
]. |
|
2708 |
totalLength := totalLength + sepCnt |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2709 |
]. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2710 |
totalLength := totalLength - sepCnt. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2711 |
|
8099 | 2712 |
maxBitsPerCharacter > 8 ifTrue:[ |
8874 | 2713 |
maxBitsPerCharacter > 16 ifTrue:[ |
2714 |
stringClass := Unicode32String. |
|
2715 |
] ifFalse:[ |
|
2716 |
stringClass := Unicode16String. |
|
2717 |
] |
|
8099 | 2718 |
] ifFalse:[ |
8874 | 2719 |
stringClass := String. |
8099 | 2720 |
]. |
2721 |
||
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2722 |
endCharacter notNil ifTrue:[ |
8874 | 2723 |
endCharacter isCharacter ifTrue:[ |
2724 |
totalLength := totalLength + 1 |
|
2725 |
] ifFalse:[ |
|
2726 |
totalLength := totalLength + endCharacter size |
|
2727 |
]. |
|
260 | 2728 |
]. |
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2729 |
totalLength <= 0 ifTrue:[^ '']. |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2730 |
|
260 | 2731 |
spaces := ' '. |
1016 | 2732 |
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
|
2733 |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2734 |
needEmphasis ifTrue:[ |
8874 | 2735 |
newRuns := RunArray new. |
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2736 |
]. |
260 | 2737 |
|
2738 |
" |
|
2739 |
now, replace ... |
|
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2740 |
Be careful with runArrays: |
8874 | 2741 |
replacing individual elements is VERY expensive. |
2742 |
Therefore, create a new runArray from scratch. |
|
260 | 2743 |
" |
2744 |
pos := 1. |
|
293 | 2745 |
idx1 to:idx2 do:[:lineIndex | |
8874 | 2746 |
lineString := self at:lineIndex. |
2747 |
thisLen := lineString size. |
|
2748 |
thisLen ~~ 0 ifTrue:[ |
|
2749 |
withEmphasis ifFalse:[ |
|
2750 |
lineString := lineString string. |
|
2751 |
]. |
|
2752 |
||
2753 |
(anyTab := compressTabs) ifTrue:[ |
|
2754 |
" |
|
2755 |
mhmh: could use withTabs from String-class here, |
|
2756 |
but we should avoid creating too many temporary strings |
|
2757 |
(especially, since this method is typically used when converting |
|
2758 |
big texts such as when saving in the filebrowser ...). |
|
2759 |
Therefore, we convert tabs inline here doing a direct replace |
|
2760 |
in newString." |
|
2761 |
||
2762 |
idx := lineString findFirst:[:c | (c ~~ Character space)]. |
|
2763 |
nTabs := (idx-1) // 8. |
|
2764 |
anyTab := (nTabs > 0) |
|
2765 |
]. |
|
2766 |
anyTab ifTrue:[ |
|
2767 |
"any tabs" |
|
2768 |
||
2769 |
idx := nTabs * 8 + 1. "/ index of first copied character in string |
|
2770 |
||
2771 |
newString atAll:(pos to:pos+nTabs-1) put:(Character tab). |
|
2772 |
newRuns notNil ifTrue:[ |
|
2773 |
newRuns add:nil withOccurrences:nTabs |
|
2774 |
]. |
|
2775 |
pos := pos + nTabs. |
|
2776 |
||
2777 |
newString replaceFrom:pos with:lineString startingAt:idx. |
|
2778 |
newRuns notNil ifTrue:[ |
|
2779 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2780 |
idx to:lineString size do:[:pos | |
|
2781 |
newRuns add:(lineString emphasisAt:pos) |
|
2782 |
] |
|
2783 |
] ifFalse:[ |
|
2784 |
newRuns add:nil withOccurrences:(lineString size - idx + 1) |
|
2785 |
] |
|
2786 |
]. |
|
2787 |
pos := pos + thisLen - (nTabs * 8). |
|
2788 |
] ifFalse:[ |
|
2789 |
newString replaceFrom:pos with:lineString. |
|
2790 |
||
2791 |
newRuns notNil ifTrue:[ |
|
2792 |
lineString hasChangeOfEmphasis ifTrue:[ |
|
2793 |
newRuns addAll:(lineString emphasis) |
|
2794 |
] ifFalse:[ |
|
2795 |
newRuns add:nil withOccurrences:lineString size |
|
2796 |
] |
|
2797 |
]. |
|
2798 |
pos := pos + thisLen. |
|
2799 |
]. |
|
2800 |
]. |
|
2801 |
||
2802 |
lineIndex ~~ lastLine ifTrue:[ |
|
2803 |
c := sepCharacter |
|
2804 |
] ifFalse:[ |
|
2805 |
c := endCharacter |
|
2806 |
]. |
|
2807 |
||
2808 |
c notNil ifTrue:[ |
|
2809 |
c isCharacter ifTrue:[ |
|
2810 |
newString at:pos put:c. |
|
2811 |
newRuns notNil ifTrue:[ |
|
2812 |
newRuns add:nil. |
|
2813 |
]. |
|
2814 |
pos := pos + 1 |
|
2815 |
] ifFalse:[ |
|
2816 |
newString replaceFrom:pos with:c. |
|
2817 |
newRuns notNil ifTrue:[ |
|
2818 |
newRuns add:nil withOccurrences:c size |
|
2819 |
]. |
|
2820 |
pos := pos + c size |
|
2821 |
] |
|
2822 |
]. |
|
260 | 2823 |
]. |
2824 |
||
2825 |
" |
|
2826 |
in case of tab compression, the result has to be |
|
2827 |
cut to size ... sorry |
|
2828 |
" |
|
2829 |
pos ~~ totalLength ifTrue:[ |
|
8874 | 2830 |
newString := newString copyTo:(pos - 1) |
260 | 2831 |
]. |
1536
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2832 |
|
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2833 |
newRuns notNil ifTrue:[ |
8874 | 2834 |
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
|
2835 |
]. |
29d45204ceed
tuned #asStringWith:... if any Text lines with emphasis are involved.
Claus Gittinger <cg@exept.de>
parents:
1495
diff
changeset
|
2836 |
|
260 | 2837 |
^ newString |
2838 |
||
2839 |
" |
|
2840 |
creating entries for searchpath: |
|
2841 |
||
8874 | 2842 |
#('foo' 'bar' 'baz' '/foo/bar') |
2843 |
asStringWith:$: |
|
2844 |
from:1 to:4 |
|
2845 |
compressTabs:false |
|
2846 |
final:nil |
|
260 | 2847 |
|
2848 |
with trailing colon: |
|
2849 |
||
8874 | 2850 |
#('foo' 'bar' 'baz' '/foo/bar') |
2851 |
asStringWith:$: |
|
2852 |
from:1 to:4 |
|
2853 |
compressTabs:false |
|
2854 |
final:$: |
|
260 | 2855 |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2856 |
concatenating elements (nil sepChars and nil endChars): |
260 | 2857 |
|
8874 | 2858 |
#('foo' 'bar' 'baz') |
2859 |
asStringWith:nil |
|
2860 |
from:1 to:3 |
|
2861 |
compressTabs:false |
|
2862 |
final:nil |
|
260 | 2863 |
|
2864 |
creating a string from a collection of lines: |
|
2865 |
||
8874 | 2866 |
#('foo' 'bar' 'baz') |
2867 |
asStringWith:(Character cr) |
|
2868 |
from:1 to:3 |
|
2869 |
compressTabs:false |
|
2870 |
final:(Character cr) |
|
1395
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2871 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2872 |
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
|
2873 |
|
f324be2221e3
asStringWith:... handles emphasized text
Claus Gittinger <cg@exept.de>
parents:
1385
diff
changeset
|
2874 |
(Array |
8874 | 2875 |
with:'foo' |
2876 |
with:('bar' asText allBold) |
|
2877 |
with:'baz' |
|
2878 |
with:('baz2' asText emphasizeAllWith:#italic) |
|
2879 |
) |
|
2880 |
asStringWith:(Character cr) |
|
2881 |
from:1 to:4 |
|
2882 |
compressTabs:false |
|
2883 |
final:(Character cr) |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2884 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2885 |
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
|
2886 |
|
8874 | 2887 |
#('foo' 'bar' 'baz') |
2888 |
asStringWith:'__' |
|
2889 |
from:1 to:3 |
|
2890 |
compressTabs:false |
|
2891 |
final:nil |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2892 |
|
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2893 |
and as final characters: |
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2894 |
|
8874 | 2895 |
#('foo' 'bar' 'baz') |
2896 |
asStringWith:'__' |
|
2897 |
from:1 to:3 |
|
2898 |
compressTabs:false |
|
2899 |
final:'***' |
|
4677
90a037f88b0e
allow strings as sepCharacter and endCharacter in #asStringWith:...
Claus Gittinger <cg@exept.de>
parents:
4459
diff
changeset
|
2900 |
|
260 | 2901 |
" |
1003
c82f2dcf5611
care for embedded 16bit strings when concatenating a stringCollection
Claus Gittinger <cg@exept.de>
parents:
929
diff
changeset
|
2902 |
|
3602
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2903 |
"Created: / 17.6.1998 / 12:30:32 / cg" |
dd53d372c250
added #asStringWithoutEmphasis...
Claus Gittinger <cg@exept.de>
parents:
3482
diff
changeset
|
2904 |
"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
|
2905 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2906 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2907 |
subCollections:aBlock |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2908 |
"Answser an ordered collection of ordered collections |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2909 |
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
|
2910 |
for which the given block evaluates to true." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2911 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2912 |
|str answer current e| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2913 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2914 |
str := self readStream. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2915 |
answer := OrderedCollection new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2916 |
current := OrderedCollection new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2917 |
[ str atEnd ] whileFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2918 |
e := str next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2919 |
current add:e. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2920 |
(aBlock value:e) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2921 |
answer add:current. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2922 |
current := OrderedCollection new |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2923 |
] |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2924 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2925 |
current notEmpty ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2926 |
answer add:current |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2927 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2928 |
^ answer |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2929 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2930 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
2931 |
#( 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
|
2932 |
" |
260 | 2933 |
! ! |
2934 |
||
7253 | 2935 |
!SequenceableCollection methodsFor:'converting-reindexed'! |
2936 |
||
8874 | 2937 |
from:startIndex |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2938 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2939 |
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
|
2940 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2941 |
however, physically, no copy is made). |
9291 | 2942 |
Warning: |
2943 |
The slice SHARES the memory for the element-data with the original, |
|
2944 |
this means that any modifications in the original are visible in the slice |
|
2945 |
and vice versa." |
|
7253 | 2946 |
|
2947 |
^ self |
|
9291 | 2948 |
from:startIndex |
2949 |
to:self size |
|
2950 |
by:1 |
|
8874 | 2951 |
|
2952 |
" |
|
2953 |
#(1 2 3 4 5 6 7) from:3 |
|
2954 |
( #(1 2 3 4 5 6 7) from:3 ) first |
|
2955 |
( #(1 2 3 4 5 6 7) from:3 ) last |
|
2956 |
( #(1 2 3 4 5 6 7) from:3 ) size |
|
2957 |
" |
|
2958 |
! |
|
2959 |
||
2960 |
from:startIndex by:step |
|
9291 | 2961 |
"Create a ReindexedCollection from the receiver. |
2962 |
The new collection represents the elements starting at startIndex to the end. |
|
2963 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
|
2964 |
however, physically, no copy is made). |
|
2965 |
Warning: |
|
2966 |
The slice SHARES the memory for the element-data with the original, |
|
2967 |
this means that any modifications in the original are visible in the slice |
|
2968 |
and vice versa." |
|
7253 | 2969 |
|
2970 |
^ self |
|
9291 | 2971 |
from:startIndex |
2972 |
to:(step > 1 |
|
2973 |
ifTrue: [self size] |
|
2974 |
ifFalse: [1]) |
|
2975 |
by:step |
|
8874 | 2976 |
|
2977 |
" |
|
2978 |
#(1 2 3 4 5 6 7) from:3 by:2 |
|
2979 |
( #(1 2 3 4 5 6 7) from:3 by:2) first |
|
2980 |
( #(1 2 3 4 5 6 7) from:3 by:2) last |
|
2981 |
( #(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
|
2982 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2983 |
! |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2984 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2985 |
from:startIndex count:numberOfElements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2986 |
"return a sub-collection consisting of numberOfElements elements |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2987 |
in the receiver starting at index start (i.e. reference to a slice). |
9291 | 2988 |
Warning: |
2989 |
The slice SHARES the memory for the element-data with the original, |
|
2990 |
this means that any modifications in the original are visible in the slice |
|
2991 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2992 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
2993 |
^ self |
9291 | 2994 |
from:startIndex |
2995 |
to:(startIndex + numberOfElements - 1) |
|
8874 | 2996 |
|
2997 |
" |
|
2998 |
#($a $b $c $d $e $f $g) from:2 count:3 |
|
2999 |
'1234567890' from:2 count:5 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3000 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3001 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3002 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3003 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3004 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3005 |
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
|
3006 |
slice := coll from:3 to:7. |
8874 | 3007 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3008 |
coll at:4 put:40. |
8874 | 3009 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3010 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3011 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3012 |
" |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3013 |
|coll slice| |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3014 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3015 |
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
|
3016 |
slice := coll from:3 to:7. |
8874 | 3017 |
slice. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3018 |
slice at:1 put:40. |
8874 | 3019 |
slice. |
3020 |
" |
|
3021 |
! |
|
3022 |
||
3023 |
from:startIndex to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3024 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3025 |
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
|
3026 |
(i.e. logically it represents the receiver copyFrom:startIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3027 |
however, physically, no copy is made). |
9291 | 3028 |
Warning: |
3029 |
The slice SHARES the memory for the element-data with the original, |
|
3030 |
this means that any modifications in the original are visible in the slice |
|
3031 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3032 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3033 |
^ self |
9291 | 3034 |
from:startIndex |
3035 |
to:endIndex |
|
3036 |
by: (startIndex <= endIndex |
|
3037 |
ifTrue: [1] |
|
3038 |
ifFalse: [-1]) |
|
8874 | 3039 |
|
3040 |
" |
|
3041 |
#(1 2 3 4 5 6 7) from:3 to:6 |
|
3042 |
( #(1 2 3 4 5 6 7) from:3 to:6) first |
|
3043 |
( #(1 2 3 4 5 6 7) from:3 to:6) last |
|
3044 |
( #(1 2 3 4 5 6 7) from:3 to:6) size |
|
3045 |
" |
|
3046 |
! |
|
3047 |
||
3048 |
from:startIndex to:endIndex by:step |
|
9291 | 3049 |
"Create a ReindexedCollection from the receiver. |
3050 |
The new collection represents the receivers elements up to endIndex. |
|
3051 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
3052 |
however, physically, no copy is made). |
|
3053 |
Warning: |
|
3054 |
The slice SHARES the memory for the element-data with the original, |
|
3055 |
this means that any modifications in the original are visible in the slice |
|
3056 |
and vice versa." |
|
7253 | 3057 |
|
3058 |
^ ReindexedCollection |
|
9291 | 3059 |
on:self |
3060 |
from:startIndex |
|
3061 |
to:endIndex |
|
3062 |
by:step |
|
8874 | 3063 |
|
3064 |
" |
|
3065 |
#(1 2 3 4 5 6 7) from:3 to:7 by:2 |
|
3066 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) first |
|
3067 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) last |
|
3068 |
( #(1 2 3 4 5 6 7) from:3 to:7 by:2) size |
|
3069 |
" |
|
3070 |
! |
|
3071 |
||
3072 |
to:endIndex |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3073 |
"Create a ReindexedCollection from the receiver. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3074 |
The new collection represents the receivers elements up to endIndex. |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3075 |
(i.e. logically it represents the receiver copyTo:endIndex, |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3076 |
however, physically, no copy is made). |
9291 | 3077 |
Warning: |
3078 |
The slice SHARES the memory for the element-data with the original, |
|
3079 |
this means that any modifications in the original are visible in the slice |
|
3080 |
and vice versa." |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3081 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3082 |
^ self |
9291 | 3083 |
from:1 |
3084 |
to:endIndex |
|
3085 |
by:1 |
|
8874 | 3086 |
|
3087 |
" |
|
3088 |
#(1 2 3 4 5 6 7) to:4 |
|
3089 |
( #(1 2 3 4 5 6 7) to:4) first |
|
3090 |
( #(1 2 3 4 5 6 7) to:4) last |
|
3091 |
( #(1 2 3 4 5 6 7) to:4) size |
|
3092 |
" |
|
3093 |
! |
|
3094 |
||
3095 |
to:endIndex by:step |
|
9291 | 3096 |
"Create a ReindexedCollection from the receiver. |
3097 |
The new collection represents the receivers elements up to endIndex. |
|
3098 |
(i.e. logically it represents the receiver copyTo:endIndex, |
|
3099 |
however, physically, no copy is made). |
|
3100 |
Warning: |
|
3101 |
The slice SHARES the memory for the element-data with the original, |
|
3102 |
this means that any modifications in the original are visible in the slice |
|
3103 |
and vice versa." |
|
7253 | 3104 |
|
3105 |
^ self |
|
9291 | 3106 |
from:(step > 0 |
3107 |
ifTrue: [1] |
|
3108 |
ifFalse: [self size]) |
|
3109 |
to:endIndex |
|
3110 |
by:step |
|
8874 | 3111 |
|
3112 |
" |
|
3113 |
#(1 2 3 4 5 6 7) to:4 by:2 |
|
3114 |
( #(1 2 3 4 5 6 7) to:4 by:2) first |
|
3115 |
( #(1 2 3 4 5 6 7) to:4 by:2) last |
|
3116 |
( #(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
|
3117 |
" |
7253 | 3118 |
! ! |
3119 |
||
1 | 3120 |
!SequenceableCollection methodsFor:'copying'! |
3121 |
||
9106 | 3122 |
++ aCollection |
3123 |
"return a new collection formed from concatenating the receiver with |
|
3124 |
the argument. The class of the new collection is determined by the |
|
3125 |
receivers class, so mixing classes is possible, if the second collections |
|
17922 | 3126 |
elements can be stored into instances of the receivers class. |
3127 |
This is just syntactic sugar for javascript" |
|
9106 | 3128 |
|
3129 |
^ self , aCollection |
|
3130 |
||
3131 |
" |
|
3132 |
#($a $b $c) ++ #(1 2 3) |
|
3133 |
#($a $b $c) ++ '123' |
|
3134 |
'abc' ++ '123' |
|
3135 |
'abc' ++ #($q $w $e $r $t $y) asSortedCollection |
|
3136 |
'abc' ++ #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3137 |
'abc' asArray ++ #(1 2 3 4 5) |
|
3138 |
" |
|
17922 | 3139 |
|
3140 |
"Modified (comment): / 14-02-2012 / 14:17:11 / cg" |
|
9106 | 3141 |
! |
3142 |
||
1 | 3143 |
, aCollection |
17938 | 3144 |
"return a new collection formed from concatenating the receiver with the argument. |
3145 |
The class of the new collection is determined by the |
|
3146 |
receiver's class, so mixing classes is possible, if the second collection's |
|
3147 |
elements can be stored into instances of the receiver's class." |
|
1 | 3148 |
|
8874 | 3149 |
|newCollection |
1 | 3150 |
mySize "{ Class: SmallInteger }" |
3151 |
newSize "{ Class: SmallInteger }" |
|
3152 |
otherSize "{ Class: SmallInteger }" |
|
3153 |
dstIndex "{ Class: SmallInteger }"| |
|
3154 |
||
3155 |
mySize := self size. |
|
3156 |
otherSize := aCollection size. |
|
3157 |
newSize := mySize + otherSize. |
|
202 | 3158 |
|
252 | 3159 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
1 | 3160 |
|
3161 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
3162 |
dstIndex := mySize + 1. |
|
202 | 3163 |
aCollection isSequenceable ifTrue:[ |
17938 | 3164 |
" |
3165 |
yes, aCollection has indexed elements |
|
3166 |
" |
|
3167 |
newCollection replaceFrom:dstIndex to:newSize with:aCollection startingAt:1. |
|
1 | 3168 |
] ifFalse:[ |
17938 | 3169 |
" |
3170 |
no, enumerate aCollection |
|
3171 |
" |
|
3172 |
aCollection do:[:element | |
|
3173 |
newCollection at:dstIndex put:element. |
|
3174 |
dstIndex := dstIndex + 1 |
|
3175 |
] |
|
1 | 3176 |
]. |
3177 |
^ newCollection |
|
44 | 3178 |
|
95 | 3179 |
" |
3180 |
#($a $b $c) , #(1 2 3) |
|
3181 |
#($a $b $c) , '123' |
|
3182 |
'abc' , '123' |
|
3183 |
'abc' , #($q $w $e $r $t $y) asSortedCollection |
|
3184 |
'abc' , #(1 2 3 4 5)" "-- will fail, since strings cannot store integers |
|
3185 |
'abc' asArray , #(1 2 3 4 5) |
|
3186 |
" |
|
17938 | 3187 |
|
3188 |
"Modified (comment): / 01-04-2012 / 13:17:56 / cg" |
|
1 | 3189 |
! |
3190 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3191 |
copyButFirst:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3192 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3193 |
except for the first count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3194 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3195 |
^ self copyFrom:(count + 1) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3196 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3197 |
" |
8874 | 3198 |
#($a $b $c $d $e $f $g) copyButFirst:2 |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3199 |
'1234567890' copyButFirst:2 |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3200 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3201 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3202 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3203 |
copyButLast:count |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3204 |
"return a new collection consisting of the receivers elements |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3205 |
except for the last count elements - for convenience." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3206 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3207 |
^ self copyTo:(self size - count) |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3208 |
|
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3209 |
" |
8874 | 3210 |
#($a $b $c $d $e $f $g) copyButLast:2 |
3211 |
'1234567890' copyButLast:2 |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3212 |
" |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3213 |
! |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3214 |
|
602 | 3215 |
copyFirst:count |
3216 |
"return a new collection consisting of the receivers first count |
|
3217 |
elements - this is just a rename of copyTo: - for compatibility." |
|
3218 |
||
3219 |
^ self copyFrom:1 to:count |
|
3220 |
||
3221 |
" |
|
3222 |
#($a $b $c $d $e $f $g) copyFirst:5 |
|
3223 |
'1234567890' copyFirst:4 |
|
3224 |
" |
|
3225 |
! |
|
3226 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3227 |
copyFrom:startIndex |
9234 | 3228 |
"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
|
3229 |
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
|
3230 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3231 |
^ self copyFrom:startIndex to:(self size) |
602 | 3232 |
|
3233 |
" |
|
3234 |
#($a $b $c $d $e $f $g) copyFrom:2 |
|
3235 |
'1234567890' copyFrom:2 |
|
8874 | 3236 |
(10 to:19) copyFrom:5 |
602 | 3237 |
" |
3238 |
! |
|
3239 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3240 |
copyFrom:startIndex count:numberOfElements |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3241 |
"return a new collection consisting of numberOfElements elements |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3242 |
extracted from the receiver starting at index start |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3243 |
(i.e. extract a slice). |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3244 |
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
|
3245 |
Raise an error, if stopIndex is out of range." |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3246 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3247 |
^ self |
8874 | 3248 |
copyFrom:startIndex |
3249 |
to:(startIndex + numberOfElements - 1) |
|
3250 |
||
3251 |
" |
|
3252 |
#($a $b $c $d $e $f $g) copyFrom:2 count:3 |
|
3253 |
'1234567890' copyFrom:2 count:5 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3254 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3255 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3256 |
"Modified: 20.2.1997 / 14:23:01 / cg" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3257 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
3258 |
|
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3259 |
copyFrom:startIndex to:stopIndex |
602 | 3260 |
"return a new collection consisting of receivers elements |
4403 | 3261 |
between start and stop. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3262 |
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
|
3263 |
Raise an error, if stopIndex is out of range." |
602 | 3264 |
|
3265 |
|newCollection newSize| |
|
3266 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3267 |
newSize := stopIndex - startIndex + 1. |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3268 |
newSize < 0 ifTrue:[ |
8874 | 3269 |
newSize := 0 |
4402
7c19502120b7
care for bad startIndex in #copyFrom:to:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
3270 |
]. |
602 | 3271 |
newCollection := self copyEmptyAndGrow:newSize. |
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3272 |
newCollection replaceFrom:1 to:newSize with:self startingAt:startIndex. |
602 | 3273 |
^ newCollection |
3274 |
||
3275 |
" |
|
3276 |
#($a $b $c $d $e $f $g) copyFrom:2 to:5 |
|
3277 |
'1234567890' copyFrom:2 to:5 |
|
8874 | 3278 |
(1 to:10) copyFrom:2 to:5 |
3279 |
||
3280 |
'1234567890' copyFrom:2 to:15 |
|
3281 |
(1 to:10) copyFrom:12 to:15 |
|
602 | 3282 |
" |
3283 |
! |
|
3284 |
||
3285 |
copyLast:count |
|
3286 |
"return a new collection consisting of the receivers last count |
|
3287 |
elements." |
|
3288 |
||
3289 |
|sz| |
|
3290 |
||
3291 |
sz := self size. |
|
3292 |
^ self copyFrom:(sz - count + 1) to:sz |
|
3293 |
||
3294 |
" |
|
3295 |
#($a $b $c $d $e $f $g) copyLast:5 |
|
3296 |
'1234567890' copyLast:4 |
|
3297 |
" |
|
3298 |
! |
|
3299 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3300 |
copyMapping:map |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3301 |
"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
|
3302 |
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
|
3303 |
Otherwise, it is left unchanged." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3304 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3305 |
|s| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3306 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3307 |
s := WriteStream on:self species new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3308 |
self do:[:el | |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3309 |
s nextPut:(map at:el ifAbsent:el) |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3310 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3311 |
^ s contents |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3312 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3313 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3314 |
|map| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3315 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3316 |
map := Dictionary new |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3317 |
at:1 put:'one'; |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3318 |
at:2 put:'two'; |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3319 |
yourself. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3320 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3321 |
#(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
|
3322 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3323 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3324 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3325 |
copyMappingFrom:inMap to:outMap |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3326 |
"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
|
3327 |
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
|
3328 |
Otherwise, it is left unchanged." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3329 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3330 |
|d| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3331 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3332 |
d := Dictionary new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3333 |
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
|
3334 |
^ self copyMapping:d |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3335 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3336 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3337 |
#(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
|
3338 |
'hello' copyMappingFrom:'eo' to:'**' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3339 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3340 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3341 |
|
3653 | 3342 |
copyReplaceAll:oldElement with:newElement |
3343 |
"return a copy of the receiver, where all elements equal to oldElement |
|
3344 |
have been replaced by newElement." |
|
3345 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3346 |
"/ 'Warning: #copyReplaceAll:with: will change semantics as defined in ANSI soon' errorPrintCR. |
3653 | 3347 |
^ self copy replaceAll:oldElement with:newElement |
3348 |
||
3349 |
" |
|
8874 | 3350 |
#(1 2 1 2 1 2 1 2 1 2) copyReplaceAll:1 with:99 |
3351 |
'hello world' copyReplaceAll:$l with:$* |
|
3653 | 3352 |
" |
3353 |
||
3354 |
"Modified: / 18.7.1998 / 22:52:17 / cg" |
|
3355 |
! |
|
3356 |
||
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3357 |
copyReplaceAll:oldObject withAll:aCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3358 |
"return a copy, where all oldObjects are replaced by all |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3359 |
elements from aCollection (i.e. sliced in). |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3360 |
Non-destructive; returns a new collection. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3361 |
The implementation here is a general-purpose one; |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3362 |
and should be redefined in String, if heavily used." |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3363 |
|
5489 | 3364 |
|s| |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3365 |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3366 |
"/ '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
|
3367 |
|
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3368 |
s := WriteStream on:self species new. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3369 |
self do:[:el | |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3370 |
el = oldObject ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3371 |
s nextPutAll:aCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3372 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3373 |
s nextPut:el |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3374 |
] |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3375 |
]. |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3376 |
^ s contents |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3377 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3378 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3379 |
args: oldObject : <object> |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3380 |
newObject : <collection> |
3992
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3381 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3382 |
returns: newCollection |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3383 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3384 |
|
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3385 |
" |
8874 | 3386 |
'123123abc123' copyReplaceAll:$1 withAll:'one' |
3387 |
#(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
|
3388 |
" |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3389 |
! |
549eefe16a28
added #copyReplaceAll:withAll:
Claus Gittinger <cg@exept.de>
parents:
3887
diff
changeset
|
3390 |
|
602 | 3391 |
copyReplaceFrom:startIndex to:endIndex with:aCollection |
3392 |
"return a copy of the receiver, where the elements from startIndex to |
|
3393 |
endIndex have been replaced by the elements of aCollection" |
|
3394 |
||
3395 |
|newColl sz replSize| |
|
3396 |
||
3397 |
replSize := aCollection size. |
|
3398 |
sz := self size - (endIndex - startIndex + 1) + replSize. |
|
3399 |
newColl := self copyEmptyAndGrow:sz. |
|
3400 |
newColl replaceFrom:1 to:(startIndex - 1) with:self. |
|
3401 |
newColl replaceFrom:startIndex with:aCollection. |
|
3402 |
newColl replaceFrom:(startIndex + replSize) with:self startingAt:(endIndex + 1). |
|
3403 |
^ newColl |
|
3404 |
||
3405 |
" |
|
3406 |
#(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 | 3407 |
'hello world' copyReplaceFrom:6 to:6 with:' there, ' |
17966 | 3408 |
'one two three' copyReplaceFrom:5 to:7 with:'zwei' |
3409 |
||
3410 |
the following is something we want, not an unexpected side feature: |
|
3411 |
'12345' copyReplaceFrom:5 to:4 with:'xxx' |
|
3412 |
" |
|
3413 |
||
3414 |
"Modified (comment): / 27-07-2012 / 16:39:17 / cg" |
|
602 | 3415 |
! |
3416 |
||
3417 |
copyReplaceFrom:startIndex with:aCollection |
|
3418 |
"return a copy of the receiver, where the elements from startIndex to |
|
3419 |
the end have been replaced by the elements of aCollection" |
|
3420 |
||
3421 |
^ self copyReplaceFrom:startIndex to:(self size) with:aCollection |
|
3422 |
||
3423 |
" |
|
8874 | 3424 |
#(1 2 3 4 5 6 7 8 9 0) copyReplaceFrom:3 with:#(a b c) |
3425 |
'hello world' copyReplaceFrom:7 with:'smalltalk fan' |
|
602 | 3426 |
" |
3427 |
! |
|
3428 |
||
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3429 |
copyReplacing:oldElement withObject:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3430 |
"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
|
3431 |
have been replaced by newElement. |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3432 |
ANSI version of what used to be #copyReplaceAll:with:" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3433 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3434 |
^ self copy replaceAll:oldElement with:newElement |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3435 |
|
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3436 |
" |
8874 | 3437 |
#(1 2 1 2 1 2 1 2 1 2) copyReplacing:1 withObject:99 |
3438 |
'hello world' copyReplacing:$l withObject:$* |
|
6054
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3439 |
" |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3440 |
! |
7719f857af51
added #copyReplacing:withObject: (ANSI)
Claus Gittinger <cg@exept.de>
parents:
6027
diff
changeset
|
3441 |
|
602 | 3442 |
copyThrough:element |
3443 |
"return a new collection consisting of the receiver elements |
|
8874 | 3444 |
up-to (AND including) the first occurrence of element." |
602 | 3445 |
|
3446 |
|idx| |
|
3447 |
||
3448 |
idx := self indexOf:element. |
|
3449 |
idx == 0 ifTrue:[^ nil]. "question: is this ok?" |
|
3450 |
^ self copyFrom:1 to:idx |
|
3451 |
||
3452 |
" |
|
3453 |
#($a $b $c $d $e $f $g) copyThrough:$d |
|
3454 |
'1234567890' copyThrough:$5 |
|
3455 |
'1234567890' copyThrough:$a |
|
3456 |
'1234567890' copyThrough:$1 |
|
3457 |
" |
|
3458 |
! |
|
3459 |
||
8373
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3460 |
copyTo:stopIndex |
9234 | 3461 |
"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
|
3462 |
Raise an error, if stopIndex is out of range." |
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3463 |
|
bd8ef62e964c
comments in reindexing methods
Claus Gittinger <cg@exept.de>
parents:
8367
diff
changeset
|
3464 |
^ self copyFrom:1 to:stopIndex |
602 | 3465 |
|
3466 |
" |
|
3467 |
#($a $b $c $d $e $f $g) copyTo:5 |
|
3468 |
'1234567890' copyTo:4 |
|
3469 |
" |
|
3470 |
! |
|
3471 |
||
5526 | 3472 |
copyToMax:stop |
3473 |
"return a new collection consisting of receivers elements |
|
8874 | 3474 |
from 1 up to (including) index stop, or up to the receivers end, |
5526 | 3475 |
whichever is smaller (i.e. like copyTo:, but do not err if receiver is smaller" |
3476 |
||
3477 |
^ self copyFrom:1 to:(self size min:stop) |
|
3478 |
||
3479 |
" |
|
3480 |
#($a $b $c $d $e $f $g) copyTo:10 - raises an error |
|
3481 |
#($a $b $c $d $e $f $g) copyToMax:10 |
|
3482 |
" |
|
3483 |
! |
|
3484 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3485 |
copyTransliterating:oldElements to:newElements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3486 |
"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
|
3487 |
The transiteraion works like perl's tr operator. |
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 |
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
|
3490 |
corresponding character in the replacement list. |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3491 |
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
|
3492 |
/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
|
3493 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3494 |
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
|
3495 |
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
|
3496 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3497 |
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
|
3498 |
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
|
3499 |
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
|
3500 |
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
|
3501 |
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
|
3502 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3503 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3504 |
^ self |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3505 |
copyTransliterating:oldElements to:newElements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3506 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3507 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3508 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3509 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3510 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3511 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3512 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3513 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3514 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3515 |
can also be used to remove character: |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3516 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3517 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3518 |
'abcdefg |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3519 |
hijkl1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3520 |
234567890' copyTransliterating:(Array with:Character cr) to:'' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3521 |
|
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3522 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3523 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3524 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3525 |
copyTransliterating:oldElements to:newElements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3526 |
complement:complement squashDuplicates:squashDuplicates |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3527 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3528 |
"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
|
3529 |
The transiteraion works like perl's tr operator. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3530 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3531 |
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
|
3532 |
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
|
3533 |
or deleted. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3534 |
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
|
3535 |
/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
|
3536 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3537 |
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
|
3538 |
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
|
3539 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3540 |
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
|
3541 |
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
|
3542 |
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
|
3543 |
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
|
3544 |
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
|
3545 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3546 |
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
|
3547 |
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
|
3548 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3549 |
|
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3550 |
|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
|
3551 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3552 |
map := Dictionary new. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3553 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3554 |
s1 := oldElements readStream. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3555 |
s2 := newElements readStream. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3556 |
range1 := range2 := nil. |
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 |
(range1 notNil and:[range1 atEnd]) ifTrue:[range1 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3559 |
range1 notNil or:[ s1 atEnd not ] |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3560 |
] whileTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3561 |
range1 notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3562 |
c1 := range1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3563 |
range1 atEnd ifTrue:[range1 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3564 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3565 |
c1 := s1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3566 |
s1 peek == $\ ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3567 |
s1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3568 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3569 |
s1 peek == $- ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3570 |
s1 next. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3571 |
last := s1 next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3572 |
inc := c1 < last ifTrue:[1] ifFalse:[-1]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3573 |
range1 := (c1 to:last by:inc) readStream. |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3574 |
c1 := range1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3575 |
range1 atEnd ifTrue:[range1 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3576 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3577 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3578 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3579 |
range2 notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3580 |
c2 := range2 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3581 |
range2 atEnd ifTrue:[range2 := nil]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3582 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3583 |
s2 atEnd ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3584 |
"/ keep c2 |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3585 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3586 |
c2 := s2 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3587 |
s2 peek == $\ ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3588 |
s1 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3589 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3590 |
s2 peek == $- ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3591 |
s2 next. |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3592 |
last := s2 next. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3593 |
inc := c2 < last ifTrue:[1] ifFalse:[-1]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3594 |
range2 := (c2 to:last by:inc) readStream. |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3595 |
c2 := range2 next. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3596 |
range2 atEnd ifTrue:[range2 := nil]. |
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 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3599 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3600 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3601 |
map at:c1 put:c2 |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3602 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3603 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3604 |
outStream := WriteStream on:(self species new). |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3605 |
self do:[:el | |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3606 |
|xLated out| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3607 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3608 |
complement ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3609 |
(map includesKey:el) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3610 |
xLated := out := el. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3611 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3612 |
out := newElements at:1 ifAbsent:nil. |
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 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3615 |
(map includesKey:el) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3616 |
xLated := out := map at:el. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3617 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3618 |
out := el. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3619 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3620 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3621 |
out notNil ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3622 |
(squashDuplicates and:[prevXlated notNil and:[prevXlated = xLated]]) ifTrue:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3623 |
"/ ignore |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3624 |
] ifFalse:[ |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3625 |
outStream nextPut:out. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3626 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3627 |
prevXlated := out. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3628 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3629 |
]. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3630 |
^ outStream contents |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3631 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3632 |
" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3633 |
'abcdefghijkl1234567890' copyTransliterating:'b-g' to:'B-G' |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3634 |
'abcdefghij-kl1234567890' copyTransliterating:'b\-g' to:'B+G' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3635 |
'abcdefghijkl1234567890' copyTransliterating:'69' to:'96' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3636 |
'abcdefghijkl1234567890' copyTransliterating:'a' to:'b' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3637 |
'abcdefghijkl1234567890' copyTransliterating:'aeiou' to:'AEIOU' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3638 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'QERTYUIOPX' |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3639 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'b-za' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3640 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'c-zab' |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3641 |
'abcdefghijkl1234567890' copyTransliterating:'a-z' to:'z-a' |
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3642 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3643 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3644 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3645 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3646 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'A' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3647 |
complement:false squashDuplicates:true |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3648 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3649 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3650 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3651 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3652 |
'abcdefghijkl1234567890' copyTransliterating:'0-9' to:'*' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3653 |
complement:true squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3654 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3655 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3656 |
complement:true squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3657 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3658 |
'abcdefghijkl1234567890' copyTransliterating:'a-zA-Z' to:' ' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3659 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3660 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3661 |
- delete all a-zA-Z |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3662 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3663 |
complement:false squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3664 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3665 |
- delete all except a-zA-Z |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3666 |
'abcdefghijkl1234567890abcdefghijkl' copyTransliterating:'a-zA-Z' to:'' |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3667 |
complement:true squashDuplicates:false |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3668 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3669 |
- squash multiple spaces |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3670 |
'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
|
3671 |
complement:false squashDuplicates:true |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3672 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3673 |
- squash multiple digits |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3674 |
'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
|
3675 |
complement:false squashDuplicates:true |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3676 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3677 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3678 |
|
17862 | 3679 |
copyUpThrough:element |
3680 |
"return a new collection consisting of the receiver elements |
|
3681 |
up-to (and including) the first occurrence of element; |
|
3682 |
or to the end, if element is not included" |
|
3683 |
||
3684 |
|idx| |
|
3685 |
||
3686 |
idx := self indexOf:element. |
|
3687 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3688 |
^ self copyFrom:1 to:idx |
|
3689 |
||
3690 |
" |
|
3691 |
#($a $b $c $d $e $f $g) copyUpThrough:$d |
|
3692 |
'1234567890' copyUpThrough:$5 |
|
3693 |
'1234567890' copyUpThrough:$a |
|
3694 |
'1234567890' copyUpThrough:$1 |
|
3695 |
'123456789' copyUpThrough:$0 |
|
3696 |
" |
|
3697 |
||
3698 |
"Created: / 14-09-2011 / 16:28:16 / cg" |
|
3699 |
! |
|
3700 |
||
602 | 3701 |
copyUpTo:element |
3702 |
"return a new collection consisting of the receiver elements |
|
8874 | 3703 |
up-to (but excluding) the first occurrence of element; |
602 | 3704 |
or to the end, if element is not included" |
3705 |
||
3706 |
|idx| |
|
3707 |
||
3708 |
idx := self indexOf:element. |
|
3709 |
idx == 0 ifTrue:[^ self copy]. "question: is this ok?" |
|
3710 |
idx == 1 ifTrue:[^ self copyEmpty]. |
|
3711 |
^ self copyFrom:1 to:(idx-1) |
|
3712 |
||
3713 |
" |
|
3714 |
#($a $b $c $d $e $f $g) copyUpTo:$d |
|
3715 |
'1234567890' copyUpTo:$5 |
|
3716 |
'1234567890' copyUpTo:$a |
|
3717 |
'1234567890' copyUpTo:$1 |
|
3718 |
'123456789' copyUpTo:$0 |
|
3719 |
" |
|
3720 |
! |
|
3721 |
||
1 | 3722 |
copyWith:newElement |
233 | 3723 |
"return a new collection containing the receivers elements |
8874 | 3724 |
and the single new element, newElement. |
233 | 3725 |
This is different from concatentation, which expects another collection |
3726 |
as argument, but equivalent to copy-and-addLast." |
|
1 | 3727 |
|
3728 |
|newCollection mySize newSize| |
|
3729 |
||
3730 |
mySize := self size. |
|
3731 |
newSize := mySize + 1. |
|
252 | 3732 |
newCollection := self copyEmptyAndGrow:newSize. |
1 | 3733 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
3734 |
newCollection at:newSize put:newElement. |
|
3735 |
^newCollection |
|
44 | 3736 |
|
95 | 3737 |
" |
3738 |
#(1 2 3 4 5) copyWith:$a |
|
3739 |
'abcdefg' copyWith:$h |
|
3740 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
3741 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
3742 |
" |
|
1 | 3743 |
! |
3744 |
||
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3745 |
copyWithFirst:newFirstElement |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3746 |
"return a new collection containing the receivers elements |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3747 |
and the single new element, newElement up front. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3748 |
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
|
3749 |
as argument, but equivalent to copy-and-addFirst." |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3750 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3751 |
|newCollection mySize newSize| |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3752 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3753 |
mySize := self size. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3754 |
newSize := mySize + 1. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3755 |
newCollection := self copyEmptyAndGrow:newSize. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3756 |
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
|
3757 |
newCollection at:1 put:newFirstElement. |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3758 |
^newCollection |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3759 |
|
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3760 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3761 |
#(1 2 3 4 5) copyWithFirst:$a |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3762 |
'abcdefg' copyWithFirst:$h |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3763 |
'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
|
3764 |
'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
|
3765 |
" |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3766 |
! |
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
3767 |
|
61 | 3768 |
copyWithout:elementToSkip |
3769 |
"return a new collection consisting of a copy of the receiver, with |
|
3770 |
ALL elements equal to elementToSkip are left out. |
|
3771 |
No error is reported, if elementToSkip is not in the collection." |
|
3772 |
||
293 | 3773 |
|n "{ Class: SmallInteger }" |
3774 |
sz "{ Class: SmallInteger }" |
|
3775 |
srcIndex "{ Class: SmallInteger }" |
|
3776 |
dstIndex "{ Class: SmallInteger }" |
|
3777 |
skipIndex "{ Class: SmallInteger }" |
|
3778 |
copy l| |
|
61 | 3779 |
|
8874 | 3780 |
"the code below may look like overkill, |
61 | 3781 |
however, for big collections its better to move data |
3782 |
around in big chunks" |
|
3783 |
||
3784 |
n := self occurrencesOf:elementToSkip. |
|
3785 |
n == 0 ifTrue:[^ self copy]. |
|
3786 |
||
3787 |
sz := self size. |
|
252 | 3788 |
copy := self copyEmptyAndGrow:(sz - n). |
61 | 3789 |
|
3790 |
srcIndex := 1. |
|
3791 |
dstIndex := 1. |
|
3792 |
||
3793 |
n timesRepeat:[ |
|
8874 | 3794 |
skipIndex := self indexOf:elementToSkip startingAt:srcIndex. |
3795 |
l := skipIndex - srcIndex. |
|
3796 |
l ~~ 0 ifTrue:[ |
|
3797 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3798 |
with:self startingAt:srcIndex. |
|
3799 |
dstIndex := dstIndex + l |
|
3800 |
]. |
|
3801 |
srcIndex := skipIndex + 1 |
|
61 | 3802 |
]. |
3803 |
l := sz - srcIndex. |
|
3804 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3805 |
with:self startingAt:srcIndex. |
61 | 3806 |
^ copy |
3807 |
||
95 | 3808 |
" |
3809 |
#($a $b $c $d $e $f $g) copyWithout:$d |
|
3810 |
#($a $b $c $d $e $f $g) copyWithout:$a |
|
3811 |
#($a $b $c $d $e $f $g) copyWithout:$g |
|
8874 | 3812 |
'abcdefghi' copyWithout:$h |
3813 |
'abcdefg' copyWithout:$h |
|
95 | 3814 |
#($a $b $c $a $a $d $e $a $f $g) copyWithout:$a |
3815 |
#($a $b $c $d $e $f $g) copyWithout:$x |
|
3816 |
#(90 80 70 60 50) copyWithout:70 |
|
3817 |
#(90 80 70 80 60 45 80 50) copyWithout:80 |
|
3818 |
" |
|
61 | 3819 |
! |
3820 |
||
602 | 3821 |
copyWithoutFirst:elementToSkip |
3822 |
"return a new collection consisting of a copy of the receivers elements |
|
8874 | 3823 |
without the first elementToSkip, if it was present. |
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3824 |
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
|
3825 |
Do not confuse this with copyButFirst:" |
602 | 3826 |
|
3827 |
|copy skipIndex sz| |
|
3828 |
||
3829 |
skipIndex := self indexOf:elementToSkip startingAt:1. |
|
3830 |
(skipIndex == 0) ifTrue:[^ self copy]. |
|
3831 |
||
3832 |
sz := self size - 1. |
|
3833 |
copy := self copyEmptyAndGrow:sz. |
|
3834 |
copy replaceFrom:1 to:(skipIndex - 1) with:self startingAt:1. |
|
3835 |
copy replaceFrom:skipIndex to:sz with:self startingAt:(skipIndex + 1). |
|
3836 |
^ copy |
|
3837 |
||
3838 |
" |
|
3839 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$d |
|
3840 |
#($a $b $c $d $e $f $g) copyWithoutFirst:$x |
|
3841 |
#(90 80 70 60 50) copyWithoutFirst:70 |
|
3842 |
#(90 80 70 80 60 45 80 50) copyWithoutFirst:80 |
|
3843 |
" |
|
3844 |
! |
|
3845 |
||
8673 | 3846 |
copyWithoutIdentical:elementToSkip |
3847 |
"return a new collection consisting of a copy of the receiver, with |
|
3848 |
ALL elements identical to elementToSkip are left out. |
|
3849 |
No error is reported, if elementToSkip is not in the collection." |
|
3850 |
||
3851 |
|n "{ Class: SmallInteger }" |
|
3852 |
sz "{ Class: SmallInteger }" |
|
3853 |
srcIndex "{ Class: SmallInteger }" |
|
3854 |
dstIndex "{ Class: SmallInteger }" |
|
3855 |
skipIndex "{ Class: SmallInteger }" |
|
3856 |
copy l| |
|
3857 |
||
8874 | 3858 |
"the code below may look like overkill, |
8673 | 3859 |
however, for big collections its better to move data |
3860 |
around in big chunks" |
|
3861 |
||
3862 |
n := self count:[:el | el == elementToSkip]. |
|
3863 |
n == 0 ifTrue:[^ self copy]. |
|
3864 |
||
3865 |
sz := self size. |
|
3866 |
copy := self copyEmptyAndGrow:(sz - n). |
|
3867 |
||
3868 |
srcIndex := 1. |
|
3869 |
dstIndex := 1. |
|
3870 |
||
3871 |
n timesRepeat:[ |
|
8874 | 3872 |
skipIndex := self identityIndexOf:elementToSkip startingAt:srcIndex. |
3873 |
l := skipIndex - srcIndex. |
|
3874 |
l ~~ 0 ifTrue:[ |
|
3875 |
copy replaceFrom:dstIndex to:(dstIndex + l - 1) |
|
3876 |
with:self startingAt:srcIndex. |
|
3877 |
dstIndex := dstIndex + l |
|
3878 |
]. |
|
3879 |
srcIndex := skipIndex + 1 |
|
8673 | 3880 |
]. |
3881 |
l := sz - srcIndex. |
|
3882 |
copy replaceFrom:dstIndex to:(dstIndex + l) |
|
8874 | 3883 |
with:self startingAt:srcIndex. |
8673 | 3884 |
^ copy |
3885 |
||
3886 |
" |
|
8874 | 3887 |
#(#a #b #c 'a' #e #f #g) copyWithoutIdentical:#a |
3888 |
#(#a #b #c 'a' #e #f #g) copyWithout:#a |
|
8673 | 3889 |
" |
3890 |
! |
|
3891 |
||
61 | 3892 |
copyWithoutIndex:omitIndex |
3893 |
"return a new collection consisting of receivers elements |
|
3894 |
without the argument stored at omitIndex" |
|
3895 |
||
3896 |
|copy sz| |
|
3897 |
||
202 | 3898 |
sz := self size - 1. |
252 | 3899 |
copy := self copyEmptyAndGrow:sz. |
293 | 3900 |
copy replaceFrom:1 to:(omitIndex - 1) with:self startingAt:1. |
3901 |
copy replaceFrom:omitIndex to:sz with:self startingAt:(omitIndex + 1). |
|
61 | 3902 |
^ copy |
3903 |
||
95 | 3904 |
" |
3905 |
#(1 2 3 4 5 6 7 8 9 0) copyWithoutIndex:3 |
|
3906 |
'abcdefghijkl' copyWithoutIndex:5 |
|
3907 |
" |
|
1 | 3908 |
! |
3909 |
||
202 | 3910 |
copyWithoutLast:count |
3911 |
"return a new collection consisting of the receivers elements |
|
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3912 |
except the last count elements. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3913 |
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
|
3914 |
because the name may confuse users with the copyWithoutFirst: functionality. |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3915 |
Please use copyButLast:." |
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3916 |
|
9250 | 3917 |
<resource:#obsolete> |
3918 |
||
5206
c0ebfca02f7c
added #copyButFirst: and #copyButLast:
Claus Gittinger <cg@exept.de>
parents:
5188
diff
changeset
|
3919 |
^ self copyTo:(self size - count) |
202 | 3920 |
|
3921 |
" |
|
8874 | 3922 |
#($a $b $c $d $e $f $g) copyWithoutLast:5 |
3923 |
'1234567890' copyWithoutLast:4 |
|
202 | 3924 |
" |
5231 | 3925 |
! |
3926 |
||
17754 | 3927 |
repeatedConcatenation:nTimes |
3928 |
"return a new collection consisting of nTimes the concatenation of the receiver" |
|
3929 |
||
3930 |
|newColl idx| |
|
3931 |
||
3932 |
newColl := self species new:(self size * nTimes). |
|
3933 |
idx := 1. |
|
3934 |
nTimes timesRepeat:[ |
|
3935 |
newColl replaceFrom:idx with:self startingAt:1. |
|
3936 |
idx := idx + self size. |
|
3937 |
]. |
|
3938 |
^ newColl |
|
3939 |
||
3940 |
" |
|
3941 |
'hello' repeatedConcatenation:5 |
|
3942 |
#[1 2 3] repeatedConcatenation:3 |
|
3943 |
#(1 2 3) repeatedConcatenation:1 |
|
3944 |
" |
|
3945 |
! |
|
3946 |
||
5231 | 3947 |
restAfter:anElement |
8366 | 3948 |
"return a new collection consisting of the receivers elements after |
5231 | 3949 |
(but excluding) anElement. |
8366 | 3950 |
If anElement is not in the receiver, the returned collection |
3951 |
will be empty. |
|
3952 |
See also #upTo:." |
|
5231 | 3953 |
|
3954 |
|pos| |
|
3955 |
||
3956 |
pos := self indexOf:anElement. |
|
8366 | 3957 |
pos == 0 ifTrue:[^ self copyEmpty]. |
3958 |
||
5231 | 3959 |
^ self copyFrom:(pos + 1) |
3960 |
||
3961 |
" |
|
8874 | 3962 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
3963 |
#(1 2 3 4 5 6 7 8 9) restAfter:5 |
|
3964 |
'hello world' upTo:Character space |
|
3965 |
'hello world' restAfter:Character space |
|
3966 |
'1234.5678' upTo:$. |
|
3967 |
'1234.5678' restAfter:$. |
|
8366 | 3968 |
" |
5231 | 3969 |
! |
3970 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3971 |
trimForWhich:aCheckBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3972 |
"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
|
3973 |
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
|
3974 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3975 |
|startIndex "{ Class: SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3976 |
endIndex "{ Class: SmallInteger }" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3977 |
sz| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3978 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3979 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3980 |
startIndex := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3981 |
endIndex := sz. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3982 |
|
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
3983 |
[(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
|
3984 |
startIndex := startIndex + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3985 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3986 |
[(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
|
3987 |
endIndex := endIndex - 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3988 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3989 |
startIndex > endIndex ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3990 |
^ self class new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3991 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3992 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3993 |
^ self |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3994 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3995 |
^ self copyFrom:startIndex to:endIndex |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3996 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
3997 |
" |
17734
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
3998 |
' foo ' trimForWhich:[:ch | ch isSeparator] |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
3999 |
' ' trimForWhich:[:ch | ch isSeparator] |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4000 |
#(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
|
4001 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4002 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4003 |
|
5231 | 4004 |
upTo:anElement |
4005 |
"return a new collection consisting of the receivers elements upTo |
|
4006 |
(but excluding) anElement. |
|
4007 |
If anElement is not in the receiver, the returned collection |
|
4008 |
will consist of all elements of the receiver. |
|
9307 | 4009 |
See also #restAfter: , #copyFrom:index." |
5231 | 4010 |
|
4011 |
|pos| |
|
4012 |
||
4013 |
pos := self indexOf:anElement. |
|
4014 |
pos == 0 ifTrue:[^ self copy]. |
|
4015 |
||
4016 |
^ self copyFrom:1 to:(pos - 1) |
|
4017 |
||
4018 |
" |
|
8874 | 4019 |
#(1 2 3 4 5 6 7 8 9) upTo:5 |
4020 |
'hello world' upTo:Character space |
|
4021 |
#(9 8 7 6 5 4 3 2 1) asSortedCollection upTo:5 |
|
4022 |
'1234.5678' upTo:$. |
|
4023 |
'1234' upTo:$. |
|
5231 | 4024 |
|
4025 |
raises an error: |
|
4026 |
||
8874 | 4027 |
(Dictionary new |
9307 | 4028 |
at:#foo put:'foo'; |
4029 |
at:#bar put:'bar'; |
|
4030 |
at:#baz put:'baz'; |
|
4031 |
yourself) upTo:#bar |
|
8874 | 4032 |
|
5231 | 4033 |
" |
4034 |
||
4035 |
"Modified: 11.5.1996 / 11:14:05 / cg" |
|
4036 |
! |
|
4037 |
||
4038 |
upTo:anElement count:count |
|
4039 |
"return a new collection consisting of the receivers elements upTo |
|
8874 | 4040 |
(but excluding) count found anElements, i.e. the first found count-1 |
4041 |
elements are included in the returned collection; if count < 0 the |
|
5231 | 4042 |
procedure is done from the end of the collection backwards. |
4043 |
If anElement is not in the receiver, the returned collection |
|
4044 |
will consist of all elements of the receiver" |
|
4045 |
||
4046 |
|startPos endPos pos found| |
|
4047 |
||
4048 |
(count == 0 or: [count == 1]) ifTrue:[ |
|
8874 | 4049 |
^ self upTo:anElement |
5231 | 4050 |
]. |
4051 |
||
4052 |
startPos := 1. |
|
4053 |
endPos := self size. |
|
4054 |
found := 0. |
|
4055 |
||
4056 |
count > 1 ifTrue:[ |
|
8874 | 4057 |
pos := 0. |
4058 |
[pos < self size and: [found < count]] |
|
4059 |
whileTrue: [pos := pos + 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
4060 |
found == count ifTrue:[ |
|
4061 |
endPos := pos - 1 |
|
4062 |
] |
|
4063 |
]. |
|
5231 | 4064 |
|
4065 |
count < 0 ifTrue:[ |
|
8874 | 4066 |
pos := self size + 1. |
4067 |
[pos > 1 and: [found < count abs]] |
|
4068 |
whileTrue: [pos := pos - 1. (self at: pos) == anElement ifTrue: [found := found + 1]]. |
|
4069 |
found == count abs ifTrue:[ |
|
4070 |
startPos := pos + 1 |
|
4071 |
] |
|
4072 |
]. |
|
5231 | 4073 |
|
4074 |
^ self copyFrom: startPos to: endPos |
|
4075 |
||
4076 |
" |
|
8874 | 4077 |
'hello1_hello2_hello3' upTo:$_ count: 2 returns first two 'hellos' |
5231 | 4078 |
'hello1_hello2_hello3' upTo:$_ count: -1 returns last 'hello' |
4079 |
'hello1_hello2_hello3' upTo:$_ count: 0 returns first 'hello1' |
|
4080 |
'hello1_hello2_hello3' upTo:$_ count: 1 returns first 'hello1' |
|
4081 |
'hello1_hello2_hello3' upTo:$_ same; returns first 'hello1' |
|
4082 |
" |
|
5563 | 4083 |
! |
4084 |
||
4085 |
upToAny:aCollectionOfObjects |
|
4086 |
"return a new collection consisting of the receivers elements upTo |
|
4087 |
(but excluding) any in aCollectionOfObjects. |
|
4088 |
If none of aCollectionOfObjects is found in the receiver, the returned collection |
|
4089 |
will consist of all elements of the receiver. |
|
4090 |
See also #upTo:" |
|
4091 |
||
4092 |
|pos| |
|
4093 |
||
4094 |
pos := self indexOfAny:aCollectionOfObjects. |
|
4095 |
pos == 0 ifTrue:[^ self copy]. |
|
4096 |
||
4097 |
^ self copyFrom:1 to:(pos - 1) |
|
4098 |
||
4099 |
" |
|
8874 | 4100 |
'hello world' upToAny:#($/ $:) |
4101 |
'hello:world' upToAny:#($/ $:) |
|
4102 |
'hello/world' upToAny:#($/ $:) |
|
5563 | 4103 |
" |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4104 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4105 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4106 |
withoutLeadingForWhich:aCheckBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4107 |
"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
|
4108 |
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
|
4109 |
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
|
4110 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4111 |
|index sz| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4112 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4113 |
sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4114 |
index := 1. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4115 |
[index <= sz] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4116 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4117 |
index == 1 ifTrue:[^ self]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4118 |
^ self copyFrom:index |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4119 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4120 |
index := index + 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4121 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4122 |
^ self class new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4123 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4124 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4125 |
'****foo****' withoutLeadingForWhich:[:ch | ch isLetter not] |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4126 |
#( 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
|
4127 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4128 |
! |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4129 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4130 |
withoutTrailingForWhich:aCheckBlock |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4131 |
"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
|
4132 |
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
|
4133 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4134 |
|index sz| |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4135 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4136 |
index := sz := self size. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4137 |
[index ~~ 0] whileTrue:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4138 |
(aCheckBlock value:(self at:index)) ifFalse:[ |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4139 |
index == sz ifTrue:[^ self]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4140 |
^ self copyTo:index |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4141 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4142 |
index := index - 1 |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4143 |
]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4144 |
^ self class new |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4145 |
|
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4146 |
" |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4147 |
' foo....' withoutTrailingForWhich:[:ch | ch isLetter not]. |
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
4148 |
#( 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
|
4149 |
" |
602 | 4150 |
! ! |
4151 |
||
4152 |
!SequenceableCollection methodsFor:'enumerating'! |
|
4153 |
||
4154 |
collect:aBlock |
|
4155 |
"evaluate the argument, aBlock for every element in the collection |
|
4156 |
and return a collection of the results" |
|
4157 |
||
4158 |
|newCollection |
|
4159 |
sz "{ Class:SmallInteger }"| |
|
4160 |
||
4161 |
sz := self size. |
|
4162 |
newCollection := self copyEmptyAndGrow:sz. |
|
4163 |
1 to:sz do:[:index | |
|
8874 | 4164 |
newCollection at:index put:(aBlock value:(self at:index)). |
602 | 4165 |
]. |
4166 |
^ newCollection |
|
4167 |
||
4168 |
" |
|
8874 | 4169 |
#(one two three four five six) collect:[:element | element asUppercase] |
4170 |
#(1 2 3 4 5 6 7 8 9) collect:[:element | element factorial] |
|
4171 |
(1 to:9) collect:[:element | element * element] |
|
602 | 4172 |
" |
202 | 4173 |
! |
4174 |
||
602 | 4175 |
do:aBlock |
4176 |
"evaluate the argument, aBlock for every element in the collection." |
|
4177 |
||
4178 |
|stop "{ Class:SmallInteger }"| |
|
4179 |
||
4180 |
stop := self size. |
|
4181 |
1 to:stop do:[:index | |
|
8874 | 4182 |
aBlock value:(self at:index). |
602 | 4183 |
] |
2927 | 4184 |
|
602 | 4185 |
" |
1422 | 4186 |
#(one two three four five six) do:[:element | Transcript showCR:element] |
602 | 4187 |
" |
4188 |
! |
|
4189 |
||
2927 | 4190 |
do:aBlock separatedBy:sepBlock |
4191 |
"evaluate the argument, aBlock for every element in the collection. |
|
4192 |
Between each element (i.e. not before the first element and not after the |
|
4193 |
last element), evaluate sepBlock. |
|
4194 |
This supports printing of collections with elements separated by some |
|
4195 |
string." |
|
4196 |
||
4197 |
|stop "{ Class:SmallInteger }"| |
|
4198 |
||
4199 |
stop := self size. |
|
4200 |
stop < 1 ifTrue:[^ self]. |
|
4201 |
aBlock value:(self at:1). |
|
4202 |
2 to:stop do:[:index | |
|
8874 | 4203 |
sepBlock value. |
4204 |
aBlock value:(self at:index) |
|
2927 | 4205 |
]. |
4206 |
||
4207 |
" |
|
4208 |
#(one two three four five six) |
|
8874 | 4209 |
do:[:each | Transcript show:each] |
4210 |
separatedBy:[Transcript show:' , '] |
|
2927 | 4211 |
" |
4212 |
! |
|
3003 | 4213 |
|
3044 | 4214 |
doWithIndex:aBlock |
17928 | 4215 |
"Squeak/V'Age compatibility; |
4216 |
like keysAndValuesDo:, but passes the index as second argument." |
|
4217 |
||
4218 |
self keysAndValuesDo:[:index :el | aBlock value:el value:index]. |
|
4219 |
||
4220 |
"Created: / 17-10-1997 / 12:33:10 / cg" |
|
3044 | 4221 |
! |
4222 |
||
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4223 |
from:start collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4224 |
"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
|
4225 |
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
|
4226 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4227 |
^ self from:start to:self size collect:aBlock |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4228 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4229 |
" |
8874 | 4230 |
#(one two three four five six) |
4231 |
from:2 |
|
4232 |
collect:[:element | element asUppercase] |
|
5220
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4233 |
" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4234 |
|
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4235 |
"Created: / 30.1.2000 / 01:02:28 / cg" |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4236 |
! |
66ac0778fe08
added #from:collect: - for convenience
Claus Gittinger <cg@exept.de>
parents:
5206
diff
changeset
|
4237 |
|
17944 | 4238 |
from:start conform:aOneArgBlock |
4239 |
"return true, if the elements starting at the start-index conform to some condition. |
|
4240 |
I.e. return false, if aBlock returns false for any of those elements; |
|
4241 |
true otherwise." |
|
4242 |
||
4243 |
self from:start to:(self size) do:[:element | |
|
4244 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
4245 |
]. |
|
4246 |
^ true |
|
4247 |
||
4248 |
"Created: / 25-05-2012 / 14:02:13 / cg" |
|
4249 |
! |
|
4250 |
||
602 | 4251 |
from:startIndex do:aBlock |
4252 |
"evaluate the argument, aBlock for the elements starting with the |
|
4253 |
element at startIndex to the end." |
|
4254 |
||
4255 |
^ self from:startIndex to:self size do:aBlock |
|
1 | 4256 |
|
95 | 4257 |
" |
8874 | 4258 |
#(one two three four five six) |
4259 |
from:3 |
|
4260 |
do:[:element | Transcript showCR:element] |
|
602 | 4261 |
" |
4262 |
! |
|
4263 |
||
7490 | 4264 |
from:startIndex keysAndValuesDo:aBlock |
4265 |
"evaluate the argument, aBlock for the elements and indices starting with the |
|
4266 |
element at startIndex to the end." |
|
4267 |
||
4268 |
^ self from:startIndex to:self size keysAndValuesDo:aBlock |
|
4269 |
||
4270 |
" |
|
8874 | 4271 |
#(one two three four five six) |
4272 |
from:3 |
|
4273 |
keysAndValuesDo:[:element :idx | Transcript showCR:(idx -> element) ] |
|
7490 | 4274 |
" |
4275 |
! |
|
4276 |
||
7337 | 4277 |
from:index1 to:index2 by:stepArg do:aBlock |
4278 |
"evaluate the argument, aBlock for the elements with index index1 to index2 stepping by step in the collection" |
|
4279 |
||
4280 |
|start "{ Class:SmallInteger }" |
|
4281 |
stop "{ Class:SmallInteger }" |
|
4282 |
step "{ Class:SmallInteger }" | |
|
4283 |
||
4284 |
start := index1. "/ these assignments force type checking... |
|
4285 |
stop := index2. "/ and guarantee inline loop code below. |
|
4286 |
step := stepArg. |
|
4287 |
||
4288 |
start to:stop by:step do:[:index | |
|
8874 | 4289 |
aBlock value:(self at:index). |
7337 | 4290 |
] |
4291 |
||
4292 |
" |
|
8874 | 4293 |
#(one two three four five six seven eight nine ten) |
4294 |
from:2 |
|
4295 |
to:10 |
|
4296 |
by:2 |
|
4297 |
do:[:element | Transcript showCR:element] |
|
7337 | 4298 |
" |
4299 |
! |
|
4300 |
||
602 | 4301 |
from:start to:stop collect:aBlock |
4302 |
"evaluate the argument, aBlock for the elements indexed by start |
|
4303 |
to stop in the collection and return a collection of the results" |
|
4304 |
||
4305 |
|newCollection sz |
|
4306 |
idx "{ Class:SmallInteger }"| |
|
4307 |
||
4308 |
sz := stop - start + 1. |
|
4309 |
newCollection := self copyEmptyAndGrow:sz. |
|
4310 |
idx := 1. |
|
4311 |
start to:stop do:[:index | |
|
8874 | 4312 |
newCollection at:idx put:(aBlock value:(self at:index)). |
4313 |
idx := idx + 1 |
|
602 | 4314 |
]. |
4315 |
^ newCollection |
|
4316 |
||
4317 |
" |
|
8874 | 4318 |
#(one two three four five six) |
4319 |
from:2 |
|
4320 |
to:4 |
|
4321 |
collect:[:element | element asUppercase] |
|
602 | 4322 |
" |
4323 |
! |
|
4324 |
||
17944 | 4325 |
from:start to:end conform:aOneArgBlock |
4326 |
"return true, if the elements from start-index to end-index conform to some condition. |
|
4327 |
I.e. return false, if aBlock returns false for any of those elements; |
|
4328 |
true otherwise." |
|
4329 |
||
4330 |
self from:start to:end do:[:element | |
|
4331 |
(aOneArgBlock value:element) ifFalse:[^ false] |
|
4332 |
]. |
|
4333 |
^ true |
|
4334 |
||
4335 |
" |
|
4336 |
#(1 2 3 4 5) from:2 to:2 conform:[:el | el even] |
|
4337 |
#(1 2 2 4 5) from:2 to:4 conform:[:el | el even] |
|
4338 |
#(1 2 2 4 5) from:2 to:5 conform:[:el | el even] |
|
4339 |
#(2 4 6 8 10) conform:[:el | el even] |
|
4340 |
" |
|
4341 |
||
4342 |
"Modified: / 13-09-2006 / 11:19:03 / cg" |
|
4343 |
"Created: / 25-05-2012 / 14:00:50 / cg" |
|
4344 |
! |
|
4345 |
||
602 | 4346 |
from:index1 to:index2 do:aBlock |
4347 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4348 |
index2 in the collection" |
|
4349 |
||
4350 |
|start "{ Class:SmallInteger }" |
|
4351 |
stop "{ Class:SmallInteger }" | |
|
4352 |
||
7337 | 4353 |
start := index1. "/ these assignments force type checking... |
4354 |
stop := index2. "/ and guarantee inline loop code below. |
|
4355 |
||
602 | 4356 |
start to:stop do:[:index | |
17841 | 4357 |
aBlock value:(self at:index). |
602 | 4358 |
] |
4359 |
||
4360 |
" |
|
8874 | 4361 |
#(one two three four five six) |
17841 | 4362 |
from:3 to:5 do:[:element | Transcript showCR:element] |
4363 |
" |
|
4364 |
||
4365 |
"Modified: / 02-06-2011 / 13:23:06 / cg" |
|
602 | 4366 |
! |
4367 |
||
17928 | 4368 |
from:index1 to:index2 doWithIndex:aBlock |
4369 |
"Squeak/V'Age compatibility; |
|
4370 |
like keysAndValuesDo:, but passes the index as second argument." |
|
4371 |
||
4372 |
self from:index1 to:index2 keysAndValuesDo:[:index :el | aBlock value:el value:index]. |
|
4373 |
||
4374 |
"Created: / 29-02-2012 / 11:47:48 / cg" |
|
4375 |
! |
|
4376 |
||
6868 | 4377 |
from:index1 to:index2 keysAndValuesDo:aBlock |
4378 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4379 |
index2 in the collection; pass both index and value." |
|
4380 |
||
4381 |
|start "{ Class:SmallInteger }" |
|
4382 |
stop "{ Class:SmallInteger }" | |
|
4383 |
||
7337 | 4384 |
start := index1. "/ these assignments force type checking... |
4385 |
stop := index2. "/ and guarantee inline loop code below. |
|
4386 |
||
6868 | 4387 |
start to:stop do:[:index | |
8874 | 4388 |
aBlock value:index value:(self at:index). |
6868 | 4389 |
] |
4390 |
||
4391 |
" |
|
8874 | 4392 |
#(one two three four five six) |
4393 |
from:3 |
|
4394 |
to:5 |
|
4395 |
keysAndValuesDo:[:idx :element | Transcript show:idx; space; showCR:element] |
|
6868 | 4396 |
" |
4397 |
! |
|
4398 |
||
17841 | 4399 |
from:index1 to:index2 orEndDo:aBlock |
4400 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4401 |
index2 or the end (whichever comes first) in the collection" |
|
4402 |
||
4403 |
self from:index1 to:(index2 min:self size) do:aBlock |
|
4404 |
||
4405 |
" |
|
4406 |
#(one two three four five six) |
|
4407 |
from:3 to:10 orEndDo:[:element | Transcript showCR:element] |
|
4408 |
" |
|
4409 |
||
4410 |
"Created: / 02-06-2011 / 13:21:32 / cg" |
|
4411 |
! |
|
4412 |
||
602 | 4413 |
from:index1 to:index2 reverseDo:aBlock |
4414 |
"evaluate the argument, aBlock for the elements with index index1 to |
|
4415 |
index2 in the collection. Step in reverse order" |
|
4416 |
||
4417 |
|start "{ Class:SmallInteger }" |
|
4418 |
stop "{ Class:SmallInteger }" | |
|
4419 |
||
7337 | 4420 |
start := index1. "/ these assignments force type checking... |
4421 |
stop := index2. "/ and guarantee inline loop code below. |
|
4422 |
||
602 | 4423 |
stop to:start by:-1 do:[:index | |
8874 | 4424 |
aBlock value:(self at:index). |
602 | 4425 |
] |
4426 |
||
4427 |
" |
|
8874 | 4428 |
#(one two three four five six) |
4429 |
from:3 |
|
4430 |
to:5 |
|
4431 |
reverseDo:[:element | Transcript showCR:element] |
|
95 | 4432 |
" |
61 | 4433 |
! |
4434 |
||
6027 | 4435 |
from:startIndex to:stopIndex select:aBlock |
4436 |
"evaluate the argument, aBlock for the elements at startIndex..stopIndex |
|
4437 |
and return a collection of those elements for which the block return true." |
|
4438 |
||
4439 |
|element newColl species needCopy |
|
4440 |
n "{ Class:SmallInteger }"| |
|
4441 |
||
4442 |
n := stopIndex - startIndex + 1. |
|
4443 |
species := self species. |
|
17940 | 4444 |
species growIsCheap ifTrue:[ |
4445 |
newColl := self copyEmpty:n. |
|
4446 |
needCopy := false |
|
17955 | 4447 |
] ifFalse:[ |
17940 | 4448 |
newColl := OrderedCollection new:n. |
4449 |
needCopy := true |
|
6027 | 4450 |
]. |
4451 |
startIndex to:stopIndex do:[:index | |
|
17940 | 4452 |
element := self at:index. |
4453 |
(aBlock value:element) ifTrue:[ |
|
4454 |
newColl add:element |
|
4455 |
]. |
|
6027 | 4456 |
]. |
4457 |
needCopy ifTrue:[ |
|
17940 | 4458 |
newColl := (species withAll:newColl) postCopyFrom:self |
6027 | 4459 |
]. |
4460 |
^ newColl |
|
4461 |
||
4462 |
" |
|
8874 | 4463 |
#(faba one two three four five six) |
17940 | 4464 |
from:2 to:5 select:[:element | element startsWith:'f'] |
6027 | 4465 |
" |
4466 |
! |
|
4467 |
||
17910 | 4468 |
from:startArg to:stopArg with:aSequenceableCollection doWithIndex:aThreeArgBlock |
4469 |
"evaluate the argument, aBlock for successive elements from |
|
4470 |
each the receiver and the argument, aSequenceableCollection. |
|
4471 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4472 |
the index as arguments. |
|
4473 |
The collection argument must implement access via a numeric key." |
|
4474 |
||
4475 |
|start "{ Class: SmallInteger }" |
|
4476 |
stop "{ Class: SmallInteger }" | |
|
4477 |
||
4478 |
start := startArg. |
|
4479 |
stop := stopArg. |
|
4480 |
start to:stop do:[:index | |
|
4481 |
aThreeArgBlock |
|
4482 |
value:(self at:index) |
|
4483 |
value:(aSequenceableCollection at:index) |
|
4484 |
value:index. |
|
4485 |
] |
|
4486 |
||
4487 |
" |
|
4488 |
#(one two three four five six) |
|
4489 |
from:2 to:5 |
|
4490 |
with:(1 to:10) |
|
4491 |
doWithIndex:[:el1 :el2 :idx| Transcript show:idx; space; show:el1; space; showCR:el2] |
|
4492 |
" |
|
4493 |
||
4494 |
"Created: / 08-01-2012 / 17:18:34 / cg" |
|
4495 |
! |
|
4496 |
||
10150 | 4497 |
inGroupsOf:n collect:anNArgBlock |
4498 |
"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
|
4499 |
and collect the results. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4500 |
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
|
4501 |
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
|
4502 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4503 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4504 |
|stop "{ Class:SmallInteger }" newCollection dstIdx argVector rslt| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4505 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4506 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4507 |
newCollection := self copyEmptyAndGrow:stop // n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4508 |
dstIdx := 1. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4509 |
|
10150 | 4510 |
"/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to |
4511 |
"/ 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
|
4512 |
n == 2 ifTrue:[ |
10150 | 4513 |
1 to:stop by:n do:[:index | |
4514 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4515 |
newCollection at:dstIdx put:rslt. |
|
4516 |
dstIdx := dstIdx + 1. |
|
4517 |
]. |
|
4518 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4519 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4520 |
n == 3 ifTrue:[ |
10150 | 4521 |
1 to:stop by:n do:[:index | |
4522 |
rslt := anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4523 |
newCollection at:dstIdx put:rslt. |
|
4524 |
dstIdx := dstIdx + 1. |
|
4525 |
]. |
|
4526 |
^ newCollection. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4527 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4528 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4529 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4530 |
1 to:stop by:n do:[:index | |
10150 | 4531 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4532 |
rslt := anNArgBlock valueWithArguments:argVector. |
|
4533 |
newCollection at:dstIdx put:rslt. |
|
4534 |
dstIdx := dstIdx + 1. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4535 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4536 |
^ newCollection |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4537 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4538 |
"for groups of 2, this is the same as: |
8874 | 4539 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4540 |
pairWiseCollect:[:num :sym | num->sym] |
8874 | 4541 |
|
4542 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4543 |
inGroupsOf:2 collect:[:num :sym | num->sym] |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4544 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4545 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4546 |
inGroupsOf:5 collect:[:a :b :c :d :e | Array with:a with:b with:c with:d with:e] |
4547 |
" |
|
4548 |
||
4549 |
"Modified: / 27-10-2006 / 10:07:02 / cg" |
|
4550 |
! |
|
4551 |
||
4552 |
inGroupsOf:n do:anNArgBlock |
|
4553 |
"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
|
4554 |
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
|
4555 |
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
|
4556 |
is not a multiple of n." |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4557 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4558 |
|stop "{ Class:SmallInteger }" argVector| |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4559 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4560 |
stop := self size. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4561 |
|
10150 | 4562 |
"/ the reason for inlining the cases for 2-4 args is to avoid the temporary object creation, and to |
4563 |
"/ 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
|
4564 |
n == 2 ifTrue:[ |
10150 | 4565 |
1 to:stop by:n do:[:index | |
4566 |
anNArgBlock value:(self at:index) value:(self at:index+1). |
|
4567 |
]. |
|
4568 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4569 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4570 |
n == 3 ifTrue:[ |
10150 | 4571 |
1 to:stop by:n do:[:index | |
4572 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2). |
|
4573 |
]. |
|
4574 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4575 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4576 |
n == 4 ifTrue:[ |
10150 | 4577 |
1 to:stop by:n do:[:index | |
4578 |
anNArgBlock value:(self at:index) value:(self at:index+1) value:(self at:index+2) value:(self at:index+3). |
|
4579 |
]. |
|
4580 |
^ self. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4581 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4582 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4583 |
argVector := Array new:n. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4584 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4585 |
1 to:stop by:n do:[:index | |
10150 | 4586 |
argVector replaceFrom:1 to:n with:self startingAt:index. |
4587 |
anNArgBlock valueWithArguments:argVector. |
|
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4588 |
]. |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4589 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4590 |
"for groups of 2, this is the same as: |
8874 | 4591 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4592 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4593 |
|
4594 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
|
10150 | 4595 |
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
|
4596 |
|
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4597 |
#( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ) |
10150 | 4598 |
inGroupsOf:5 do:[:a :b :c :d :e | Transcript show:a;space;show:b;space;show:c;space;show:d;space;showCR:e] |
4599 |
" |
|
4600 |
||
4601 |
"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
|
4602 |
! |
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4603 |
|
8853 | 4604 |
keysAndValuesCollect:aTwoArgBlock |
4605 |
"evaluate the argument, aBlock for every element in the collection, |
|
4606 |
passing both index and element as arguments. |
|
4607 |
Collect the returned values and return them." |
|
4608 |
||
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4609 |
|sz newCollection| |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4610 |
|
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4611 |
sz := self size. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4612 |
newCollection := self copyEmptyAndGrow:sz. |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4613 |
1 to:sz do:[:index | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4614 |
newCollection at:index put:(aTwoArgBlock value:index value:(self at:index)). |
8853 | 4615 |
]. |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4616 |
|
8853 | 4617 |
^ newCollection |
4618 |
||
4619 |
" |
|
8874 | 4620 |
#(one two three four five six) |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4621 |
keysAndValuesCollect:[:key :element | |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4622 |
key even ifTrue:element ifFalse:[element asUppercase]] |
8853 | 4623 |
|
4624 |
||
4625 |
|d| |
|
4626 |
||
4627 |
d := Dictionary new. |
|
4628 |
d at:'a' put:'A'. |
|
4629 |
d at:'b' put:'B'. |
|
4630 |
d at:'c' put:'C'. |
|
4631 |
d at:'d' put:'D'. |
|
4632 |
d at:'e' put:'E'. |
|
8874 | 4633 |
d keysAndValuesCollect:[:key :element | |
9301
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4634 |
key first codePoint even |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4635 |
ifTrue:element |
c13b68ca4a66
keysAndValuesCollect: was bad for fixed-sized collections
Claus Gittinger <cg@exept.de>
parents:
9291
diff
changeset
|
4636 |
ifFalse:[element asLowercase]] |
8853 | 4637 |
" |
4638 |
! |
|
4639 |
||
602 | 4640 |
keysAndValuesDo:aTwoArgBlock |
4641 |
"evaluate the argument, aBlock for every element in the collection, |
|
4642 |
passing both index and element as arguments." |
|
4643 |
||
4644 |
|stop "{ Class:SmallInteger }"| |
|
4645 |
||
4646 |
stop := self size. |
|
4647 |
1 to:stop do:[:index | |
|
8874 | 4648 |
aTwoArgBlock value:index value:(self at:index). |
602 | 4649 |
] |
4650 |
" |
|
8874 | 4651 |
#(one two three four five six) |
4652 |
keysAndValuesDo:[:key :element | |
|
4653 |
Transcript show:key; space; showCR:element |
|
4654 |
] |
|
602 | 4655 |
" |
4656 |
! |
|
4657 |
||
1359 | 4658 |
keysAndValuesReverseDo:aTwoArgBlock |
8874 | 4659 |
"evaluate the argument, aBlock in reverse order, for every element |
1359 | 4660 |
in the collection, passing both index and element as arguments." |
4661 |
||
4662 |
|stop "{ Class:SmallInteger }"| |
|
4663 |
||
4664 |
stop := self size. |
|
4665 |
stop to:1 by:-1 do:[:index | |
|
8874 | 4666 |
aTwoArgBlock value:index value:(self at:index). |
1359 | 4667 |
] |
4668 |
||
4669 |
" |
|
8874 | 4670 |
#(one two three four five six) |
4671 |
keysAndValuesReverseDo:[:key :element | |
|
4672 |
Transcript show:key; space; showCR:element |
|
4673 |
] |
|
1359 | 4674 |
" |
4675 |
||
4676 |
"Modified: 9.5.1996 / 00:57:23 / cg" |
|
4677 |
! |
|
4678 |
||
17797 | 4679 |
keysDo:aBlock |
4680 |
"evaluate the argument, aBlock for every key in the collection. |
|
4681 |
That is: enumerate the indices. |
|
4682 |
Here mostly for protocol compatibility." |
|
4683 |
||
17892 | 4684 |
1 to:(self size) do:aBlock |
17797 | 4685 |
|
4686 |
" |
|
4687 |
#(one two three four five six) |
|
4688 |
keysDo:[:key | Transcript show:key; space; showCR:element |
|
4689 |
] |
|
4690 |
" |
|
4691 |
||
4692 |
"Created: / 24-08-2010 / 10:11:23 / cg" |
|
4693 |
! |
|
4694 |
||
602 | 4695 |
nonNilElementsDo:aBlock |
4696 |
"evaluate the argument, aBlock for every non-nil element in the collection." |
|
4697 |
||
4698 |
|stop "{ Class:SmallInteger }" |
|
4699 |
element| |
|
4700 |
||
4701 |
stop := self size. |
|
4702 |
1 to:stop do:[:index | |
|
8874 | 4703 |
(element := self at:index) notNil ifTrue:[ |
4704 |
aBlock value:element. |
|
4705 |
] |
|
602 | 4706 |
] |
4707 |
" |
|
1422 | 4708 |
#(one nil three nil five nil seven) nonNilElementsDo:[:element | Transcript showCR:element] |
602 | 4709 |
" |
4710 |
! |
|
4711 |
||
10150 | 4712 |
pairWiseCollect:aTwoArgBlock |
4713 |
"evaluate the argument, aTwoArgBlock for every pair of elements in the collection. |
|
602 | 4714 |
The block is called with 2 arguments for each 2 elements in the receiver. |
4715 |
An error will be reported, if the number of elements in the receiver |
|
4716 |
is not a multiple of 2. |
|
4717 |
Collect the results and return a new collection containing those." |
|
4718 |
||
10150 | 4719 |
^ self inGroupsOf:2 collect:aTwoArgBlock |
44 | 4720 |
|
95 | 4721 |
" |
8874 | 4722 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4723 |
pairWiseCollect:[:num :sym | sym->num] |
8874 | 4724 |
|
4725 |
||
4726 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4727 |
pairWiseCollect:[:x :y | x@y] |
4728 |
" |
|
4729 |
||
4730 |
"Modified: / 27-10-2006 / 10:05:24 / cg" |
|
4731 |
! |
|
4732 |
||
4733 |
pairWiseDo:aTwoArgBlock |
|
10385 | 4734 |
"evaluate the argument, aTwoArgBlock for every group of 2 elements in the collection. |
4735 |
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
|
4736 |
is not a multiple of 2. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4737 |
CONFUSION ATTACK: |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4738 |
this is different from pairWiseDo:. |
406b1590afe8
Merged with trunk r10476
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17732
diff
changeset
|
4739 |
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
|
4740 |
(sigh: but we were first, so they should have adapted...)" |
602 | 4741 |
|
10150 | 4742 |
^ self inGroupsOf:2 do:aTwoArgBlock |
6554
1e25dea82fa0
new #inGroupsOf:do: and #inGroupsOf:collect:
Claus Gittinger <cg@exept.de>
parents:
6507
diff
changeset
|
4743 |
|
602 | 4744 |
" |
8874 | 4745 |
#(1 one 2 two 3 three 4 four 5 five 6 six) |
10150 | 4746 |
pairWiseDo:[:num :sym | Transcript show:num; show:' is: '; showCR:sym] |
8874 | 4747 |
|
4748 |
||
4749 |
#(1 1 1 2 1 3 1 4 1 5) |
|
10150 | 4750 |
pairWiseDo:[:x :y | Transcript showCR:x@y] |
4751 |
" |
|
4752 |
||
10385 | 4753 |
"Modified: / 11-02-2007 / 09:31:29 / cg" |
61 | 4754 |
! |
4755 |
||
602 | 4756 |
reverseDo:aBlock |
4757 |
"evaluate the argument, aBlock for every element in the collection |
|
4758 |
in reverse order" |
|
4759 |
||
4760 |
|sz "{ Class:SmallInteger }"| |
|
4761 |
||
4762 |
sz := self size. |
|
4763 |
sz to:1 by:-1 do:[:index | |
|
8874 | 4764 |
aBlock value:(self at:index). |
602 | 4765 |
] |
4766 |
||
4767 |
" |
|
1422 | 4768 |
#(one two three four five six) reverseDo:[:element | Transcript showCR:element] |
602 | 4769 |
" |
4770 |
! |
|
4771 |
||
4772 |
select:aBlock |
|
4773 |
"evaluate the argument, aBlock for every element in the collection |
|
4774 |
and return a collection of all elements for which the block return |
|
4775 |
true" |
|
4776 |
||
4777 |
|element newColl species needCopy |
|
4778 |
sz "{ Class:SmallInteger }"| |
|
4779 |
||
4780 |
sz := self size. |
|
4781 |
species := self species. |
|
17940 | 4782 |
species growIsCheap ifTrue:[ |
4783 |
newColl := self copyEmpty:sz. |
|
4784 |
needCopy := false |
|
4785 |
] ifFalse:[ |
|
4786 |
newColl := OrderedCollection new:sz. |
|
4787 |
needCopy := true |
|
602 | 4788 |
]. |
4789 |
1 to:sz do:[:index | |
|
17940 | 4790 |
element := self at:index. |
4791 |
(aBlock value:element) ifTrue:[ |
|
4792 |
newColl add:element |
|
4793 |
]. |
|
602 | 4794 |
]. |
4795 |
needCopy ifTrue:[ |
|
17940 | 4796 |
newColl := (species withAll:newColl) postCopyFrom:self |
602 | 4797 |
]. |
61 | 4798 |
^ newColl |
4799 |
||
95 | 4800 |
" |
8874 | 4801 |
#(one two three four five six) select:[:element | element startsWith:'f'] |
4802 |
#(1 2 3 4 5 6 7 8 9) select:[:element | element odd] |
|
4803 |
(#(17 12 1 98 51) asSortedCollection:[:a :b | b < a]) select:[:element | element odd] |
|
4804 |
(1 to:9) select:[:element | element odd] |
|
4805 |
(Smalltalk allClasses) select:[:class | class name startsWith:'S'] |
|
95 | 4806 |
" |
186 | 4807 |
! |
4808 |
||
602 | 4809 |
with:aSequenceableCollection do:aTwoArgBlock |
4810 |
"evaluate the argument, aBlock for successive elements from |
|
4811 |
each the receiver and the argument, aSequenceableCollection. |
|
4812 |
The second argument, aBlock must be a two-argument block. |
|
17845 | 4813 |
The collection argument must implement access via a numeric key |
4814 |
and (new!!) the sizes must be the same." |
|
602 | 4815 |
|
4816 |
|stop "{ Class: SmallInteger }" | |
|
4817 |
||
4818 |
stop := self size. |
|
17845 | 4819 |
aSequenceableCollection size == stop ifFalse:[ |
4820 |
NotEnoughElementsSignal raiseRequestErrorString:'collections must be of the same size'. |
|
4821 |
]. |
|
4822 |
||
602 | 4823 |
1 to:stop do:[:index | |
17845 | 4824 |
aTwoArgBlock value:(self at:index) value:(aSequenceableCollection at:index). |
602 | 4825 |
] |
17845 | 4826 |
|
186 | 4827 |
" |
8874 | 4828 |
#(one two three four five six) |
17845 | 4829 |
with:(1 to:10) |
4830 |
do:[:el1 :el2 | Transcript show:el1; space; showCR:el2] |
|
4831 |
" |
|
4832 |
||
4833 |
"Modified (Format): / 30-06-2011 / 17:39:59 / cg" |
|
9454 | 4834 |
! |
4835 |
||
4836 |
with:aSequenceableCollection doWithIndex:aThreeArgBlock |
|
4837 |
"evaluate the argument, aBlock for successive elements from |
|
4838 |
each the receiver and the argument, aSequenceableCollection. |
|
4839 |
aBlock must be a three-argument block, which get elements from either collection and |
|
4840 |
the index as arguments. |
|
4841 |
The collection argument must implement access via a numeric key." |
|
4842 |
||
4843 |
|stop "{ Class: SmallInteger }" | |
|
4844 |
||
4845 |
stop := self size. |
|
4846 |
1 to:stop do:[:index | |
|
4847 |
aThreeArgBlock |
|
4848 |
value:(self at:index) |
|
4849 |
value:(aSequenceableCollection at:index) |
|
4850 |
value:index. |
|
4851 |
] |
|
17910 | 4852 |
|
9454 | 4853 |
" |
4854 |
#(one two three four five six) |
|
4855 |
with:(1 to:10) |
|
17910 | 4856 |
doWithIndex:[:el1 :el2 :idx| Transcript show:idx; space; show:el1; space; showCR:el2] |
9454 | 4857 |
" |
4858 |
||
4859 |
"Created: / 05-07-2006 / 18:07:11 / fm" |
|
17910 | 4860 |
"Modified (comment): / 08-01-2012 / 17:18:59 / cg" |
1 | 4861 |
! ! |
4862 |
||
4154 | 4863 |
!SequenceableCollection methodsFor:'filling & replacing'! |
1 | 4864 |
|
602 | 4865 |
atAllPut:anObject |
4866 |
"replace all elements of the collection by the argument, anObject. |
|
4867 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4868 |
therefore the change may affect all others referencing the receiver." |
|
4869 |
||
4870 |
self from:1 to:(self size) put:anObject |
|
4871 |
||
4872 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4873 |
args: anObject : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4874 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4875 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4876 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4877 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4878 |
" |
602 | 4879 |
(Array new:10) atAllPut:1 |
4880 |
(String new:10) atAllPut:$a |
|
4881 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4882 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4883 |
"Modified: / 20.5.1998 / 15:14:11 / cg" |
602 | 4884 |
! |
4885 |
||
1 | 4886 |
from:index1 to:index2 put:anObject |
4887 |
"replace the elements from index1 to index2 of the collection |
|
44 | 4888 |
by the argument, anObject. |
4889 |
Notice: This operation modifies the receiver, NOT a copy; |
|
4890 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4891 |
|
4892 |
|index "{ Class: SmallInteger }" |
|
4893 |
end "{ Class: SmallInteger }"| |
|
4894 |
||
4895 |
index := index1. |
|
4896 |
end := index2. |
|
4897 |
[index <= end] whileTrue:[ |
|
8874 | 4898 |
self at:index put:anObject. |
4899 |
index := index + 1 |
|
1 | 4900 |
] |
44 | 4901 |
|
95 | 4902 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4903 |
args: index1 : <integer> |
8874 | 4904 |
index2 : <integer> |
4905 |
anObject : <object> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4906 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4907 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4908 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4909 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4910 |
" |
95 | 4911 |
#(1 2 3 4 5 6 7 8 9 0) from:3 to:6 put:$X |
4912 |
'abcdefghijkl' from:3 to:6 put:$X |
|
4913 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4914 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4915 |
"Modified: / 20.5.1998 / 15:16:52 / cg" |
1 | 4916 |
! |
4917 |
||
3003 | 4918 |
replaceAll:oldObject by:newObject |
4919 |
"backward ST/X compatibility: an alias for #replaceAll:with:. |
|
4920 |
Pleace do no longer use this one; |
|
4921 |
instead use #replaceAll:with: for ST-80 compatibility." |
|
4922 |
||
5872 | 4923 |
<resource:#obsolete> |
4924 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4925 |
self obsoleteMethodWarning:'use #replaceAll:with:'. |
3003 | 4926 |
^ self replaceAll:oldObject with:newObject |
4927 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4928 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4929 |
args: oldObject : <object> |
8874 | 4930 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4931 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4932 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4933 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4934 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4935 |
"Modified: / 20.5.1998 / 15:16:28 / cg" |
3003 | 4936 |
! |
4937 |
||
4938 |
replaceAll:oldObject by:newObject from:startIndex to:stopIndex |
|
4939 |
"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
|
4940 |
Please do no longer use this one; |
3003 | 4941 |
instead use #replaceAll:with:from:to: for ST-80 compatibility." |
4942 |
||
5872 | 4943 |
<resource:#obsolete> |
4944 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4945 |
self obsoleteMethodWarning:'use #replaceAll:with:from:to:'. |
3003 | 4946 |
^ 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
|
4947 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4948 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4949 |
args: oldObject : <object> |
8874 | 4950 |
newObject : <object> |
4951 |
startIndex : <integer> |
|
4952 |
stopIndex : <integer> |
|
4953 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4954 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4955 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4956 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4957 |
"Modified: / 20.5.1998 / 15:16:02 / cg" |
3003 | 4958 |
! |
4959 |
||
2927 | 4960 |
replaceAll:oldObject with:newObject |
44 | 4961 |
"replace all oldObjects by newObject in the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4962 |
|
44 | 4963 |
Notice: This operation modifies the receiver, NOT a copy; |
4964 |
therefore the change may affect all others referencing the receiver." |
|
1 | 4965 |
|
8874 | 4966 |
^ self |
4967 |
replaceAll:oldObject |
|
4968 |
with:newObject |
|
4969 |
from:1 |
|
4970 |
to:self size |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4971 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4972 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4973 |
args: oldObject : <object> |
8874 | 4974 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4975 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4976 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4977 |
" |
362 | 4978 |
|
4979 |
" |
|
8874 | 4980 |
'123123abc123' replaceAll:$1 with:$* |
4981 |
#(1 2 3 4 1 2 3 4) copy replaceAll:1 with:'one' |
|
362 | 4982 |
" |
3468
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 |
"Modified: / 20.5.1998 / 15:26:10 / cg" |
362 | 4985 |
! |
293 | 4986 |
|
2927 | 4987 |
replaceAll:oldObject with:newObject from:startIndex to:stopIndex |
362 | 4988 |
"replace all oldObjects found between startIndex and endIndex, |
4989 |
by newObject in the receiver. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
4990 |
|
362 | 4991 |
Notice: This operation modifies the receiver, NOT a copy; |
4992 |
therefore the change may affect all others referencing the receiver." |
|
4993 |
||
4994 |
|start "{ Class: SmallInteger }" |
|
4995 |
stop "{ Class: SmallInteger }"| |
|
4996 |
||
4997 |
start := startIndex. |
|
4998 |
stop := stopIndex. |
|
4999 |
startIndex to:stopIndex do:[:index | |
|
8874 | 5000 |
(self at:index) = oldObject ifTrue:[ |
5001 |
self at:index put:newObject |
|
5002 |
] |
|
1 | 5003 |
] |
44 | 5004 |
|
95 | 5005 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5006 |
args: oldObject : <object> |
8874 | 5007 |
newObject : <object> |
5008 |
startIndex : <integer> |
|
5009 |
stopIndex : <integer> |
|
3468
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 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5012 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5013 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5014 |
" |
2980 | 5015 |
'123123abc123' replaceAll:$1 with:$* |
5016 |
'123123abc123' replaceAll:$1 with:$* from:1 to:6 |
|
5017 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:4 |
|
95 | 5018 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5019 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5020 |
"Modified: / 20.5.1998 / 15:23:10 / cg" |
95 | 5021 |
! |
5022 |
||
7615 | 5023 |
replaceAllForWhich:aConditionBlock with:newObject |
5024 |
"replace all elements for which aConditionBlock returns true |
|
5025 |
between startIndex and endIndex, by newObject in the receiver. |
|
5026 |
||
5027 |
Notice: This operation modifies the receiver, NOT a copy; |
|
5028 |
therefore the change may affect all others referencing the receiver." |
|
5029 |
||
8874 | 5030 |
self |
5031 |
replaceAllForWhich:aConditionBlock with:newObject |
|
5032 |
from:1 to:(self size) |
|
7615 | 5033 |
|
5034 |
" |
|
5035 |
args: aConditionBlock : <block> |
|
8874 | 5036 |
newObject : <object> |
7615 | 5037 |
|
5038 |
returns: self |
|
5039 |
" |
|
5040 |
! |
|
5041 |
||
5042 |
replaceAllForWhich:aConditionBlock with:newObject from:startIndex to:stopIndex |
|
5043 |
"replace all elements for which aConditionBlock returns true |
|
5044 |
between startIndex and endIndex, by newObject in the receiver. |
|
5045 |
||
5046 |
Notice: This operation modifies the receiver, NOT a copy; |
|
5047 |
therefore the change may affect all others referencing the receiver." |
|
5048 |
||
5049 |
|start "{ Class: SmallInteger }" |
|
5050 |
stop "{ Class: SmallInteger }"| |
|
5051 |
||
5052 |
start := startIndex. |
|
5053 |
stop := stopIndex. |
|
5054 |
startIndex to:stopIndex do:[:index | |
|
8874 | 5055 |
(aConditionBlock value:(self at:index)) ifTrue:[ |
5056 |
self at:index put:newObject |
|
5057 |
] |
|
7615 | 5058 |
] |
5059 |
||
5060 |
" |
|
5061 |
args: aConditionBlock : <block> |
|
8874 | 5062 |
newObject : <object> |
5063 |
startIndex : <integer> |
|
5064 |
stopIndex : <integer> |
|
7615 | 5065 |
|
5066 |
returns: self |
|
5067 |
" |
|
5068 |
! |
|
5069 |
||
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5070 |
replaceAllIdentical:oldObject with:newObject |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5071 |
"replace all oldObjects by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5072 |
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
|
5073 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5074 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5075 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5076 |
|
8874 | 5077 |
^ self |
5078 |
replaceAllIdentical:oldObject |
|
5079 |
with:newObject |
|
5080 |
from:1 |
|
5081 |
to:self size |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5082 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5083 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5084 |
args: oldObject : <object> |
8874 | 5085 |
newObject : <object> |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5086 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5087 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5088 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5089 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5090 |
" |
8874 | 5091 |
#(1 2 3 4 1.0 2.0 3.0 4.0) copy replaceAll:1 with:'one' |
5092 |
#(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
|
5093 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5094 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5095 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5096 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5097 |
replaceAllIdentical:oldObject with:newObject from:startIndex to:stopIndex |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5098 |
"replace all oldObjects found between startIndex and endIndex, |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5099 |
by newObject in the receiver. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5100 |
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
|
5101 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5102 |
Notice: This operation modifies the receiver, NOT a copy; |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5103 |
therefore the change may affect all others referencing the receiver." |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5104 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5105 |
|start "{ Class: SmallInteger }" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5106 |
stop "{ Class: SmallInteger }"| |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5107 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5108 |
start := startIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5109 |
stop := stopIndex. |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5110 |
startIndex to:stopIndex do:[:index | |
8874 | 5111 |
(self at:index) == oldObject ifTrue:[ |
5112 |
self at:index put:newObject |
|
5113 |
] |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5114 |
] |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5115 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5116 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5117 |
args: oldObject : <object> |
8874 | 5118 |
newObject : <object> |
5119 |
startIndex : <integer> |
|
5120 |
stopIndex : <integer> |
|
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5121 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5122 |
returns: self |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5123 |
" |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5124 |
|
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5125 |
" |
8874 | 5126 |
#(1 2 3 4 1 2 3 4) replaceAll:1 with:'one' from:1 to:8 |
5127 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAll:1 with:'one' from:1 to:8 |
|
5128 |
#(1 2 3 4 1.0 2.0 3.0 4.0) replaceAllIdentical:1 with:'one' from:1 to:8 |
|
5962 | 5129 |
" |
5320
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5130 |
! |
bad0dcfeb534
added #replaceAllIdentical:with:
Claus Gittinger <cg@exept.de>
parents:
5275
diff
changeset
|
5131 |
|
3003 | 5132 |
replaceAny:aCollection by:newObject |
5133 |
"backward ST/X compatibility; alias for #replaceAny:with:. |
|
2927 | 5134 |
Pleace do no longer use this one; |
3003 | 5135 |
instead use #replaceAny:with: for naming consistence." |
5136 |
||
5872 | 5137 |
<resource:#obsolete> |
5138 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5139 |
self obsoleteMethodWarning:'use #replaceAny:with:'. |
8874 | 5140 |
^ self replaceAny:aCollection with:newObject from:1 to:self size |
3003 | 5141 |
|
5142 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5143 |
args: aCollection : <colleciton of <object> > |
8874 | 5144 |
newObject : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5145 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5146 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5147 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5148 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5149 |
" |
3003 | 5150 |
'123123abc123' replaceAny:#($1 $2) with:$* |
5151 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
5152 |
" |
|
5153 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5154 |
"Modified: / 20.5.1998 / 15:17:51 / cg" |
602 | 5155 |
! |
5156 |
||
3003 | 5157 |
replaceAny:aCollection by:newObject from:startIndex to:stopIndex |
5158 |
"backward ST/X compatibility; alias for #replaceAny:with:from:to:. |
|
2927 | 5159 |
Pleace do no longer use this one; |
3003 | 5160 |
instead use #replaceAny:with:from:to: for naming consistence." |
8874 | 5161 |
|
5872 | 5162 |
<resource:#obsolete> |
5163 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5164 |
self obsoleteMethodWarning:'use #replaceAny:with:from:to:'. |
8874 | 5165 |
^ self replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
3003 | 5166 |
|
5167 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5168 |
args: aCollection : <colleciton of <object> > |
8874 | 5169 |
newObject : <object> |
5170 |
startIndex : <integer> |
|
5171 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5172 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5173 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5174 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5175 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5176 |
" |
8874 | 5177 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
5178 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
3003 | 5179 |
" |
5180 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5181 |
"Modified: / 20.5.1998 / 15:18:16 / cg" |
2927 | 5182 |
! |
5183 |
||
5184 |
replaceAny:aCollection with:newObject |
|
5185 |
"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
|
5186 |
|
2927 | 5187 |
Notice: This operation modifies the receiver, NOT a copy; |
5188 |
therefore the change may affect all others referencing the receiver." |
|
5189 |
||
8874 | 5190 |
^ self |
10439 | 5191 |
replaceAny:aCollection |
5192 |
with:newObject |
|
5193 |
from:1 |
|
5194 |
to:self size |
|
5195 |
||
5196 |
" |
|
5197 |
#(1 2 3 4 5 6 7 1 2 3 4 5 6 7) copy replaceAny:#(1 3 5 9) with:99 |
|
5198 |
'abcdefgabcdefg' copy replaceAny:'abx' with:$_ |
|
5199 |
" |
|
5200 |
||
5201 |
"Modified: / 06-03-2007 / 17:01:01 / cg" |
|
602 | 5202 |
! |
5203 |
||
2927 | 5204 |
replaceAny:aCollection with:newObject from:startIndex to:stopIndex |
362 | 5205 |
"replace all elements within a range, |
5206 |
which are in contained in aCollection by newObject. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5207 |
|
362 | 5208 |
Notice: This operation modifies the receiver, NOT a copy; |
5209 |
therefore the change may affect all others referencing the receiver." |
|
5210 |
||
5211 |
|start "{ Class: SmallInteger }" |
|
5212 |
stop "{ Class: SmallInteger }"| |
|
5213 |
||
5214 |
start := startIndex. |
|
5215 |
stop := stopIndex. |
|
5216 |
startIndex to:stopIndex do:[:index | |
|
8874 | 5217 |
(aCollection includes:(self at:index)) ifTrue:[ |
5218 |
self at:index put:newObject |
|
5219 |
] |
|
95 | 5220 |
] |
5221 |
||
5222 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5223 |
args: aCollection : <colleciton of <object> > |
8874 | 5224 |
newObject : <object> |
5225 |
startIndex : <integer> |
|
5226 |
stopIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5227 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5228 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5229 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5230 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5231 |
" |
8874 | 5232 |
'123123abc123' replaceAny:#($1 $2) with:$* from:1 to:6 |
5233 |
#('foo' 'bar' 'foo' 'baz' foo 1 2 3) replaceAny:#(foo 1) with:'*' |
|
95 | 5234 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5235 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5236 |
"Modified: / 20.5.1998 / 15:22:43 / cg" |
1 | 5237 |
! |
5238 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5239 |
replaceFrom:startIndex count:numberOfElements with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5240 |
"replace numberOfElements elements in the receiver from startIndex, |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5241 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5242 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5243 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5244 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5245 |
|
8874 | 5246 |
^ self |
5247 |
replaceFrom:startIndex |
|
5248 |
to:(startIndex + numberOfElements - 1) |
|
5249 |
with:replacementCollection |
|
5250 |
startingAt:1 |
|
5251 |
||
5252 |
" |
|
5253 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5254 |
#($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
|
5255 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5256 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5257 |
"Modified: / 20.5.1998 / 15:25:46 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5258 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5259 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5260 |
replaceFrom:startIndex count:numberOfElementsToReplace with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5261 |
"replace numberOfElementsToReplace elements in the receiver from startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5262 |
with elements taken from replacementCollection starting at repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5263 |
|
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5264 |
Notice: This operation modifies the receiver, NOT a copy; |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5265 |
therefore the change may affect all others referencing the receiver." |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5266 |
|
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5267 |
^ self |
8874 | 5268 |
replaceFrom:startIndex |
5269 |
to:(startIndex + numberOfElementsToReplace - 1) |
|
5270 |
with:replacementCollection |
|
5271 |
startingAt:repStartIndex |
|
5272 |
||
5273 |
" |
|
5274 |
'1234567890' replaceFrom:5 count:3 with:'abcdef' startingAt:3 |
|
5275 |
#($a $b $c $d $e) replaceFrom:2 count:3 with:'12345' startingAt:4 |
|
5276 |
#($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
|
5277 |
" |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5278 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5279 |
"Modified: / 20.5.1998 / 15:22:22 / cg" |
2413
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5280 |
! |
a3f91741d7ba
added #replaceFrom:count:* methods
Claus Gittinger <cg@exept.de>
parents:
2411
diff
changeset
|
5281 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5282 |
replaceFrom:startIndex to:stopIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5283 |
"replace elements in the receiver between index startIndex and stopIndex, |
44 | 5284 |
with elements taken from replacementCollection starting at 1. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5285 |
|
44 | 5286 |
Notice: This operation modifies the receiver, NOT a copy; |
5287 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5288 |
|
8874 | 5289 |
^ self |
5290 |
replaceFrom:startIndex |
|
5291 |
to:stopIndex |
|
5292 |
with:replacementCollection |
|
5293 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5294 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5295 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5296 |
args: startIndex : <integer> |
8874 | 5297 |
stopIndex : <integer> |
5298 |
replacementCollection : <colleciton of <object> > |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5299 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5300 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5301 |
" |
61 | 5302 |
|
95 | 5303 |
" |
5304 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' |
|
5305 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' |
|
5306 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5307 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5308 |
"Modified: / 20.5.1998 / 15:25:37 / cg" |
1 | 5309 |
! |
5310 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5311 |
replaceFrom:startIndex to:stopIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5312 |
"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
|
5313 |
with elements taken from replacementCollection starting at repStartIndex. |
3234 | 5314 |
Return the receiver. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5315 |
|
44 | 5316 |
Notice: This operation modifies the receiver, NOT a copy; |
5317 |
therefore the change may affect all others referencing the receiver." |
|
1 | 5318 |
|
5319 |
|srcIndex "{ Class: SmallInteger }" |
|
5320 |
dstIndex "{ Class: SmallInteger }" |
|
5321 |
end "{ Class: SmallInteger }" | |
|
5322 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5323 |
(repStartIndex < startIndex) ifTrue:[ |
8874 | 5324 |
" must do reverse copy " |
5325 |
srcIndex := repStartIndex + (stopIndex - startIndex). |
|
5326 |
dstIndex := stopIndex. |
|
5327 |
end := startIndex. |
|
5328 |
[dstIndex >= end] whileTrue:[ |
|
5329 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
|
5330 |
srcIndex := srcIndex - 1. |
|
5331 |
dstIndex := dstIndex - 1 |
|
5332 |
]. |
|
5333 |
^ self |
|
1 | 5334 |
]. |
5335 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5336 |
srcIndex := repStartIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5337 |
dstIndex := startIndex. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5338 |
end := stopIndex. |
1 | 5339 |
[dstIndex <= end] whileTrue:[ |
8874 | 5340 |
self at:dstIndex put:(replacementCollection at:srcIndex). |
5341 |
srcIndex := srcIndex + 1. |
|
5342 |
dstIndex := dstIndex + 1 |
|
1 | 5343 |
] |
61 | 5344 |
|
95 | 5345 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5346 |
args: startIndex : <integer> |
8874 | 5347 |
stopIndex : <integer> |
5348 |
replacementCollection : <colleciton of <object> > |
|
5349 |
repStartIndex : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5350 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5351 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5352 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5353 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5354 |
" |
8874 | 5355 |
'1234567890' replaceFrom:5 to:7 with:'abcdef' startingAt:3 |
5356 |
#($a $b $c $d $e) replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
5357 |
#($a $b $c $d $e) asOrderedCollection replaceFrom:2 to:3 with:'12345' startingAt:4 |
|
282 | 5358 |
|
5359 |
|c| |
|
5360 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5361 |
c replaceFrom:2 to:3 with:c startingAt:4 |
282 | 5362 |
|
5363 |
|c| |
|
5364 |
c := #($a $b $c $d $e) asOrderedCollection. |
|
8874 | 5365 |
c replaceFrom:4 to:5 with:c startingAt:2 |
95 | 5366 |
" |
3234 | 5367 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5368 |
"Modified: / 20.5.1998 / 15:23:21 / cg" |
61 | 5369 |
! |
5370 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5371 |
replaceFrom:startIndex with:replacementCollection |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5372 |
"replace elements in the receiver starting at startIndex, |
602 | 5373 |
with elements taken from replacementCollection starting at 1 |
5374 |
to the end of replacementCollection. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5375 |
|
602 | 5376 |
Notice: This operation modifies the receiver, NOT a copy; |
5377 |
therefore the change may affect all others referencing the receiver." |
|
5378 |
||
8874 | 5379 |
^ self |
5380 |
replaceFrom:startIndex |
|
5381 |
to:(startIndex + replacementCollection size - 1) |
|
5382 |
with:replacementCollection |
|
5383 |
startingAt:1 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5384 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5385 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5386 |
args: startIndex : <integer> |
8874 | 5387 |
replacementCollection : <colleciton of <object> > |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5388 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5389 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5390 |
" |
602 | 5391 |
|
5392 |
" |
|
5393 |
'1234567890' replaceFrom:5 with:'abc' |
|
5394 |
#($a $b $c $d $e) replaceFrom:2 with:'123' |
|
5395 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5396 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5397 |
"Modified: / 20.5.1998 / 15:25:27 / cg" |
602 | 5398 |
! |
5399 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5400 |
replaceFrom:startIndex with:replacementCollection startingAt:repStartIndex |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5401 |
"replace elements in the receiver starting at startIndex, |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5402 |
with elements taken from replacementCollection starting at repStartIndex |
602 | 5403 |
to the end of replacementCollection. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5404 |
|
602 | 5405 |
Notice: This operation modifies the receiver, NOT a copy; |
5406 |
therefore the change may affect all others referencing the receiver." |
|
5407 |
||
8874 | 5408 |
^ self |
5409 |
replaceFrom:startIndex |
|
5410 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
5411 |
with:replacementCollection |
|
5412 |
startingAt:repStartIndex |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5413 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5414 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5415 |
args: startIndex : <integer> |
8874 | 5416 |
replacementCollection : <colleciton of <object> > |
5417 |
offset : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5418 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5419 |
returns: self |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5420 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5421 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5422 |
" |
8874 | 5423 |
'1234567890' replaceFrom:5 with:'abcdef' startingAt:3 |
602 | 5424 |
#($a $b $c $d $e) replaceFrom:2 with:'12345' startingAt:4 |
5425 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5426 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5427 |
"Modified: / 20.5.1998 / 15:25:18 / cg" |
602 | 5428 |
! |
5429 |
||
61 | 5430 |
startingAt:sourceStart replaceElementsIn:destColl from:destStartIndex to:destEndIndex |
5431 |
"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
|
5432 |
|
61 | 5433 |
Notice: This operation modifies the destination collection, NOT a copy; |
5434 |
therefore the change may affect all others referencing this object." |
|
5435 |
||
8874 | 5436 |
destColl |
5437 |
replaceFrom:destStartIndex |
|
5438 |
to:destEndIndex |
|
5439 |
with:self |
|
5440 |
startingAt:sourceStart |
|
61 | 5441 |
|
5442 |
" |
|
5443 |
|s| |
|
5444 |
s := 'abcdefghijklmnop'. |
|
5445 |
'1234567890' startingAt:1 replaceElementsIn:s from:1 to:3. |
|
5446 |
s'123defghijklmnop' |
|
5447 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5448 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
5449 |
"Modified: / 20.5.1998 / 15:25:08 / cg" |
1 | 5450 |
! ! |
5451 |
||
17940 | 5452 |
!SequenceableCollection methodsFor:'obsolete'! |
5453 |
||
5454 |
randomizedQuickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
5455 |
"actual randomizedQuicksort worker for sort:with:-message. |
|
5456 |
This exchanges a random element within the partition, to avoid |
|
5457 |
the worst case O-square situation of quickSort. |
|
5458 |
||
5459 |
Notice, that this method has a much worse best- and average case |
|
5460 |
runTime, due to the random number generation. |
|
5461 |
The worst case of quickSort is encountered, if the choosen pivot |
|
5462 |
element lies at either end of the partition, so that a split |
|
5463 |
creates partitions of size 1 and (n-1). |
|
5464 |
Since the middle element is choosen, this worst case is very unlikely |
|
5465 |
to be encountered." |
|
5466 |
||
5467 |
<resource:#obsolete> |
|
5468 |
||
5469 |
self obsoleteMethodWarning:'use quickSortFrom:to:sortBlock:with:'. |
|
5470 |
||
5471 |
"/ code was never used - use regular sort for backward compatibility |
|
5472 |
^ self quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
5473 |
||
5474 |
||
5475 |
"/ |begin "{ Class: SmallInteger }" |
|
5476 |
"/ end "{ Class: SmallInteger }" |
|
5477 |
"/ b "{ Class: SmallInteger }" |
|
5478 |
"/ e "{ Class: SmallInteger }" |
|
5479 |
"/ rnd "{ Class: SmallInteger }" |
|
5480 |
"/ middleElement temp1 temp2 | |
|
5481 |
"/ |
|
5482 |
"/ begin := inBegin. "/ this also does a type-check |
|
5483 |
"/ end := inEnd. |
|
5484 |
"/ |
|
5485 |
"/ b := begin. |
|
5486 |
"/ e := end. |
|
5487 |
"/ |
|
5488 |
"/ "/ randomize that partition |
|
5489 |
"/ "/ by exchanging the first element with any random |
|
5490 |
"/ "/ element. |
|
5491 |
"/ rnd := Random nextIntegerBetween:b and:e. |
|
5492 |
"/ temp1 := self at:b. temp2 := self at:rnd. |
|
5493 |
"/ self at:b put:temp2. self at:rnd put:temp1. |
|
5494 |
"/ aCollection notNil ifTrue:[ |
|
5495 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
|
5496 |
"/ aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
|
5497 |
"/ ]. |
|
5498 |
"/ |
|
5499 |
"/ "/ |
|
5500 |
"/ "/ now proceed as usual |
|
5501 |
"/ "/ |
|
5502 |
"/ middleElement := self at:((b + e) // 2). |
|
5503 |
"/ |
|
5504 |
"/ [b < e] whileTrue:[ |
|
5505 |
"/ [b < end and:[sortBlock value:(self at:b) value:middleElement]] whileTrue:[b := b + 1]. |
|
5506 |
"/ [e > begin and:[sortBlock value:middleElement value:(self at:e)]] whileTrue:[e := e - 1]. |
|
5507 |
"/ |
|
5508 |
"/ (b <= e) ifTrue:[ |
|
5509 |
"/ (b == e) ifFalse:[ |
|
5510 |
"/ temp1 := self at:b. temp2 := self at:e. |
|
5511 |
"/ self at:b put:temp2. self at:e put:temp1. |
|
5512 |
"/ aCollection notNil ifTrue:[ |
|
5513 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5514 |
"/ aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5515 |
"/ ]. |
|
5516 |
"/ ]. |
|
5517 |
"/ b := b + 1. |
|
5518 |
"/ e := e - 1 |
|
5519 |
"/ ] |
|
5520 |
"/ ]. |
|
5521 |
"/ (begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e sortBlock:sortBlock with:aCollection]. |
|
5522 |
"/ (b < end) ifTrue:[self randomizedQuickSortFrom:b to:end sortBlock:sortBlock with:aCollection] |
|
5523 |
! |
|
5524 |
||
5525 |
randomizedQuickSortFrom:inBegin to:inEnd with:aCollection |
|
5526 |
"actual randomizedQuicksort worker for randomizedSort:-message. |
|
5527 |
This exchanges a random element within the partition, to avoid |
|
5528 |
the worst case O-square situation of quickSort. |
|
5529 |
||
5530 |
Notice, that this method has a much worse best- and average case |
|
5531 |
runTime, due to the random number generation. |
|
5532 |
The worst case of quickSort is encountered, if the choosen pivot |
|
5533 |
element lies at either end of the partition, so that a split |
|
5534 |
creates partitions of size 1 and (n-1). |
|
5535 |
Since the middle element is choosen, this worst case is very unlikely |
|
5536 |
to be encountered." |
|
5537 |
||
5538 |
<resource:#obsolete> |
|
5539 |
||
5540 |
self obsoleteMethodWarning:'use quickSortFrom:to:with:'. |
|
5541 |
"/ code was never used - use regular sort for backward compatibility |
|
5542 |
^ self quickSortFrom:inBegin to:inEnd with:aCollection |
|
5543 |
||
5544 |
||
5545 |
"/ |begin "{ Class: SmallInteger }" |
|
5546 |
"/ end "{ Class: SmallInteger }" |
|
5547 |
"/ b "{ Class: SmallInteger }" |
|
5548 |
"/ e "{ Class: SmallInteger }" |
|
5549 |
"/ rnd "{ Class: SmallInteger }" |
|
5550 |
"/ middleElement temp1 temp2 | |
|
5551 |
"/ |
|
5552 |
"/ begin := inBegin. "/ this also does a type-check |
|
5553 |
"/ end := inEnd. |
|
5554 |
"/ |
|
5555 |
"/ b := begin. |
|
5556 |
"/ e := end. |
|
5557 |
"/ |
|
5558 |
"/ "/ randomize that partition |
|
5559 |
"/ "/ by exchanging the first element with any random |
|
5560 |
"/ "/ element. |
|
5561 |
"/ rnd := Random nextIntegerBetween:b and:e. |
|
5562 |
"/ temp1 := self at:b. temp2 := self at:rnd. |
|
5563 |
"/ self at:b put:temp2. self at:rnd put:temp1. |
|
5564 |
"/ aCollection notNil ifTrue:[ |
|
5565 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:rnd. |
|
5566 |
"/ aCollection at:b put:temp2. aCollection at:rnd put:temp1 |
|
5567 |
"/ ]. |
|
5568 |
"/ |
|
5569 |
"/ "/ |
|
5570 |
"/ "/ now proceed as usual |
|
5571 |
"/ "/ |
|
5572 |
"/ middleElement := self at:((b + e) // 2). |
|
5573 |
"/ |
|
5574 |
"/ [b < e] whileTrue:[ |
|
5575 |
"/ [b < end and:[(self at:b) < middleElement]] whileTrue:[b := b + 1]. |
|
5576 |
"/ [e > begin and:[middleElement < (self at:e)]] whileTrue:[e := e - 1]. |
|
5577 |
"/ |
|
5578 |
"/ (b <= e) ifTrue:[ |
|
5579 |
"/ (b == e) ifFalse:[ |
|
5580 |
"/ temp1 := self at:b. temp2 := self at:e. |
|
5581 |
"/ self at:b put:temp2. self at:e put:temp1. |
|
5582 |
"/ aCollection notNil ifTrue:[ |
|
5583 |
"/ temp1 := aCollection at:b. temp2 := aCollection at:e. |
|
5584 |
"/ aCollection at:b put:temp2. aCollection at:e put:temp1 |
|
5585 |
"/ ]. |
|
5586 |
"/ ]. |
|
5587 |
"/ b := b + 1. |
|
5588 |
"/ e := e - 1 |
|
5589 |
"/ ] |
|
5590 |
"/ ]. |
|
5591 |
"/ (begin < e) ifTrue:[self randomizedQuickSortFrom:begin to:e with:aCollection]. |
|
5592 |
"/ (b < end) ifTrue:[self randomizedQuickSortFrom:b to:end with:aCollection] |
|
5593 |
"/ |
|
5594 |
"/ "Modified: 21.8.1997 / 18:30:19 / cg" |
|
5595 |
"/ "Created: 21.8.1997 / 18:34:23 / cg" |
|
5596 |
! |
|
5597 |
||
5598 |
randomizedSort |
|
5599 |
<resource: #obsolete> |
|
5600 |
"sort the collection inplace. The elements are compared using |
|
5601 |
'<' i.e. they should offer a magnitude-like protocol. |
|
5602 |
||
5603 |
This uses the randomized quicksort algorithm, |
|
5604 |
which has a better worstCase behavior than quickSort |
|
5605 |
(bit worse bestCase & averageCase behavior). |
|
5606 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
5607 |
||
5608 |
"/ |stop| |
|
5609 |
"/ |
|
5610 |
"/ stop := self size. |
|
5611 |
"/ (stop > 1) ifTrue:[ |
|
5612 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:nil |
|
5613 |
"/ ] |
|
5614 |
||
5615 |
"/ code was never used - use regular sort for backward compatibility |
|
5616 |
^ self sort |
|
5617 |
||
5618 |
! |
|
5619 |
||
5620 |
randomizedSort:sortBlock |
|
5621 |
<resource: #obsolete> |
|
5622 |
"sort the collection inplace using the 2-arg block sortBlock |
|
5623 |
for comparison. This allows any sort criteria to be implemented. |
|
5624 |
||
5625 |
This uses the randomized quicksort algorithm, |
|
5626 |
which has a better worstCase behavior than quickSort |
|
5627 |
(bit worse bestCase & averageCase behavior). |
|
5628 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
5629 |
||
5630 |
"/ |stop| |
|
5631 |
"/ |
|
5632 |
"/ stop := self size. |
|
5633 |
"/ (stop > 1) ifTrue:[ |
|
5634 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:nil |
|
5635 |
"/ ] |
|
5636 |
||
5637 |
"/ code was never used - use regular sort for backward compatibility |
|
5638 |
^ self sort:sortBlock |
|
5639 |
||
5640 |
! |
|
5641 |
||
5642 |
randomizedSort:sortBlock with:aCollection |
|
5643 |
<resource: #obsolete> |
|
5644 |
"sort the collection inplace using the 2-arg block sortBlock |
|
5645 |
for comparison. Also reorder the elements in aCollection. |
|
5646 |
Use this, when you have a key collection to sort some other collection with. |
|
5647 |
||
5648 |
This uses the randomized quicksort algorithm, |
|
5649 |
which has a better worstCase behavior than quickSort |
|
5650 |
(bit worse bestCase & averageCase behavior). |
|
5651 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
5652 |
||
5653 |
"/ |stop| |
|
5654 |
"/ |
|
5655 |
"/ stop := self size. |
|
5656 |
"/ (stop > 1) ifTrue:[ |
|
5657 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:sortBlock with:aCollection |
|
5658 |
"/ ] |
|
5659 |
||
5660 |
"/ code was never used - use regular sort for backward compatibility |
|
5661 |
^ self sort:sortBlock with:aCollection |
|
5662 |
||
5663 |
! |
|
5664 |
||
5665 |
randomizedSortWith:aCollection |
|
5666 |
<resource: #obsolete> |
|
5667 |
"sort the receiver collection inplace, using '<' to compare elements. |
|
5668 |
Also, the elements of aCollection are reordered with it. |
|
5669 |
Use this, when you have a key collection to sort another collection with. |
|
5670 |
||
5671 |
This uses the randomized quicksort algorithm, |
|
5672 |
which has a better worstCase behavior than quickSort |
|
5673 |
(bit worse bestCase & averageCase behavior). |
|
5674 |
See: Knuth or Cormen,Leiserson,Rivest pg. 163" |
|
5675 |
||
5676 |
"/ |stop| |
|
5677 |
"/ |
|
5678 |
"/ stop := self size. |
|
5679 |
"/ (stop > 1) ifTrue:[ |
|
5680 |
"/ self randomizedQuickSortFrom:1 to:stop sortBlock:[:a :b | a < b] with:aCollection |
|
5681 |
"/ ] |
|
5682 |
||
5683 |
"/ code was never used - use regular sort for backward compatibility |
|
5684 |
^ self sortWith:aCollection |
|
5685 |
||
5686 |
! ! |
|
5687 |
||
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5688 |
!SequenceableCollection methodsFor:'padded copying'! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5689 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5690 |
leftPaddedTo:size with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5691 |
"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
|
5692 |
right-adjusted (i.e. padded on the left). |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5693 |
Elements on the left are filled with padElement. |
8874 | 5694 |
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
|
5695 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5696 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5697 |
|len s| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5698 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5699 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5700 |
(len < size) ifTrue:[ |
8874 | 5701 |
s := self species new:size withAll:padElement. |
5702 |
s replaceFrom:(size - len + 1) with:self. |
|
5703 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5704 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5705 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5706 |
" |
8874 | 5707 |
'foo' leftPaddedTo:10 with:$. |
5708 |
'fooBar' leftPaddedTo:5 with:$. |
|
5709 |
123 printString leftPaddedTo:10 with:$. |
|
5710 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
5711 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
5712 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
5713 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
5714 |
#[1 2 3 4] leftPaddedTo:6 with:0 |
|
5715 |
#[1 2 3 4] leftPaddedTo:10 with:99 |
|
5716 |
#(1 2 3 4) leftPaddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5717 |
" |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5718 |
! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5719 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5720 |
paddedTo:newSize with:padElement |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5721 |
"return a new collection consisting of the receivers elements, |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5722 |
plus pad elements up to length. |
8874 | 5723 |
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
|
5724 |
the original receiver is returned unchanged." |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5725 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5726 |
|s len| |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5727 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5728 |
len := self size. |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5729 |
len < newSize ifTrue:[ |
8874 | 5730 |
s := self species new:newSize withAll:padElement. |
5731 |
s replaceFrom:1 to:len with:self. |
|
5732 |
^ s |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5733 |
] |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5734 |
|
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5735 |
" |
8874 | 5736 |
'foo' paddedTo:10 with:$. |
5737 |
123 printString paddedTo:10 with:$* |
|
5738 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
5739 |
(Float pi printString) paddedTo:15 with:$- |
|
5740 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
5741 |
#[1 2 3 4] paddedTo:6 with:0 |
|
5742 |
#[1 2 3 4] paddedTo:10 with:99 |
|
5743 |
#(1 2 3 4) paddedTo:8 with:nil |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5744 |
" |
7116 | 5745 |
! |
5746 |
||
5747 |
paddedToMultipleOf:sizeModulu with:padElement |
|
5748 |
|mySize mod numPad| |
|
5749 |
||
5750 |
mySize := self size. |
|
5751 |
mod := (mySize \\ sizeModulu). |
|
5752 |
mod == 0 ifTrue:[^ self]. |
|
5753 |
||
5754 |
numPad := sizeModulu - mod. |
|
5755 |
^ self , (self species new:numPad withAll:padElement). |
|
5756 |
||
5757 |
" |
|
8874 | 5758 |
#[] paddedToMultipleOf:3 with:0 |
5759 |
#[1] paddedToMultipleOf:3 with:0 |
|
5760 |
#[1 2] paddedToMultipleOf:3 with:0 |
|
5761 |
#[1 2 3] paddedToMultipleOf:3 with:0 |
|
7116 | 5762 |
#[1 2 3 4] paddedToMultipleOf:3 with:0 |
5763 |
||
5764 |
#() paddedToMultipleOf:3 with:nil |
|
5765 |
#(1) paddedToMultipleOf:3 with:nil |
|
5766 |
#(1 2) paddedToMultipleOf:3 with:nil |
|
5767 |
#(1 2 3) paddedToMultipleOf:3 with:nil |
|
5768 |
#(1 2 3 4) paddedToMultipleOf:3 with:nil |
|
5769 |
||
5770 |
'' paddedToMultipleOf:3 with:Character space |
|
5771 |
'a' paddedToMultipleOf:3 with:Character space |
|
5772 |
'ab' paddedToMultipleOf:3 with:Character space |
|
5773 |
'abc' paddedToMultipleOf:3 with:Character space |
|
5774 |
'abcd' paddedToMultipleOf:3 with:Character space |
|
5775 |
" |
|
5153
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5776 |
! ! |
dc159974af2b
paddedCopy methods moved from CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5004
diff
changeset
|
5777 |
|
7260 | 5778 |
!SequenceableCollection methodsFor:'private-sorting helpers'! |
2069 | 5779 |
|
17938 | 5780 |
mergeFirst:first middle:middle last:last into:dst by:aBlock |
8874 | 5781 |
"Private!! |
5358 | 5782 |
Merge the sorted ranges [first..middle] and [middle+1..last] of the receiver into the range [first..last] of dst." |
17938 | 5783 |
|
5784 |
|i1 i2 val1 val2 out| |
|
5358 | 5785 |
|
5786 |
i1 := first. |
|
17938 | 5787 |
out := first. |
5358 | 5788 |
i2 := middle + 1. |
17938 | 5789 |
val1 := self at:i1. |
5790 |
||
5791 |
i2 <= last ifTrue:[ |
|
5792 |
val2 := self at:i2. |
|
5793 |
"select 'lower' half of the elements based on comparator" |
|
5794 |
[(i1 <= middle) and:[i2 <= last]] whileTrue:[ |
|
5795 |
"this is stable if #< or #> ist used for comparison (and not #<= or #>=)" |
|
5796 |
(aBlock value:val2 value:val1) ifTrue:[ |
|
5797 |
dst at:out put:val2. |
|
5798 |
i2 := i2 + 1. |
|
5799 |
i2 <= last ifTrue:[ |
|
5800 |
val2 := self at:i2 |
|
5801 |
] |
|
5802 |
] ifFalse:[ |
|
5803 |
dst at:out put:val1. |
|
5804 |
i1 := i1 + 1. |
|
5805 |
val1 := self at:i1. |
|
5806 |
]. |
|
5807 |
out := out + 1. |
|
5808 |
]. |
|
5809 |
]. |
|
5358 | 5810 |
|
5811 |
"copy the remaining elements" |
|
17938 | 5812 |
i1 <= middle ifTrue:[ |
5813 |
dst |
|
5814 |
replaceFrom:out |
|
5815 |
to:last |
|
5816 |
with:self |
|
5817 |
startingAt:i1 |
|
5818 |
] ifFalse:[ |
|
5819 |
dst |
|
5820 |
replaceFrom:out |
|
5821 |
to:last |
|
5822 |
with:self |
|
5823 |
startingAt:i2 |
|
5824 |
]. |
|
5825 |
! |
|
5826 |
||
5827 |
mergeSortFrom: first to: last by: aBlock |
|
5358 | 5828 |
"Private!! Split the range to be sorted in half, sort each half, and merge the two half-ranges into dst." |
5829 |
||
17938 | 5830 |
|size chunkSize idx nextIdx finished temp src dst| |
5831 |
||
5832 |
size := last-first + 1. |
|
5833 |
size <= 1 ifTrue:[^ self]. |
|
5834 |
||
5835 |
"size of the base chunks" |
|
5836 |
chunkSize := 12. |
|
5837 |
idx := first. |
|
5838 |
finished := false. |
|
5839 |
[finished] whileFalse:[ |
|
5840 |
nextIdx := idx + chunkSize. |
|
5841 |
nextIdx > last ifTrue:[ |
|
5842 |
nextIdx := last+1. |
|
5843 |
finished := true. |
|
5844 |
]. |
|
5845 |
self insertionSort:aBlock from:idx to:nextIdx-1. |
|
5846 |
idx := nextIdx. |
|
5847 |
]. |
|
5848 |
||
5849 |
chunkSize < size ifTrue:[ |
|
5850 |
"now merge the chunks" |
|
5851 |
dst := Array new:self size. |
|
5852 |
src := self. |
|
5853 |
||
5854 |
[chunkSize < size] whileTrue:[ |
|
5855 |
"merge pairs of adjecant chunks" |
|
5856 |
idx := first. |
|
5857 |
finished := false. |
|
5858 |
[finished] whileFalse:[ |
|
5859 |
nextIdx := idx + (2*chunkSize). |
|
5860 |
nextIdx > last ifTrue:[ |
|
5861 |
nextIdx := last+1. |
|
5862 |
finished := true. |
|
5863 |
]. |
|
5864 |
src |
|
5865 |
mergeFirst:idx |
|
5866 |
middle:idx+chunkSize-1 |
|
5867 |
last:nextIdx-1 |
|
5868 |
into:dst |
|
5869 |
by:aBlock. |
|
5870 |
idx := nextIdx. |
|
5871 |
]. |
|
5872 |
||
5873 |
chunkSize := chunkSize * 2. |
|
5874 |
||
5875 |
"merged chunks are in dst. Swap source and destination for next step" |
|
5876 |
temp := dst. |
|
5877 |
dst := src. |
|
5878 |
src := temp. |
|
5879 |
]. |
|
5880 |
"note, the latest dst is now src (see above)" |
|
5881 |
src ~~ self ifTrue:[ |
|
5882 |
self replaceFrom:first to:last with:src startingAt:first. |
|
5883 |
]. |
|
5884 |
]. |
|
5358 | 5885 |
! |
5886 |
||
2069 | 5887 |
quickSortFrom:inBegin to:inEnd |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5888 |
"actual quicksort worker for sort-message. |
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
5889 |
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
|
5890 |
with degenerated collections. |
7c346c8015f2
Only use #> for comparisons, since this is the base mechanism.
Stefan Vogel <sv@exept.de>
parents:
8099
diff
changeset
|
5891 |
|
17938 | 5892 |
The algorithm has been extended to introSort, which is quickSort with fallBack |
5893 |
when we find out, that we have a worst case quick sort with O(n*n). |
|
5894 |
||
5895 |
Use #< for element comparisons, since this is the (fastest) base |
|
5896 |
method in Magnitude, and the others may be defined by sending #<." |
|
5897 |
||
5898 |
|begin "{ Class: SmallInteger }" |
|
5899 |
end "{ Class: SmallInteger }" |
|
5900 |
bRun "{ Class: SmallInteger }" |
|
5901 |
eRun "{ Class: SmallInteger }" |
|
5902 |
m "{ Class: SmallInteger }" |
|
5903 |
prevIdx "{ Class: SmallInteger }" |
|
5904 |
elB elM elE temp stack depthLimit| |
|
5905 |
||
5906 |
inEnd <= inBegin ifTrue:[ |
|
5907 |
"nothing to sort" |
|
5908 |
^ self. |
|
5909 |
]. |
|
5910 |
||
17940 | 5911 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
5912 |
||
5913 |
stack := OrderedCollection new:depthLimit*2. |
|
5914 |
begin := inBegin. |
|
5915 |
end := inEnd. |
|
5916 |
||
5917 |
[true] whileTrue:[ |
|
5918 |
"do not sort small chunks, |
|
5919 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
5920 |
end - begin > 12 ifTrue:[ |
|
5921 |
"ok, her we do the real quickSort..." |
|
5922 |
depthLimit <= 0 ifTrue:[ |
|
5923 |
"this is apparently a degenerated quickSort |
|
5924 |
- abort the quicksort and fall back to heapSort |
|
5925 |
which has O(n * log n) complexity in the worst case" |
|
5926 |
self heapSortFrom:begin to:end. |
|
5927 |
begin := end+1. |
|
5928 |
] ifFalse:[ |
|
5929 |
elB := self at:begin. |
|
5930 |
elE := self at:end. |
|
5931 |
m := (begin + end) // 2. |
|
5932 |
elM := self at:m. |
|
5933 |
"take the median of three as pivot (elM)" |
|
5934 |
(elM < elB) ifTrue:[ |
|
5935 |
temp := self at:begin put:elM. |
|
5936 |
elM := self at:m put:elB. |
|
5937 |
elB := temp. |
|
17938 | 5938 |
]. |
17940 | 5939 |
(elE < elB) ifTrue:[ |
5940 |
self at:begin put:elE. |
|
5941 |
elE := self at:end put:elB. |
|
5942 |
]. |
|
5943 |
(elE < elM) ifTrue:[ |
|
5944 |
self at:end put:elM. |
|
5945 |
elM := self at:m put:elE. |
|
17938 | 5946 |
]. |
17940 | 5947 |
|
5948 |
bRun := begin. |
|
5949 |
eRun := end. |
|
5950 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
5951 |
so STC can optimize" |
|
5952 |
[bRun < eRun] whileTrue:[ |
|
5953 |
[(bRun := bRun+1) <= eRun and:[(self at:bRun) < elM]] whileTrue. |
|
5954 |
[bRun <= (eRun := eRun-1) and:[elM < (self at:eRun)]] whileTrue. |
|
5955 |
||
5956 |
(bRun < eRun) ifTrue:[ |
|
5957 |
temp := self at:bRun. |
|
5958 |
self at:bRun put:(self at:eRun). |
|
5959 |
self at:eRun put:temp. |
|
5960 |
]. |
|
5961 |
]. |
|
5962 |
(bRun < end) ifTrue:[ |
|
5963 |
"remember right part for later processing" |
|
5964 |
stack add:end. |
|
5965 |
stack add:depthLimit-1. |
|
5966 |
]. |
|
5967 |
||
5968 |
"now sort the left part from begin .. new end" |
|
5969 |
depthLimit := depthLimit - 1. |
|
5970 |
end := eRun. |
|
17938 | 5971 |
]. |
5972 |
] ifFalse:[ |
|
17940 | 5973 |
stack isEmpty ifTrue:[ |
5974 |
"we are done. |
|
5975 |
Do a final insertion sort over all the elements, |
|
5976 |
to eventually sort the small, unsorted chunks" |
|
5977 |
begin := inBegin. |
|
5978 |
bRun := begin + 1. |
|
5979 |
end := inEnd. |
|
5980 |
bRun to:end do:[:idx| |
|
5981 |
temp := self at:idx. |
|
5982 |
prevIdx := idx-1. |
|
5983 |
[prevIdx >= begin and:[temp < (self at:prevIdx)]] whileTrue:[ |
|
5984 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
5985 |
prevIdx := prevIdx - 1. |
|
5986 |
]. |
|
5987 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
5988 |
self at:prevIdx+1 put:temp. |
|
5989 |
]. |
|
17938 | 5990 |
]. |
17940 | 5991 |
^ self |
17938 | 5992 |
]. |
17940 | 5993 |
begin := end + 1. |
5994 |
depthLimit := stack removeLast. |
|
5995 |
end := stack removeLast. |
|
17938 | 5996 |
]. |
17940 | 5997 |
]. |
17938 | 5998 |
! |
5999 |
||
6000 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
|
6001 |
"actual quicksort worker for sort-message. |
|
6002 |
Simulates recursion in a stack, to avoid recursion overflow |
|
6003 |
with degenerated collections. |
|
6004 |
||
6005 |
The algorithm has been extended to introSort, which is quickSort with fallBack |
|
6006 |
when we find out, that we have a worst case quick sort with O(n*n). |
|
6007 |
||
6008 |
Use sortBlock for element comparisons." |
|
6009 |
||
6010 |
|begin "{ Class: SmallInteger }" |
|
6011 |
end "{ Class: SmallInteger }" |
|
6012 |
bRun "{ Class: SmallInteger }" |
|
6013 |
eRun "{ Class: SmallInteger }" |
|
6014 |
m "{ Class: SmallInteger }" |
|
6015 |
prevIdx "{ Class: SmallInteger }" |
|
6016 |
elB elM elE temp stack depthLimit| |
|
6017 |
||
6018 |
inEnd <= inBegin ifTrue:[ |
|
6019 |
"nothing to sort" |
|
6020 |
^ self. |
|
6021 |
]. |
|
6022 |
||
17940 | 6023 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
6024 |
||
6025 |
stack := OrderedCollection new:depthLimit*2. |
|
6026 |
begin := inBegin. |
|
6027 |
end := inEnd. |
|
6028 |
||
6029 |
[true] whileTrue:[ |
|
6030 |
"do not sort small chunks, |
|
6031 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
6032 |
end - begin > 12 ifTrue:[ |
|
6033 |
"ok, her we do the real quickSort..." |
|
6034 |
depthLimit <= 0 ifTrue:[ |
|
6035 |
"this is apparently a degenerated quickSort |
|
6036 |
- abort the quicksort and fall back to heapSort |
|
6037 |
which has O(n * log n) complexity in the worst case" |
|
6038 |
self heapSort:sortBlock from:begin to:end. |
|
6039 |
begin := end+1. |
|
6040 |
] ifFalse:[ |
|
6041 |
elB := self at:begin. |
|
6042 |
elE := self at:end. |
|
6043 |
m := (begin + end) // 2. |
|
6044 |
elM := self at:m. |
|
6045 |
"take the median of three as pivot (elM)" |
|
6046 |
(sortBlock value:elM value:elB) ifTrue:[ |
|
6047 |
temp := self at:begin put:elM. |
|
6048 |
elM := self at:m put:elB. |
|
6049 |
elB := temp. |
|
17938 | 6050 |
]. |
17940 | 6051 |
(sortBlock value:elE value:elB) ifTrue:[ |
6052 |
self at:begin put:elE. |
|
6053 |
elE := self at:end put:elB. |
|
6054 |
]. |
|
6055 |
(sortBlock value:elE value:elM) ifTrue:[ |
|
6056 |
self at:end put:elM. |
|
6057 |
elM := self at:m put:elE. |
|
17938 | 6058 |
]. |
17940 | 6059 |
|
6060 |
bRun := begin. |
|
6061 |
eRun := end. |
|
6062 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
6063 |
so STC can optimize" |
|
6064 |
[bRun < eRun] whileTrue:[ |
|
6065 |
[(bRun := bRun+1) <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
6066 |
[bRun <= (eRun := eRun-1) and:[sortBlock value:elM value:(self at:eRun)]] whileTrue. |
|
6067 |
||
6068 |
(bRun < eRun) ifTrue:[ |
|
6069 |
temp := self at:bRun. |
|
6070 |
self at:bRun put:(self at:eRun). |
|
6071 |
self at:eRun put:temp. |
|
6072 |
]. |
|
6073 |
]. |
|
6074 |
(bRun < end) ifTrue:[ |
|
6075 |
"remember right part for later processing" |
|
6076 |
stack add:end. |
|
6077 |
stack add:depthLimit-1. |
|
6078 |
]. |
|
6079 |
||
6080 |
"now sort the left part from begin .. new end" |
|
6081 |
depthLimit := depthLimit - 1. |
|
6082 |
end := eRun. |
|
6083 |
] |
|
17938 | 6084 |
] ifFalse:[ |
17940 | 6085 |
stack isEmpty ifTrue:[ |
6086 |
"we are done. |
|
6087 |
Do a final insertion sort over all the elements, |
|
6088 |
to eventually sort the small, unsorted chunks" |
|
6089 |
begin := inBegin. |
|
6090 |
bRun := begin + 1. |
|
6091 |
end := inEnd. |
|
6092 |
bRun to:end do:[:idx| |
|
6093 |
temp := self at:idx. |
|
6094 |
prevIdx := idx-1. |
|
6095 |
[prevIdx >= begin and:[sortBlock value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
6096 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
6097 |
prevIdx := prevIdx - 1. |
|
6098 |
]. |
|
6099 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
6100 |
self at:prevIdx+1 put:temp. |
|
6101 |
]. |
|
17938 | 6102 |
]. |
17940 | 6103 |
^ self |
17938 | 6104 |
]. |
17940 | 6105 |
begin := end + 1. |
6106 |
depthLimit := stack removeLast. |
|
6107 |
end := stack removeLast. |
|
17938 | 6108 |
]. |
6109 |
] |
|
6110 |
! |
|
6111 |
||
6112 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock policy:policy |
|
6113 |
"actual quicksort worker for sort-message. |
|
6114 |
Simulates recursion in a stack, to avoid recursion overflow |
|
6115 |
with degenerated collections. |
|
6116 |
||
6117 |
Use sortBlock for element comparisons." |
|
6118 |
||
6119 |
|begin "{ Class: SmallInteger }" |
|
6120 |
end "{ Class: SmallInteger }" |
|
6121 |
bRun "{ Class: SmallInteger }" |
|
6122 |
eRun "{ Class: SmallInteger }" |
|
6123 |
m "{ Class: SmallInteger }" |
|
6124 |
prevIdx "{ Class: SmallInteger }" |
|
17940 | 6125 |
elB elM elE temp stack depthLimit policySortBlock| |
17938 | 6126 |
|
6127 |
inEnd <= inBegin ifTrue:[ |
|
6128 |
"nothing to sort" |
|
6129 |
^ self. |
|
6130 |
]. |
|
6131 |
||
17940 | 6132 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
6133 |
||
6134 |
stack := OrderedCollection new:depthLimit*2. |
|
6135 |
begin := inBegin. |
|
6136 |
end := inEnd. |
|
6137 |
||
6138 |
[true] whileTrue:[ |
|
6139 |
"do not sort small chunks, |
|
6140 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
6141 |
end - begin > 12 ifTrue:[ |
|
6142 |
"ok, her we do the real quickSort..." |
|
6143 |
depthLimit <= 0 ifTrue:[ |
|
6144 |
"this is apparently a degenerated quickSort |
|
6145 |
- abort the quicksort and fall back to heapSort |
|
6146 |
which has O(n * log n) complexity in the worst case" |
|
6147 |
policySortBlock := [:arg1 :arg2| sortBlock value:policy value:arg1 value:arg2]. |
|
6148 |
self heapSort:policySortBlock from:begin to:end. |
|
6149 |
begin := end+1. |
|
6150 |
] ifFalse:[ |
|
6151 |
elB := self at:begin. |
|
6152 |
elE := self at:end. |
|
6153 |
m := (begin + end) // 2. |
|
6154 |
elM := self at:m. |
|
6155 |
"take the median of three as pivot (elM)" |
|
6156 |
(sortBlock value:policy value:elM value:elB) ifTrue:[ |
|
6157 |
temp := self at:begin put:elM. |
|
6158 |
elM := self at:m put:elB. |
|
6159 |
elB := temp. |
|
6160 |
]. |
|
6161 |
(sortBlock value:policy value:elE value:elB) ifTrue:[ |
|
6162 |
self at:begin put:elE. |
|
6163 |
elE := self at:end put:elB. |
|
6164 |
]. |
|
6165 |
(sortBlock value:policy value:elE value:elM) ifTrue:[ |
|
6166 |
self at:end put:elM. |
|
6167 |
elM := self at:m put:elE. |
|
6168 |
]. |
|
6169 |
||
6170 |
bRun := begin. |
|
6171 |
eRun := end. |
|
6172 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
6173 |
so STC can optimize" |
|
6174 |
[bRun < eRun] whileTrue:[ |
|
6175 |
[(bRun := bRun+1) <= eRun and:[sortBlock value:policy value:(self at:bRun) value:elM]] whileTrue. |
|
6176 |
[bRun <= (eRun := eRun-1) and:[sortBlock value:policy value:elM value:(self at:eRun)]] whileTrue. |
|
6177 |
||
6178 |
(bRun < eRun) ifTrue:[ |
|
6179 |
temp := self at:bRun. |
|
6180 |
self at:bRun put:(self at:eRun). |
|
6181 |
self at:eRun put:temp. |
|
6182 |
]. |
|
17938 | 6183 |
]. |
17940 | 6184 |
(bRun < end) ifTrue:[ |
6185 |
"remember right part for later processing" |
|
6186 |
stack add:end. |
|
6187 |
stack add:depthLimit-1. |
|
6188 |
]. |
|
6189 |
||
6190 |
"now sort the left part from begin .. new end" |
|
6191 |
depthLimit := depthLimit - 1. |
|
6192 |
end := eRun. |
|
17938 | 6193 |
]. |
6194 |
] ifFalse:[ |
|
17940 | 6195 |
stack isEmpty ifTrue:[ |
6196 |
"we are done. |
|
6197 |
Do a final insertion sort over all the elements, |
|
6198 |
to eventually sort the small, unsorted chunks" |
|
6199 |
begin := inBegin. |
|
6200 |
bRun := begin + 1. |
|
6201 |
end := inEnd. |
|
6202 |
bRun to:end do:[:idx| |
|
6203 |
temp := self at:idx. |
|
6204 |
prevIdx := idx-1. |
|
6205 |
[prevIdx >= begin and:[sortBlock value:policy value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
6206 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
6207 |
prevIdx := prevIdx - 1. |
|
6208 |
]. |
|
6209 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
6210 |
self at:prevIdx+1 put:temp. |
|
6211 |
]. |
|
17938 | 6212 |
]. |
17940 | 6213 |
^ self |
17938 | 6214 |
]. |
17940 | 6215 |
begin := end + 1. |
6216 |
depthLimit := stack removeLast. |
|
6217 |
end := stack removeLast. |
|
17938 | 6218 |
]. |
17940 | 6219 |
]. |
17938 | 6220 |
! |
6221 |
||
6222 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock with:aCollection |
|
6223 |
"actual quicksort worker for sort-message. |
|
6224 |
Simulates recursion in a stack, to avoid recursion overflow |
|
6225 |
with degenerated collections. |
|
6226 |
||
6227 |
Use sortBlock for element comparisons." |
|
6228 |
||
6229 |
|begin "{ Class: SmallInteger }" |
|
6230 |
end "{ Class: SmallInteger }" |
|
6231 |
bRun "{ Class: SmallInteger }" |
|
6232 |
eRun "{ Class: SmallInteger }" |
|
6233 |
m "{ Class: SmallInteger }" |
|
6234 |
prevIdx "{ Class: SmallInteger }" |
|
6235 |
elB elM elE temp temp1 stack depthLimit| |
|
6236 |
||
6237 |
inEnd <= inBegin ifTrue:[ |
|
6238 |
"nothing to sort" |
|
6239 |
^ self. |
|
6240 |
]. |
|
6241 |
||
17940 | 6242 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
6243 |
||
6244 |
stack := OrderedCollection new:depthLimit*2. |
|
6245 |
begin := inBegin. |
|
6246 |
end := inEnd. |
|
6247 |
||
6248 |
[true] whileTrue:[ |
|
6249 |
"do not sort small chunks, |
|
6250 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
6251 |
end - begin > 12 ifTrue:[ |
|
6252 |
"ok, her we do the real quickSort..." |
|
6253 |
"/ heapSort does not support ...with: yet |
|
6254 |
"depthLimit <= 0" false ifTrue:[ |
|
6255 |
"this is apparently a degenerated quickSort |
|
6256 |
- abort the quicksort and fall back to heapSort |
|
6257 |
which has O(n * log n) complexity in the worst case" |
|
6258 |
self heapSort:sortBlock from:begin to:end. |
|
6259 |
begin := end+1. |
|
6260 |
] ifFalse:[ |
|
6261 |
elB := self at:begin. |
|
6262 |
elE := self at:end. |
|
6263 |
m := (begin + end) // 2. |
|
6264 |
elM := self at:m. |
|
6265 |
"take the median of three as pivot (elM)" |
|
6266 |
(sortBlock value:elM value:elB) ifTrue:[ |
|
6267 |
temp := self at:begin put:elM. |
|
6268 |
elM := self at:m put:elB. |
|
6269 |
elB := temp. |
|
6270 |
||
6271 |
temp := aCollection at:begin. |
|
6272 |
aCollection at:begin put:(aCollection at:m). |
|
6273 |
aCollection at:m put:temp. |
|
17938 | 6274 |
]. |
17940 | 6275 |
(sortBlock value:elE value:elB) ifTrue:[ |
6276 |
self at:begin put:elE. |
|
6277 |
elE := self at:end put:elB. |
|
6278 |
||
6279 |
temp := aCollection at:end. |
|
6280 |
aCollection at:end put:(aCollection at:begin). |
|
6281 |
aCollection at:begin put:temp. |
|
6282 |
]. |
|
6283 |
(sortBlock value:elE value:elM) ifTrue:[ |
|
6284 |
self at:end put:elM. |
|
6285 |
elM := self at:m put:elE. |
|
6286 |
||
6287 |
temp := aCollection at:end. |
|
6288 |
aCollection at:end put:(aCollection at:m). |
|
6289 |
aCollection at:m put:temp. |
|
17938 | 6290 |
]. |
17940 | 6291 |
|
6292 |
bRun := begin. |
|
6293 |
eRun := end. |
|
6294 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
6295 |
so STC can optimize" |
|
6296 |
[bRun < eRun] whileTrue:[ |
|
6297 |
[(bRun := bRun+1) <= eRun and:[sortBlock value:(self at:bRun) value:elM]] whileTrue. |
|
6298 |
[bRun <= (eRun := eRun-1) and:[sortBlock value:elM value:(self at:eRun)]] whileTrue. |
|
6299 |
||
6300 |
(bRun < eRun) ifTrue:[ |
|
6301 |
temp := self at:bRun. |
|
6302 |
self at:bRun put:(self at:eRun). |
|
6303 |
self at:eRun put:temp. |
|
6304 |
||
6305 |
temp := aCollection at:bRun. |
|
6306 |
aCollection at:bRun put:(aCollection at:eRun). |
|
6307 |
aCollection at:eRun put:temp. |
|
6308 |
]. |
|
6309 |
]. |
|
6310 |
(bRun < end) ifTrue:[ |
|
6311 |
"remember right part for later processing" |
|
6312 |
stack add:end. |
|
6313 |
stack add:depthLimit-1. |
|
6314 |
]. |
|
6315 |
||
6316 |
"now sort the left part from begin .. new end" |
|
6317 |
depthLimit := depthLimit - 1. |
|
6318 |
end := eRun. |
|
6319 |
] |
|
17938 | 6320 |
] ifFalse:[ |
17940 | 6321 |
stack isEmpty ifTrue:[ |
6322 |
"we are done. |
|
6323 |
Do a final insertion sort over all the elements, |
|
6324 |
to eventually sort the small, unsorted chunks" |
|
6325 |
begin := inBegin. |
|
6326 |
bRun := begin + 1. |
|
6327 |
end := inEnd. |
|
6328 |
bRun to:end do:[:idx| |
|
6329 |
temp := self at:idx. |
|
6330 |
temp1 := aCollection at:idx. |
|
6331 |
prevIdx := idx-1. |
|
6332 |
[prevIdx >= begin and:[sortBlock value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
6333 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
6334 |
aCollection at:prevIdx+1 put:(aCollection at:prevIdx). |
|
6335 |
prevIdx := prevIdx - 1. |
|
6336 |
]. |
|
6337 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
6338 |
self at:prevIdx+1 put:temp. |
|
6339 |
aCollection at:prevIdx+1 put:temp1. |
|
6340 |
]. |
|
17938 | 6341 |
]. |
17940 | 6342 |
^ self |
17938 | 6343 |
]. |
17940 | 6344 |
begin := end + 1. |
6345 |
depthLimit := stack removeLast. |
|
6346 |
end := stack removeLast. |
|
17938 | 6347 |
]. |
6348 |
] |
|
6349 |
! |
|
6350 |
||
6351 |
quickSortFrom:inBegin to:inEnd with:aCollection |
|
6352 |
"actual quicksort worker for sort-message. |
|
6353 |
Simulates recursion in a stack, to avoid recursion overflow |
|
6354 |
with degenerated collections. |
|
6355 |
||
8874 | 6356 |
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
|
6357 |
method in Magnitude, and the others may be defined by sending #<." |
2069 | 6358 |
|
6359 |
|begin "{ Class: SmallInteger }" |
|
6360 |
end "{ Class: SmallInteger }" |
|
5605
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6361 |
bRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6362 |
eRun "{ Class: SmallInteger }" |
7795fe0fb8e8
changed quickSort to be stable with '<=' compare as sortBlock.
Claus Gittinger <cg@exept.de>
parents:
5575
diff
changeset
|
6363 |
m "{ Class: SmallInteger }" |
17938 | 6364 |
prevIdx "{ Class: SmallInteger }" |
6365 |
elB elM elE temp temp1 stack depthLimit| |
|
6366 |
||
6367 |
inEnd <= inBegin ifTrue:[ |
|
6368 |
"nothing to sort" |
|
6369 |
^ self. |
|
6370 |
]. |
|
6371 |
||
17940 | 6372 |
depthLimit := (1 + inEnd-inBegin) integerLog2 * 2. |
6373 |
||
6374 |
stack := OrderedCollection new:depthLimit*2. |
|
6375 |
begin := inBegin. |
|
6376 |
end := inEnd. |
|
6377 |
||
6378 |
[true] whileTrue:[ |
|
6379 |
"do not sort small chunks, |
|
6380 |
instead do an insertion sort over the whole range at the end, which is faster" |
|
6381 |
end - begin > 12 ifTrue:[ |
|
6382 |
"ok, her we do the real quickSort..." |
|
6383 |
"/ heapSort does not support ...with: yet |
|
6384 |
"depthLimit <= 0" false ifTrue:[ |
|
6385 |
"this is apparently a degenerated quickSort |
|
6386 |
- abort the quicksort and fall back to heapSort |
|
6387 |
which has O(n * log n) complexity in the worst case" |
|
6388 |
self heapSortFrom:begin to:end. |
|
6389 |
begin := end+1. |
|
6390 |
] ifFalse:[ |
|
6391 |
elB := self at:begin. |
|
6392 |
elE := self at:end. |
|
6393 |
m := (begin + end) // 2. |
|
6394 |
elM := self at:m. |
|
6395 |
"take the median of three as pivot (elM)" |
|
6396 |
(elM < elB) ifTrue:[ |
|
6397 |
temp := self at:begin put:elM. |
|
6398 |
elM := self at:m put:elB. |
|
6399 |
elB := temp. |
|
6400 |
||
6401 |
temp := aCollection at:begin. |
|
6402 |
aCollection at:begin put:(aCollection at:m). |
|
6403 |
aCollection at:m put:temp. |
|
17938 | 6404 |
]. |
17940 | 6405 |
(elE < elB) ifTrue:[ |
6406 |
self at:begin put:elE. |
|
6407 |
elE := self at:end put:elB. |
|
6408 |
||
6409 |
temp := aCollection at:end. |
|
6410 |
aCollection at:end put:(aCollection at:begin). |
|
6411 |
aCollection at:begin put:temp. |
|
6412 |
]. |
|
6413 |
(elE < elM) ifTrue:[ |
|
6414 |
self at:end put:elM. |
|
6415 |
elM := self at:m put:elE. |
|
6416 |
||
6417 |
temp := aCollection at:end. |
|
6418 |
aCollection at:end put:(aCollection at:m). |
|
6419 |
aCollection at:m put:temp. |
|
17938 | 6420 |
]. |
17940 | 6421 |
|
6422 |
bRun := begin. |
|
6423 |
eRun := end. |
|
6424 |
"use simple expressions without additional statements in whileXXX: conditions, |
|
6425 |
so STC can optimize" |
|
6426 |
[bRun < eRun] whileTrue:[ |
|
6427 |
[(bRun := bRun+1) <= eRun and:[(self at:bRun) < elM]] whileTrue. |
|
6428 |
[bRun <= (eRun := eRun-1) and:[elM < (self at:eRun)]] whileTrue. |
|
6429 |
||
6430 |
(bRun < eRun) ifTrue:[ |
|
6431 |
temp := self at:bRun. |
|
6432 |
self at:bRun put:(self at:eRun). |
|
6433 |
self at:eRun put:temp. |
|
6434 |
||
6435 |
temp := aCollection at:bRun. |
|
6436 |
aCollection at:bRun put:(aCollection at:eRun). |
|
6437 |
aCollection at:eRun put:temp. |
|
6438 |
]. |
|
6439 |
]. |
|
6440 |
(bRun < end) ifTrue:[ |
|
6441 |
"remember right part for later processing" |
|
6442 |
stack add:end. |
|
6443 |
stack add:depthLimit-1. |
|
6444 |
]. |
|
6445 |
||
6446 |
"now sort the left part from begin .. new end" |
|
6447 |
depthLimit := depthLimit - 1. |
|
6448 |
end := eRun. |
|
6449 |
] |
|
17938 | 6450 |
] ifFalse:[ |
17940 | 6451 |
stack isEmpty ifTrue:[ |
6452 |
"we are done. |
|
6453 |
Do a final insertion sort over all the elements, |
|
6454 |
to eventually sort the small, unsorted chunks" |
|
6455 |
begin := inBegin. |
|
6456 |
bRun := begin + 1. |
|
6457 |
end := inEnd. |
|
6458 |
bRun to:end do:[:idx| |
|
6459 |
temp := self at:idx. |
|
6460 |
temp1 := aCollection at:idx. |
|
6461 |
prevIdx := idx-1. |
|
6462 |
[prevIdx >= begin and:[temp < (self at:prevIdx)]] whileTrue:[ |
|
6463 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
6464 |
aCollection at:prevIdx+1 put:(aCollection at:prevIdx). |
|
6465 |
prevIdx := prevIdx - 1. |
|
6466 |
]. |
|
6467 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
6468 |
self at:prevIdx+1 put:temp. |
|
6469 |
aCollection at:prevIdx+1 put:temp1. |
|
6470 |
]. |
|
17938 | 6471 |
]. |
17940 | 6472 |
^ self |
17938 | 6473 |
]. |
17940 | 6474 |
begin := end + 1. |
6475 |
depthLimit := stack removeLast. |
|
6476 |
end := stack removeLast. |
|
17938 | 6477 |
]. |
6919
08fbf325f789
changed all quickSort methods to a simulated recursive
Claus Gittinger <cg@exept.de>
parents:
6868
diff
changeset
|
6478 |
] |
2069 | 6479 |
! ! |
6480 |
||
602 | 6481 |
!SequenceableCollection methodsFor:'queries'! |
6482 |
||
6483 |
firstIndex |
|
6484 |
"return the first elements index" |
|
6485 |
||
6486 |
^ 1 |
|
6487 |
! |
|
6488 |
||
6489 |
isSequenceable |
|
6490 |
"return true, if the receiver is some kind of sequenceableCollection" |
|
6491 |
||
6492 |
^ true |
|
1 | 6493 |
! |
6494 |
||
602 | 6495 |
isSequenceableCollection |
6496 |
"OBSOLETE: use isSequenceable for ST-80 compatibility. |
|
6497 |
This method is a historic leftover and will be removed." |
|
6498 |
||
5872 | 6499 |
<resource:#obsolete> |
6500 |
||
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
6501 |
self obsoleteMethodWarning:'use #isSequenceable'. |
602 | 6502 |
^ true |
3435
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
6503 |
|
04c06b559fa6
isSequenceableCollection is now obsolete;
Claus Gittinger <cg@exept.de>
parents:
3408
diff
changeset
|
6504 |
"Modified: / 8.5.1998 / 21:27:18 / cg" |
1 | 6505 |
! |
6506 |
||
5358 | 6507 |
isSorted |
6508 |
"return true. if my elements are sorted (already)" |
|
6509 |
||
6510 |
|lastEl el| |
|
6511 |
||
6512 |
lastEl := self first. |
|
6513 |
2 to:self size do:[:i | |
|
8874 | 6514 |
el := self at:i. |
6515 |
el >= lastEl ifFalse: [^ false]. |
|
6516 |
lastEl := el |
|
5358 | 6517 |
]. |
6518 |
^ true |
|
6519 |
||
6520 |
" |
|
8874 | 6521 |
#(1 2 3 5 10 100) isSorted |
6522 |
#(1 2 3 5 100 10) isSorted |
|
5358 | 6523 |
" |
6524 |
! |
|
6525 |
||
6526 |
isSortedBy:aBlock |
|
6527 |
"return true, if my elements are sorted (already) by the given criterion (sortBlock)" |
|
6528 |
||
6529 |
|lastEl el| |
|
6530 |
||
6531 |
lastEl := self first. |
|
6532 |
2 to:self size do:[:i | |
|
8874 | 6533 |
el := self at:i. |
6534 |
(aBlock value:lastEl value:el) ifFalse: [^ false]. |
|
6535 |
lastEl := el |
|
5358 | 6536 |
]. |
6537 |
^ true |
|
6538 |
||
6539 |
" |
|
8874 | 6540 |
#(1 2 3 5 10 100) isSortedBy:[:a :b | a <= b] |
6541 |
#(1 2 3 5 100 10) isSortedBy:[:a :b | a <= b] |
|
6542 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a <= b] |
|
6543 |
#(100 10 5 3 2 1) isSortedBy:[:a :b | a > b] |
|
5358 | 6544 |
" |
6545 |
||
6546 |
! |
|
6547 |
||
602 | 6548 |
keys |
6549 |
"return a collection with all keys in the Smalltalk dictionary" |
|
6550 |
||
6551 |
|sz| |
|
6552 |
||
1 | 6553 |
sz := self size. |
602 | 6554 |
sz == 0 ifTrue:[ |
8874 | 6555 |
^ #() |
61 | 6556 |
]. |
602 | 6557 |
^ 1 to:sz |
1 | 6558 |
! |
6559 |
||
602 | 6560 |
lastIndex |
6561 |
"return the last elements index" |
|
6562 |
||
6563 |
^ self size |
|
61 | 6564 |
! |
6565 |
||
602 | 6566 |
size |
6567 |
"return the number of elements in the collection. |
|
6568 |
concrete implementations must define this" |
|
6569 |
||
6570 |
^ self subclassResponsibility |
|
2165
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6571 |
! |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6572 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6573 |
zeroIndex |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6574 |
"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
|
6575 |
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
|
6576 |
ST-compatibility" |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6577 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6578 |
^ 0 |
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6579 |
|
9ef030343739
added #zeroIndex for protocol completeness (ST-80)
Claus Gittinger <cg@exept.de>
parents:
2069
diff
changeset
|
6580 |
"Created: 14.2.1997 / 16:13:03 / cg" |
1 | 6581 |
! ! |
6582 |
||
6583 |
!SequenceableCollection methodsFor:'searching'! |
|
6584 |
||
3887 | 6585 |
detect:aBlock startingAt:startIndex |
6586 |
"find the first element, for which evaluation of the argument, aBlock |
|
6587 |
return true, starting the search with startIndex. |
|
6588 |
If none does so, report an error" |
|
6589 |
||
6590 |
^ self detect:aBlock startingAt:startIndex ifNone:[self errorNotFound] |
|
6591 |
||
6592 |
" |
|
8874 | 6593 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 |
6594 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 |
|
3887 | 6595 |
" |
6596 |
||
6597 |
"Created: / 21.10.1998 / 18:47:28 / cg" |
|
6598 |
! |
|
6599 |
||
6600 |
detect:aBlock startingAt:startIndex ifNone:exceptionBlock |
|
6601 |
"find the first element, for which evaluation of the argument, aBlock |
|
6602 |
return true, starting the search with startIndex. |
|
5717 | 6603 |
If none does so, return the evaluation of exceptionBlock" |
6604 |
||
6605 |
|stop| |
|
3887 | 6606 |
|
6607 |
stop := self size. |
|
5717 | 6608 |
self from:startIndex to:stop do:[:el | |
8874 | 6609 |
(aBlock value:el) ifTrue:[ |
6610 |
^ el |
|
6611 |
]. |
|
3887 | 6612 |
]. |
6613 |
^ exceptionBlock value |
|
6614 |
||
6615 |
" |
|
8874 | 6616 |
#(11 12 13 14) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
6617 |
#(12 14 16 18) detect:[:n | n odd] startingAt:3 ifNone:['sorry'] |
|
3887 | 6618 |
" |
6619 |
||
6620 |
"Created: / 21.10.1998 / 18:46:01 / cg" |
|
6621 |
! |
|
6622 |
||
11705 | 6623 |
findFirst:aBlock ifNone:exceptionalValue |
10453 | 6624 |
"find the index of the first element, for which evaluation of the argument, aBlock |
11705 | 6625 |
returns true; return its index or the value from exceptionalValue if none detected. |
3887 | 6626 |
This is much like #detect, however, here an INDEX is returned, |
6627 |
while #detect returns the element." |
|
602 | 6628 |
|
6629 |
|stop "{ Class: SmallInteger }" | |
|
6630 |
||
32 | 6631 |
stop := self size. |
602 | 6632 |
1 to:stop do:[:index | |
10453 | 6633 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
1 | 6634 |
]. |
11705 | 6635 |
^ exceptionalValue value |
6636 |
||
6637 |
" |
|
6638 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 3)] ifNone:0 |
|
6639 |
#(1 2 3 4 5 6) findFirst:[:x | (x >= 7)] ifNone:nil |
|
6640 |
" |
|
1 | 6641 |
! |
6642 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6643 |
findFirst:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6644 |
"find the first element, for which evaluation of the argument, aBlock |
3887 | 6645 |
returns true; return its index or 0 if none detected. |
6646 |
This is much like #detect:startingAt:, however, here an INDEX is returned, |
|
6647 |
while #detect returns the element." |
|
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6648 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6649 |
|start "{ Class: SmallInteger }" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6650 |
stop "{ Class: SmallInteger }" | |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6651 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6652 |
start := startIndex. "/ as a compiler hint |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6653 |
stop := self size. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6654 |
start to:stop do:[:index | |
8874 | 6655 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6656 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6657 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6658 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6659 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6660 |
#(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
|
6661 |
'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6662 |
" |
3887 | 6663 |
|
6664 |
"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
|
6665 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6666 |
|
602 | 6667 |
findLast:aBlock |
6668 |
"find the last element, for which evaluation of the argument, aBlock |
|
8193 | 6669 |
returns true. |
6670 |
Return its index or 0 if none detected." |
|
602 | 6671 |
|
6672 |
|start "{ Class: SmallInteger }"| |
|
6673 |
||
6674 |
start := self size. |
|
6675 |
start to:1 by:-1 do:[:index | |
|
8874 | 6676 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
252 | 6677 |
]. |
6678 |
^ 0 |
|
6679 |
||
6680 |
" |
|
602 | 6681 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] |
6682 |
'one.two.three' findLast:[:c | (c == $.)] |
|
252 | 6683 |
" |
6684 |
! |
|
6685 |
||
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6686 |
findLast:aBlock startingAt:startIndex |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6687 |
"find the last element, for which evaluation of the argument, aBlock |
8193 | 6688 |
returns true. Start the search at startIndex. |
6689 |
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
|
6690 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6691 |
|start "{ Class: SmallInteger }"| |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6692 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6693 |
start := startIndex. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6694 |
start to:1 by:-1 do:[:index | |
8874 | 6695 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
929
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6696 |
]. |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6697 |
^ 0 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6698 |
|
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6699 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6700 |
#(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
|
6701 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6702 |
" |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6703 |
! |
674de4c05dcb
added findFirst:startingAt: and findLast:startingAt:
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
6704 |
|
8193 | 6705 |
findLast:aBlock startingAt:startIndex endingAt:endIndex |
6706 |
"find the last element, for which evaluation of the argument, aBlock |
|
6707 |
returns true. Start the search at startIndex. |
|
6708 |
End the search at endIndex or when an element is found. |
|
6709 |
Return its index or 0 if none detected." |
|
6710 |
||
6711 |
|start "{ Class: SmallInteger }"| |
|
6712 |
||
6713 |
start := startIndex. |
|
6714 |
start to:endIndex by:-1 do:[:index | |
|
8874 | 6715 |
(aBlock value:(self at:index)) ifTrue:[^ index]. |
8193 | 6716 |
]. |
6717 |
^ 0 |
|
6718 |
||
6719 |
" |
|
6720 |
#(1 99 3 99 5 6) findLast:[:x | (x == 99)] startingAt:3 |
|
6721 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:6 |
|
6722 |
#(1 99 3 99 3 5 6) findLast:[:x | (x == 3)] startingAt:7 endingAt:5 |
|
6723 |
'one.two.three' findLast:[:c | (c == $.)] startingAt:7 |
|
6724 |
" |
|
6725 |
! |
|
6726 |
||
159 | 6727 |
indexOfAny:aCollection |
6728 |
"search the collection for an element in aCollection. |
|
6729 |
if found, return the index otherwise return 0. |
|
8874 | 6730 |
The comparison is done using = |
159 | 6731 |
(i.e. equality test - not identity test). |
6732 |
||
6733 |
Notice, that for big collections, the runtime of this search |
|
6734 |
grows proportional to size(receiver) * size(aCollection). |
|
6735 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6736 |
||
6737 |
^ self indexOfAny:aCollection startingAt:1 |
|
6738 |
||
6739 |
" |
|
6740 |
#(10 20 30 40 50 60 70) indexOfAny:#(40 30 50) |
|
6741 |
#(10 20 30 40 50 60 70) indexOfAny:#(40.0 30.0 50) |
|
6742 |
" |
|
6743 |
! |
|
6744 |
||
6745 |
indexOfAny:aCollection startingAt:start |
|
6746 |
"search the collection for an element in aCollection, |
|
6747 |
starting the search at index start; |
|
6748 |
if found, return the index otherwise return 0. |
|
8874 | 6749 |
The comparison is done using = |
159 | 6750 |
(i.e. equality test - not identity test). |
6751 |
||
6752 |
Notice, that for big collections, the runtime of this search |
|
6753 |
grows proportional to size(receiver) * size(aCollection). |
|
6754 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
|
6755 |
||
6756 |
||
6757 |
|startIndex "{ Class: SmallInteger }" |
|
6758 |
stop "{ Class: SmallInteger }" | |
|
6759 |
||
6760 |
startIndex := start. |
|
6761 |
stop := self size. |
|
6762 |
startIndex to:stop do:[:index | |
|
8874 | 6763 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
159 | 6764 |
]. |
6765 |
^ 0 |
|
6766 |
" |
|
8874 | 6767 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 |
6768 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 |
|
159 | 6769 |
" |
1 | 6770 |
! |
6771 |
||
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6772 |
indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6773 |
"search the collection for an element in aCollection, |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6774 |
starting the search at index start; |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6775 |
if found, return the index. If not, return the value returned by exceptionBlock. |
8874 | 6776 |
The comparison is done using = |
518
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6777 |
(i.e. equality test - not identity test). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6778 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6779 |
Notice, that for big collections, the runtime of this search |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6780 |
grows proportional to size(receiver) * size(aCollection). |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6781 |
You may think about using other mechanisms (Sets, Dictionaries etc)." |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6782 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6783 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6784 |
|val| |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6785 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6786 |
val := self indexOfAny:aCollection startingAt:start . |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6787 |
val == 0 ifTrue:[^ exceptionBlock value]. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6788 |
^ val. |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6789 |
|
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6790 |
" |
8874 | 6791 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40 50 30) startingAt:5 ifAbsent:-1 |
6792 |
#(10 20 30 40 10 20 30 40) indexOfAny:#(40.0 50 30.0) startingAt:5 ifAbsent:-1 |
|
6793 |
#(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
|
6794 |
" |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6795 |
! |
5372a8875432
added indexOfAny:startingAt:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
424
diff
changeset
|
6796 |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6797 |
indexOfAnyOf:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6798 |
"squeak compatibility: same as #indexOfAny:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6799 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6800 |
^ self indexOfAny:aCollection |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6801 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6802 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6803 |
#(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
|
6804 |
#(10 20 30 40 50 60 70) indexOfAnyOf:#(40.0 30.0 50) |
17815 | 6805 |
'abcdefg' indexOfAnyOf:(CharacterSet newFrom:'cef') |
6806 |
" |
|
6807 |
||
6808 |
"Modified: / 28-01-2011 / 18:03:14 / cg" |
|
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6809 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6810 |
|
8874 | 6811 |
indexOfAnyOf:aCollection startingAt:start |
4338
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6812 |
"squeak compatibility: same as #indexOfAny:startingAt:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6813 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6814 |
^ self indexOfAny:aCollection startingAt:start |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6815 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6816 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6817 |
#(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
|
6818 |
#(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
|
6819 |
" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6820 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6821 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6822 |
indexOfAnyOf:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6823 |
"squeak compatibility: same as #indexOfAny:startingAt:ifAbsent:" |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6824 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6825 |
^ self indexOfAny:aCollection startingAt:start ifAbsent:exceptionBlock |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6826 |
|
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6827 |
! |
326f43992dcd
added #indexOfAnyOf:* for squeak compatibility.
Claus Gittinger <cg@exept.de>
parents:
4303
diff
changeset
|
6828 |
|
368 | 6829 |
indexOfSubCollection:aCollection |
9234 | 6830 |
"find a subcollection. If found, return the index; if not found, return 0." |
368 | 6831 |
|
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6832 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:0 |
368 | 6833 |
|
6834 |
" |
|
8874 | 6835 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() |
6836 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) |
|
6837 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1 2 3 4) |
|
6838 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(2 3 5) |
|
6839 |
#(1 2 3 2 3 4 5) indexOfSubCollection:#(2 3 4) |
|
6840 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7) |
|
6841 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 8) |
|
6842 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(5 6 7 8) |
|
17841 | 6843 |
'foobarbaz' indexOfSubCollection:'bar' |
6844 |
" |
|
6845 |
||
6846 |
"Modified: / 20-04-2011 / 19:11:20 / cg" |
|
368 | 6847 |
! |
6848 |
||
602 | 6849 |
indexOfSubCollection:aCollection ifAbsent:exceptionBlock |
6850 |
"find a subcollection. If found, return the index; |
|
6851 |
if not found, return the result of evaluating exceptionBlock." |
|
6852 |
||
6853 |
^ self indexOfSubCollection:aCollection startingAt:1 ifAbsent:exceptionBlock |
|
6854 |
! |
|
6855 |
||
368 | 6856 |
indexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6857 |
"find a subcollection starting at index. |
368 | 6858 |
If found, return the index; if not found, return 0." |
6859 |
||
1145
a094d90e11bf
dont use [0] blocks - use 0 constant instead
Claus Gittinger <cg@exept.de>
parents:
1016
diff
changeset
|
6860 |
^ self indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
368 | 6861 |
! |
6862 |
||
6863 |
indexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
|
6864 |
"find a subcollection, starting at index. If found, return the index; |
|
6865 |
if not found, return the result of evaluating exceptionBlock. |
|
6866 |
This is a q&d hack - not very efficient" |
|
6867 |
||
8874 | 6868 |
|same first |
368 | 6869 |
mySize "{Class: SmallInteger }" |
6870 |
checkIndex "{Class: SmallInteger }" |
|
6871 |
cmpIndex "{Class: SmallInteger }" |
|
6872 |
sz "{Class: SmallInteger }"| |
|
6873 |
||
6874 |
mySize := self size. |
|
6875 |
sz := aCollection size. |
|
6876 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
|
6877 |
first := aCollection at:1. |
|
6878 |
||
8874 | 6879 |
checkIndex := startIndex - 1. |
368 | 6880 |
[true] whileTrue:[ |
8874 | 6881 |
checkIndex := self indexOf:first startingAt:checkIndex+1. |
6882 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6883 |
||
6884 |
(checkIndex + sz - 1) > mySize ifTrue:[ |
|
6885 |
^ 0 |
|
6886 |
]. |
|
6887 |
same := true. |
|
6888 |
cmpIndex := 1. |
|
6889 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6890 |
(self at:checkIndex + cmpIndex - 1) = (aCollection at:cmpIndex) ifFalse:[ |
|
6891 |
same := false |
|
6892 |
] ifTrue:[ |
|
6893 |
cmpIndex := cmpIndex + 1 |
|
6894 |
] |
|
6895 |
]. |
|
6896 |
same ifTrue:[^ checkIndex]. |
|
6897 |
] |
|
6898 |
||
6899 |
" |
|
6900 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#() startingAt:2 |
|
6901 |
#(1 2 3 4 5 6 7) indexOfSubCollection:#(1) startingAt:2 |
|
6902 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2 3) startingAt:2 |
|
6903 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:2 |
|
6904 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:3 |
|
6905 |
#(1 2 1 2 1 2 3) indexOfSubCollection:#(1 2) startingAt:4 |
|
368 | 6906 |
" |
602 | 6907 |
! |
6908 |
||
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6909 |
lastIndexOfSubCollection:aCollection |
8874 | 6910 |
"find a subcollection from the end. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6911 |
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
|
6912 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6913 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6914 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6915 |
"Created: / 16.5.1998 / 20:08:55 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6916 |
"Modified: / 16.5.1998 / 20:12:37 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6917 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6918 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6919 |
lastIndexOfSubCollection:aCollection ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6920 |
"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
|
6921 |
if not found, return the result of evaluating exceptionBlock." |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6922 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6923 |
^ self lastIndexOfSubCollection:aCollection startingAt:self size ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6924 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6925 |
"Created: / 16.5.1998 / 20:09:08 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6926 |
"Modified: / 16.5.1998 / 20:12:39 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6927 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6928 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6929 |
lastIndexOfSubCollection:aCollection startingAt:startIndex |
8874 | 6930 |
"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
|
6931 |
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
|
6932 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6933 |
^ self lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:0 |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6934 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6935 |
"Created: / 16.5.1998 / 20:09:23 / cg" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6936 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6937 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6938 |
lastIndexOfSubCollection:aCollection startingAt:startIndex ifAbsent:exceptionBlock |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6939 |
"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
|
6940 |
if not found, return the result of evaluating exceptionBlock. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6941 |
This is a q&d hack - not very efficient" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6942 |
|
8874 | 6943 |
|same last |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6944 |
mySize "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6945 |
checkIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6946 |
cmpIndex "{Class: SmallInteger }" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6947 |
sz "{Class: SmallInteger }"| |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6948 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6949 |
mySize := self size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6950 |
sz := aCollection size. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6951 |
sz == 0 ifTrue:[^ exceptionBlock value]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6952 |
last := aCollection at:sz. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6953 |
|
8874 | 6954 |
checkIndex := startIndex + sz. |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6955 |
checkIndex > mySize ifTrue:[ |
8874 | 6956 |
checkIndex := mySize + 1 |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6957 |
]. |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6958 |
[true] whileTrue:[ |
8874 | 6959 |
checkIndex := self lastIndexOf:last startingAt:checkIndex-1. |
6960 |
checkIndex == 0 ifTrue:[^ exceptionBlock value]. |
|
6961 |
||
6962 |
(checkIndex - sz + 1) < 1 ifTrue:[ |
|
6963 |
^ 0 |
|
6964 |
]. |
|
6965 |
same := true. |
|
6966 |
cmpIndex := 1. |
|
6967 |
[same and:[cmpIndex <= sz]] whileTrue:[ |
|
6968 |
(self at:checkIndex - sz + cmpIndex) = (aCollection at:cmpIndex) ifFalse:[ |
|
6969 |
same := false |
|
6970 |
] ifTrue:[ |
|
6971 |
cmpIndex := cmpIndex + 1 |
|
6972 |
] |
|
6973 |
]. |
|
6974 |
same ifTrue:[^ checkIndex - sz + 1]. |
|
6975 |
] |
|
6976 |
||
6977 |
" |
|
6978 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#() startingAt:2 |
|
6979 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) |
|
6980 |
#(1 2 3 4 5 6 7) lastIndexOfSubCollection:#(1) startingAt:4 |
|
6981 |
#(1 3 1 2 1 3 3) lastIndexOfSubCollection:#(1 2 3) |
|
6982 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) startingAt:2 |
|
6983 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2 3) |
|
6984 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) |
|
6985 |
#(1 2 1 2 1 2 3) lastIndexOfSubCollection:#(1 2) startingAt:5 |
|
6986 |
#(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
|
6987 |
" |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6988 |
|
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
6989 |
"Modified: / 16.5.1998 / 20:21:46 / cg" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6990 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6991 |
|
7264 | 6992 |
!SequenceableCollection methodsFor:'searching-equality'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6993 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6994 |
includes:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6995 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6996 |
Comparison is done using equality compare (i.e. =). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6997 |
See #includesIdentical: if identity is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
6998 |
|
10194 | 6999 |
^ (self indexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7000 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7001 |
" |
8874 | 7002 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7003 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7004 |
returns: true - if found |
10194 | 7005 |
false - if not found |
8874 | 7006 |
" |
7007 |
||
7008 |
" |
|
7009 |
#(10 20 30 40 50 60 70) includes:99 |
|
7010 |
#(10 20 30 40 50 60 70) includes:40 |
|
7011 |
#(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
|
7012 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7013 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7014 |
"Modified: / 20.5.1998 / 15:00:23 / cg" |
3452
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
7015 |
! |
95d66fc71059
added #lastIndexOfSubCollection family of methods.
Claus Gittinger <cg@exept.de>
parents:
3435
diff
changeset
|
7016 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7017 |
indexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7018 |
"search the collection for anElement; |
602 | 7019 |
if found, return the index otherwise return 0. |
8874 | 7020 |
The comparison is done using = |
602 | 7021 |
(i.e. equality test - not identity test)." |
7022 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7023 |
^ self indexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7024 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7025 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7026 |
args: anElement : <object> |
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 |
returns: elementIndex - if found |
8874 | 7029 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7030 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7031 |
|
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 |
#(10 20 30 40 50 60 70) indexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7034 |
#(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
|
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 |
"Modified: / 20.5.1998 / 14:59:55 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7038 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7039 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7040 |
indexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7041 |
"search the collection for anElement; |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
7042 |
if found, return the index; otherwise return the value of the exceptionBlock. |
8874 | 7043 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7044 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7045 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7046 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7047 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7048 |
index := self indexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7049 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7050 |
^ index |
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7053 |
args: anElement : <object> |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
7054 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7055 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7056 |
returns: elementIndex - if found |
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
7057 |
exceptionBlock value - if not found |
8874 | 7058 |
" |
7059 |
||
7060 |
" |
|
7061 |
#(10 20 30 40 10 20 30 40) indexOf:40 ifAbsent:['none'] |
|
7062 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 ifAbsent:['none'] |
|
7063 |
#(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
|
7064 |
" |
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 |
"Modified: / 20.5.1998 / 14:57:35 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7067 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7068 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7069 |
indexOf:elementToFind replaceWith:replacement startingAt:start stoppingAt:stop |
8874 | 7070 |
"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
|
7071 |
stopping the search at stop. If found, replace the element by replacement |
17944 | 7072 |
and return the index. If not found, return 0. |
7073 |
The comparison is done using = |
|
7074 |
(i.e. equality test - not identity test)." |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7075 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7076 |
|idx| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7077 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7078 |
idx := self indexOf:elementToFind startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7079 |
((idx > 0) and:[idx <= stop]) ifTrue:[ |
17944 | 7080 |
self at:idx put:replacement. |
7081 |
^ idx |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7082 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7083 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7084 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7085 |
" |
8874 | 7086 |
args: elementToFind : <object> |
17944 | 7087 |
replacement : <integer> |
7088 |
start : <integer> |
|
7089 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7090 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7091 |
returns: elementIndex - if found (and replaced) |
17944 | 7092 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7093 |
" |
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 |
|a| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7097 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7098 |
a := #(10 20 30 40 50 60 70). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7099 |
(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
|
7100 |
a printNL. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7101 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7102 |
|
17944 | 7103 |
"Modified: / 20-05-1998 / 14:59:30 / cg" |
7104 |
"Modified (comment): / 23-05-2012 / 13:29:18 / cg" |
|
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 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7107 |
indexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7108 |
"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
|
7109 |
if found, return the index otherwise return 0. |
8874 | 7110 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7111 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7112 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7113 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7114 |
stop "{ Class: SmallInteger }" | |
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 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7117 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7118 |
startIndex to:stop do:[:index | |
8874 | 7119 |
anElement = (self at:index) ifTrue:[^ index]. |
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 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7122 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7123 |
" |
8874 | 7124 |
args: anElement : <object> |
7125 |
start : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7126 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7127 |
returns: elementIndex - if found |
8874 | 7128 |
0 - if not found |
7129 |
" |
|
7130 |
||
7131 |
" |
|
7132 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 |
|
7133 |
#(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
|
7134 |
" |
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 |
"Modified: / 20.5.1998 / 14:58:49 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7137 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7138 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7139 |
indexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7140 |
"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
|
7141 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7142 |
If found (within the range), return the index, otherwise return 0. |
8874 | 7143 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7144 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7145 |
|
602 | 7146 |
|startIndex "{ Class: SmallInteger }" |
7147 |
stopIndex "{ Class: SmallInteger }" | |
|
7148 |
||
7149 |
startIndex := start. |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7150 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7151 |
startIndex to:stopIndex do:[:index | |
8874 | 7152 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7153 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7154 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7155 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7156 |
" |
8874 | 7157 |
args: anElement : <object> |
7158 |
start : <integer> |
|
7159 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7160 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7161 |
returns: elementIndex - if found |
8874 | 7162 |
exceptionBlock value - if not found |
7163 |
" |
|
7164 |
||
7165 |
" |
|
7166 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:99 |
|
7167 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:3 endingAt:5 |
|
7168 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:1 endingAt:3 |
|
7169 |
#(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
|
7170 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7171 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7172 |
"Created: / 12.4.1996 / 17:37:13 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7173 |
"Modified: / 20.5.1998 / 14:58:28 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7174 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7175 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7176 |
indexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7177 |
"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
|
7178 |
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
|
7179 |
exceptionBlock. |
8874 | 7180 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7181 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7182 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7183 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7184 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7185 |
index := self indexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7186 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7187 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7188 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7189 |
" |
8874 | 7190 |
args: anElement : <object> |
7191 |
start : <integer> |
|
7192 |
exceptionBlock : <valueImplementor> |
|
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 |
returns: elementIndex - if found |
8874 | 7195 |
exceptionBlock value - if not found |
7196 |
" |
|
7197 |
||
7198 |
" |
|
7199 |
#(10 20 30 40 10 20 30 40) indexOf:40 startingAt:5 ifAbsent:['none'] |
|
7200 |
#(10 20 30 40 10 20 30 40) indexOf:40.0 startingAt:5 ifAbsent:['none'] |
|
7201 |
#(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
|
7202 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7203 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7204 |
"Modified: / 20.5.1998 / 14:58:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7205 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7206 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7207 |
lastIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7208 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7209 |
if found, return the index otherwise return 0. |
8874 | 7210 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7211 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7212 |
|
8874 | 7213 |
^ self lastIndexOf:anElement startingAt:self size |
3468
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 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7216 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7217 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7218 |
returns: elementIndex - if found |
8874 | 7219 |
0 - if not found |
7220 |
" |
|
7221 |
||
7222 |
" |
|
7223 |
#(10 20 30 40 50 60 70) lastIndexOf:40 |
|
7224 |
#(10 20 30 40 50 60 70) lastIndexOf:40.0 |
|
7225 |
#(10 20 30 40 50 60 70) lastIndexOf:35 |
|
7226 |
#(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
|
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 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7230 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7231 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7232 |
lastIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7233 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7234 |
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
|
7235 |
exceptionBlock. |
8874 | 7236 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7237 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7238 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7239 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7240 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7241 |
index := self lastIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7242 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7243 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7244 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7245 |
" |
8874 | 7246 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 ifAbsent:['none'] |
7247 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 ifAbsent:['none'] |
|
7248 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 ifAbsent:['none'] |
|
7249 |
#(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
|
7250 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7251 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7252 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7253 |
lastIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7254 |
"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
|
7255 |
if found, return the index otherwise return 0. |
8874 | 7256 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7257 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7258 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7259 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7260 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7261 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7262 |
startIndex to:1 by:-1 do:[:index | |
8874 | 7263 |
anElement = (self at:index) ifTrue:[^ index]. |
602 | 7264 |
]. |
7265 |
^ 0 |
|
7266 |
||
7267 |
" |
|
8874 | 7268 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 |
7269 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 |
|
7270 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 |
|
7271 |
#(10 20 30 40 10 20 30 40) lastIndexOf:10 startingAt:8 |
|
7272 |
#(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
|
7273 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7274 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7275 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7276 |
lastIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 7277 |
"search the collection backwards for anElement starting the search at |
7278 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7279 |
otherwise return the value of the exceptionBlock. |
8874 | 7280 |
The comparison is done using = |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7281 |
(i.e. equality test - not identity test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7282 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7283 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7284 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7285 |
index := self lastIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7286 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7287 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7288 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7289 |
" |
8874 | 7290 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40 startingAt:8 ifAbsent:['none'] |
7291 |
#(10 20 30 40 10 20 30 40) lastIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
7292 |
#(10 20 30 40 10 20 30 40) lastIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
7293 |
#(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
|
7294 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7295 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7296 |
|
4788 | 7297 |
lastIndexOfAny:aCollection |
7298 |
"search the collection backwards for any in aCollection; |
|
7299 |
if found, return the index, otherwise return 0. |
|
8874 | 7300 |
The comparison is done using = |
4788 | 7301 |
(i.e. equality test - not identity test)." |
7302 |
||
8874 | 7303 |
^ self lastIndexOfAny:aCollection startingAt:self size |
4788 | 7304 |
|
7305 |
" |
|
7306 |
args: aCollection : <collection> |
|
7307 |
||
7308 |
returns: elementIndex - if found |
|
8874 | 7309 |
0 - if not found |
7310 |
" |
|
7311 |
||
7312 |
" |
|
7313 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40 60) |
|
4788 | 7314 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(40.0 60) |
8874 | 7315 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(35 40) |
7316 |
#(10 20 30 40 50 60 70) lastIndexOfAny:#(15 35) |
|
4788 | 7317 |
" |
7318 |
||
7319 |
"Modified: / 20.5.1998 / 15:02:10 / cg" |
|
7320 |
! |
|
7321 |
||
7322 |
lastIndexOfAny:aCollection startingAt:start |
|
7323 |
"search the collection backwards for any in aCollection, starting the search at index start; |
|
7324 |
if found, return the index, otherwise return 0. |
|
8874 | 7325 |
The comparison is done using = |
4788 | 7326 |
(i.e. equality test - not identity test)." |
7327 |
||
7328 |
|startIndex "{ Class: SmallInteger }"| |
|
7329 |
||
7330 |
startIndex := start. |
|
7331 |
startIndex to:1 by:-1 do:[:index | |
|
8874 | 7332 |
(aCollection includes:(self at:index)) ifTrue:[^ index]. |
4788 | 7333 |
]. |
7334 |
^ 0 |
|
7335 |
||
7336 |
" |
|
8874 | 7337 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:8 |
7338 |
#(10 20 30 40 10 20 30 40) lastIndexOfAny:#(40 60) startingAt:7 |
|
4788 | 7339 |
" |
7340 |
! |
|
7341 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7342 |
nextIndexOf:anElement from:start to:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7343 |
"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
|
7344 |
stopping at:stop; |
17864 | 7345 |
if found, return the index otherwise return 0. |
8874 | 7346 |
The comparison is done using = |
17864 | 7347 |
(i.e. equality test - not identity test)." |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7348 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7349 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7350 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7351 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7352 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7353 |
stopIndex := stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7354 |
startIndex to:stop do:[:index | |
17864 | 7355 |
anElement = (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7356 |
]. |
17864 | 7357 |
^ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7358 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7359 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7360 |
args: anElement : <object> |
17864 | 7361 |
start : <integer> |
7362 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7363 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7364 |
returns: elementIndex - if found |
17864 | 7365 |
nil - if not found |
8874 | 7366 |
" |
7367 |
||
7368 |
" |
|
7369 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 |
|
7370 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 |
|
7371 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 |
|
602 | 7372 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7373 |
|
17864 | 7374 |
"Modified: / 23-09-2011 / 14:03:05 / cg" |
602 | 7375 |
! |
7376 |
||
7377 |
nextIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
7378 |
"search the collection for anElement, starting the search at index start |
|
7379 |
and stopping at stop; |
|
17944 | 7380 |
if found, return the index otherwise return the value of the exceptionBlock. |
8874 | 7381 |
The comparison is done using = |
602 | 7382 |
(i.e. equality test - not identity test)." |
7383 |
||
7384 |
|index| |
|
7385 |
||
7386 |
index := self nextIndexOf:anElement from:start to:stop. |
|
17864 | 7387 |
index == 0 ifTrue:[^ exceptionBlock value]. |
602 | 7388 |
^ index |
7389 |
||
7390 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7391 |
args: anElement : <object> |
17864 | 7392 |
start : <integer> |
7393 |
stop : <integer> |
|
7394 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7395 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7396 |
returns: elementIndex - if found |
17864 | 7397 |
valueImplementor value - if not found |
8874 | 7398 |
" |
7399 |
||
7400 |
" |
|
7401 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40 from:2 to:6 ifAbsent:['none'] |
|
7402 |
#(10 20 30 40 10 20 30 40) nextIndexOf:40.0 from:2 to:6 ifAbsent:['none'] |
|
7403 |
#(10 20 30 40 10 20 30 40) nextIndexOf:35 from:2 to:6 ifAbsent:['none'] |
|
602 | 7404 |
" |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7405 |
|
17864 | 7406 |
"Modified: / 23-09-2011 / 14:03:36 / cg" |
17944 | 7407 |
"Modified (comment): / 23-05-2012 / 13:29:42 / cg" |
2275 | 7408 |
! |
7409 |
||
7410 |
prevIndexOf:anElement from:startSearchIndex to:endSearchIndex |
|
7411 |
"search the collection for anElement, starting the search at index start; |
|
7412 |
ending at stop, going in reverse direction. |
|
17864 | 7413 |
If found (within the range), return the index, otherwise return 0. |
8874 | 7414 |
The comparison is done using = |
17864 | 7415 |
(i.e. equality test - not identity test)." |
2275 | 7416 |
|
7417 |
|startIndex "{ Class: SmallInteger }" |
|
7418 |
stopIndex "{ Class: SmallInteger }" | |
|
7419 |
||
7420 |
startIndex := startSearchIndex. |
|
7421 |
stopIndex := endSearchIndex max:1. |
|
7422 |
startIndex to:stopIndex by:-1 do:[:index | |
|
17864 | 7423 |
anElement = (self at:index) ifTrue:[^ index]. |
2275 | 7424 |
]. |
17864 | 7425 |
^ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7426 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7427 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7428 |
args: anElement : <object> |
17864 | 7429 |
start : <integer> |
7430 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7431 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7432 |
returns: elementIndex - if found |
17864 | 7433 |
nil - if not found |
8874 | 7434 |
" |
7435 |
||
7436 |
" |
|
7437 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 |
|
7438 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 |
|
7439 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 |
|
2275 | 7440 |
" |
7441 |
||
17864 | 7442 |
"Modified: / 23-09-2011 / 14:02:36 / cg" |
2275 | 7443 |
! |
7444 |
||
7445 |
prevIndexOf:anElement from:start to:stop ifAbsent:exceptionBlock |
|
7446 |
"search the collection for anElement, starting the search at index start |
|
7447 |
and stopping at stop, doing a reverse search; |
|
7448 |
if found, return the index otherwise return the value of the |
|
7449 |
exceptionBlock. |
|
8874 | 7450 |
The comparison is done using = |
2275 | 7451 |
(i.e. equality test - not identity test)." |
7452 |
||
7453 |
|index| |
|
7454 |
||
7455 |
index := self prevIndexOf:anElement from:start to:stop. |
|
7456 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
|
7457 |
^ index |
|
7458 |
||
7459 |
" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7460 |
args: anElement : <object> |
8874 | 7461 |
start : <integer> |
7462 |
stop : <integer> |
|
7463 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7464 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7465 |
returns: elementIndex - if found |
8874 | 7466 |
valueImplementor value - if not found |
7467 |
" |
|
7468 |
||
7469 |
" |
|
7470 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40 from:7 to:2 ifAbsent:['none'] |
|
7471 |
#(10 20 30 40 10 20 30 40) prevIndexOf:40.0 from:7 to:2 ifAbsent:['none'] |
|
7472 |
#(10 20 30 40 10 20 30 40) prevIndexOf:35 from:7 to:5 ifAbsent:['none'] |
|
2275 | 7473 |
" |
7474 |
||
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7475 |
"Modified: / 20.5.1998 / 15:13:05 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7476 |
! ! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7477 |
|
7264 | 7478 |
!SequenceableCollection methodsFor:'searching-identity'! |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7479 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7480 |
identityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7481 |
"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
|
7482 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7483 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7484 |
^ self identityIndexOf:anElement startingAt:1 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7485 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7486 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7487 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7488 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7489 |
returns: elementIndex - if found |
8874 | 7490 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7491 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7492 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7493 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7494 |
#(10 20 30 40 50 60 70) identityIndexOf:40 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7495 |
#(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
|
7496 |
#(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
|
7497 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7498 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7499 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7500 |
#(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
|
7501 |
#(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
|
7502 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7503 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7504 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7505 |
"Modified: / 20.5.1998 / 14:57:14 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7506 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7507 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7508 |
identityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7509 |
"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
|
7510 |
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
|
7511 |
exceptionBlock." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7512 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7513 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7514 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7515 |
index := self identityIndexOf:anElement startingAt:1. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7516 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7517 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7518 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7519 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7520 |
args: anElement : <object> |
8874 | 7521 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7522 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7523 |
returns: elementIndex - if found |
8874 | 7524 |
exceptionBlock value - if not found |
7525 |
" |
|
7526 |
||
7527 |
" |
|
7528 |
#(10 20 30 40 50 60 70) identityIndexOf:40 ifAbsent:['none'] |
|
7529 |
#(10 20 30 40 50 60 70) identityIndexOf:35 ifAbsent:['none'] |
|
7530 |
#(10 20 30 40 50 60 70) identityIndexOf:40.0 ifAbsent:['none'] |
|
7531 |
#(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
|
7532 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7533 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7534 |
"Modified: / 20.5.1998 / 14:57:31 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7535 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7536 |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7537 |
identityIndexOf:anElement or:anotherElement |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7538 |
"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
|
7539 |
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
|
7540 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7541 |
^ self identityIndexOf:anElement or:anotherElement startingAt:1 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7542 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7543 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7544 |
args: anElement : <object> |
8874 | 7545 |
anotherElement : <object> |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7546 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7547 |
returns: elementIndex - if found |
8874 | 7548 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7549 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7550 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7551 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7552 |
identityIndexOf:anElement or:anotherElement startingAt:start |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7553 |
"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
|
7554 |
using identity compare (i.e. ==); |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7555 |
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
|
7556 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7557 |
|startIndex "{ Class: SmallInteger }" |
8874 | 7558 |
stop "{ Class: SmallInteger }" |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7559 |
el | |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7560 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7561 |
startIndex := start. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7562 |
stop := self size. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7563 |
startIndex to:stop do:[:index | |
8874 | 7564 |
el := self at:index. |
7565 |
anElement == el ifTrue:[^ index]. |
|
7566 |
anotherElement == el ifTrue:[^ index]. |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7567 |
]. |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7568 |
^ 0 |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7569 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7570 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7571 |
args: anElement : <object> |
8874 | 7572 |
anotherElement : <object> |
7573 |
start : <integer> |
|
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7574 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7575 |
returns: elementIndex - if found |
8874 | 7576 |
0 - if not found |
5520
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7577 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7578 |
|
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7579 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7580 |
#(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
|
7581 |
" |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7582 |
! |
97e2afebaa2e
protocol completeness: added #identityIndexOf:or:
Claus Gittinger <cg@exept.de>
parents:
5501
diff
changeset
|
7583 |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7584 |
identityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7585 |
"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
|
7586 |
using identity compare (i.e. ==); |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7587 |
if found, return the index otherwise return 0." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7588 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7589 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7590 |
stop "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7591 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7592 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7593 |
stop := self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7594 |
startIndex to:stop do:[:index | |
8874 | 7595 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7596 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7597 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7598 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7599 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7600 |
args: anElement : <object> |
8874 | 7601 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7602 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7603 |
returns: elementIndex - if found |
8874 | 7604 |
0 - if not found |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7605 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7606 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7607 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7608 |
#(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
|
7609 |
#(10 20 30 40 10 20 30 40) identityIndexOf:40.0 startingAt:5 |
8874 | 7610 |
#(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
|
7611 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7612 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7613 |
"Modified: / 20.5.1998 / 14:57:09 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7614 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7615 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7616 |
identityIndexOf:anElement startingAt:start endingAt:stop |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7617 |
"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
|
7618 |
ending at stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7619 |
If found (within the range), return the index, otherwise return 0. |
17944 | 7620 |
The comparison is done using == |
7621 |
(i.e. identity test - not equality test)." |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7622 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7623 |
|startIndex "{ Class: SmallInteger }" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7624 |
stopIndex "{ Class: SmallInteger }" | |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7625 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7626 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7627 |
stopIndex := self size min:stop. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7628 |
startIndex to:stopIndex do:[:index | |
17944 | 7629 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7630 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7631 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7632 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7633 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7634 |
args: anElement : <object> |
17944 | 7635 |
start : <integer> |
7636 |
stop : <integer> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7637 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7638 |
returns: elementIndex - if found |
17944 | 7639 |
0 - if not found |
7640 |
" |
|
7641 |
||
7642 |
"Created: / 12-04-1996 / 18:23:07 / cg" |
|
7643 |
"Modified: / 20-05-1998 / 15:00:50 / cg" |
|
7644 |
"Modified (comment): / 23-05-2012 / 13:28:21 / cg" |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7645 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7646 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7647 |
identityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7648 |
"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
|
7649 |
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
|
7650 |
exceptionBlock. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7651 |
This one searches for identical objects (i.e. ==)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7652 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7653 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7654 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7655 |
index := self identityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7656 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7657 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7658 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7659 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7660 |
args: anElement : <object> |
8874 | 7661 |
start : <integer> |
7662 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7663 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7664 |
returns: elementIndex - if found |
8874 | 7665 |
exceptionBlock value - if not found |
7666 |
" |
|
7667 |
||
7668 |
" |
|
7669 |
#(10 20 30 40 10) identityIndexOf:10 startingAt:3 ifAbsent:['none'] |
|
7670 |
#(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
|
7671 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7672 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7673 |
"Modified: / 20.5.1998 / 15:01:23 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7674 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7675 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7676 |
includesIdentical:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7677 |
"return true if the collection contains anElement; false otherwise. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7678 |
Comparison is done using identity compare (i.e. ==). |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7679 |
See #includes: if equality is asked for." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7680 |
|
10194 | 7681 |
^ (self identityIndexOf:anElement startingAt:1) ~~ 0 |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7682 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7683 |
" |
8874 | 7684 |
args: anElement : <object> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7685 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7686 |
returns: true - if found |
10194 | 7687 |
false - if not found |
8874 | 7688 |
" |
7689 |
||
7690 |
" |
|
7691 |
#(10 20 30 40 50 60 70) includesIdentical:40 |
|
7692 |
#(10 20 30 40 50 60 70) includesIdentical:40.0 |
|
7693 |
#(10 20 30 40 50 60 70) includes:40 |
|
7694 |
#(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
|
7695 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7696 |
be careful: |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7697 |
|
8874 | 7698 |
#(10 20 30 40.0 50 60 70) includes:40.0 |
7699 |
#(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
|
7700 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7701 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7702 |
"Modified: / 20.5.1998 / 15:00:16 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7703 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7704 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7705 |
lastIdentityIndexOf:anElement |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7706 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7707 |
if found, return the index otherwise return 0. |
8874 | 7708 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7709 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7710 |
|
8874 | 7711 |
^ self lastIdentityIndexOf:anElement startingAt:self size |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7712 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7713 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7714 |
args: anElement : <object> |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7715 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7716 |
returns: elementIndex - if found |
8874 | 7717 |
0 - if not found |
7718 |
" |
|
7719 |
||
7720 |
" |
|
7721 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40 |
|
7722 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:40.0 |
|
7723 |
#(10 20 30 40 50 60 70) lastIdentityIndexOf:35 |
|
7724 |
#(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
|
7725 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7726 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7727 |
"Created: / 20.5.1998 / 15:03:06 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7728 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7729 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7730 |
lastIdentityIndexOf:anElement ifAbsent:exceptionBlock |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7731 |
"search the collection backwards for anElement; |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7732 |
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
|
7733 |
exceptionBlock. |
8874 | 7734 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7735 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7736 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7737 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7738 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7739 |
index := self lastIdentityIndexOf:anElement startingAt:self size. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7740 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7741 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7742 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7743 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7744 |
args: anElement : <object> |
8874 | 7745 |
exceptionBlock : <valueImplementor> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7746 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7747 |
returns: elementIndex - if found |
8874 | 7748 |
exceptionBlock value - if not found |
7749 |
" |
|
7750 |
||
7751 |
" |
|
7752 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 ifAbsent:['none'] |
|
7753 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 ifAbsent:['none'] |
|
7754 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 ifAbsent:['none'] |
|
7755 |
#(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
|
7756 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7757 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7758 |
"Created: / 20.5.1998 / 15:04:46 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7759 |
"Modified: / 20.5.1998 / 15:05:53 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7760 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7761 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7762 |
lastIdentityIndexOf:anElement startingAt:start |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7763 |
"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
|
7764 |
if found, return the index otherwise return 0. |
8874 | 7765 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7766 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7767 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7768 |
|startIndex "{ Class: SmallInteger }"| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7769 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7770 |
startIndex := start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7771 |
startIndex to:1 by:-1 do:[:index | |
8874 | 7772 |
anElement == (self at:index) ifTrue:[^ index]. |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7773 |
]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7774 |
^ 0 |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7775 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7776 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7777 |
args: anElement : <object> |
8874 | 7778 |
start : <integer> |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7779 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7780 |
returns: elementIndex - if found |
8874 | 7781 |
0 - if not found |
7782 |
" |
|
7783 |
||
7784 |
" |
|
7785 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 |
|
7786 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 |
|
7787 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 |
|
7788 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:10 startingAt:8 |
|
7789 |
#(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
|
7790 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7791 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7792 |
"Modified: / 20.5.1998 / 15:06:04 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7793 |
! |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7794 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7795 |
lastIdentityIndexOf:anElement startingAt:start ifAbsent:exceptionBlock |
8874 | 7796 |
"search the collection backwards for anElement starting the search at |
7797 |
index start; if found, return the index |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7798 |
otherwise return the value of the exceptionBlock. |
8874 | 7799 |
The comparison is done using == |
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7800 |
(i.e. identity test - not equality test)." |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7801 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7802 |
|index| |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7803 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7804 |
index := self lastIdentityIndexOf:anElement startingAt:start. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7805 |
(index == 0) ifTrue:[^ exceptionBlock value]. |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7806 |
^ index |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7807 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7808 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7809 |
args: anElement : <object> |
8874 | 7810 |
start : <integer> |
7811 |
exceptionBlock : <valueImplementor> |
|
3468
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7812 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7813 |
returns: elementIndex - if found |
8874 | 7814 |
exceptionBlock value - if not found |
7815 |
" |
|
7816 |
||
7817 |
" |
|
7818 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40 startingAt:8 ifAbsent:['none'] |
|
7819 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:40.0 startingAt:8 ifAbsent:['none'] |
|
7820 |
#(10 20 30 40 10 20 30 40) lastIdentityIndexOf:35 startingAt:8 ifAbsent:['none'] |
|
7821 |
#(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
|
7822 |
" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7823 |
|
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7824 |
"Modified: / 20.5.1998 / 15:06:21 / cg" |
f91f61ea3b5c
changed returnValue of nextIndexOf: / prevIndexOf
Claus Gittinger <cg@exept.de>
parents:
3452
diff
changeset
|
7825 |
"Created: / 20.5.1998 / 15:07:42 / cg" |
1 | 7826 |
! ! |
7827 |
||
7828 |
!SequenceableCollection methodsFor:'sorting & reordering'! |
|
7829 |
||
6958 | 7830 |
detectFirstInOrder:sortBlock |
7831 |
"find the first element of the collection sorted with sortBlock" |
|
7832 |
||
7833 |
|selectedForNow start| |
|
7834 |
||
7835 |
start := true. |
|
7836 |
self do:[:element| |
|
8874 | 7837 |
start ifTrue:[ |
7838 |
start := false. |
|
7839 |
selectedForNow := element. |
|
7840 |
] ifFalse:[ |
|
7841 |
(sortBlock value:element value:selectedForNow) ifTrue:[ |
|
7842 |
selectedForNow := element. |
|
7843 |
]. |
|
7844 |
]. |
|
6958 | 7845 |
]. |
7846 |
||
7847 |
^ selectedForNow |
|
7848 |
||
7849 |
" |
|
8874 | 7850 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a < b] |
7851 |
#(1 16 7 98 3 19 4 0) detectFirstInOrder:[:a :b | a > b] |
|
7852 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a < b] |
|
7853 |
#(1 -1 16 -16 7 -7 98 -98) detectFirstInOrder:[:a :b | a abs < b abs] |
|
6958 | 7854 |
" |
7855 |
! |
|
7856 |
||
17940 | 7857 |
randomShuffle |
7858 |
"random shuffle my elements in place. |
|
7859 |
This is a destructive algorithm. |
|
7860 |
Moses, Oakford, Durstenfeld, Knuth algorithm. |
|
7861 |
See 'The Art of Computer Programming'." |
|
7862 |
||
7863 |
self size to:2 by:-1 do:[:endIndex | |
|
7864 |
|rndIndex t| |
|
7865 |
||
7866 |
rndIndex := Random nextIntegerBetween:1 and:endIndex. |
|
7867 |
"/ exchange |
|
7868 |
t := self at:rndIndex. |
|
7869 |
self at:rndIndex put:(self at:endIndex). |
|
7870 |
self at:endIndex put:t. |
|
7871 |
]. |
|
7872 |
||
7873 |
" |
|
7874 |
(1 to:10) asOrderedCollection randomShuffle |
|
7875 |
||
7876 |
|c| c := (1 to:100) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7877 |
|c| c := (1 to:1000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7878 |
|c| c := (1 to:10000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7879 |
|c| c := (1 to:100000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7880 |
|c| c := (1 to:1000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7881 |
|c| c := (1 to:10000000) asOrderedCollection . TimeDuration toRun:[ c randomShuffle ] |
|
7882 |
" |
|
7883 |
||
7884 |
"how random are we ? (should all be around 1000000/24.0 i.e. 41666.6): |
|
7885 |
||
7886 |
|a score| |
|
7887 |
||
7888 |
a := 'abcd'. |
|
7889 |
score := DictionaryWithDefault newWithDefaultValue:0. |
|
7890 |
||
7891 |
1000000 times do:[ |
|
7892 |
|sorted| |
|
7893 |
sorted := a copy randomShuffle. |
|
7894 |
score at:sorted put:(score at:sorted)+1. |
|
7895 |
]. |
|
7896 |
||
7897 |
score keys asSortedCollection do:[:k | |
|
7898 |
Transcript showCR:('%1: %2' bindWith:k with:(score at:k)). |
|
7899 |
]. |
|
7900 |
" |
|
7901 |
||
7902 |
"Modified: / 22-10-2008 / 15:37:48 / cg" |
|
7903 |
! |
|
7904 |
||
7905 |
reverse |
|
7906 |
"destructively reverse the order of the elements inplace. |
|
7907 |
WARNING: this is a destructive operation, which modifies the receiver. |
|
7908 |
Please use reversed (with a d) for a functional version." |
|
7909 |
||
7910 |
|lowIndex "{ Class: SmallInteger }" |
|
7911 |
hiIndex "{ Class: SmallInteger }" |
|
7912 |
t1 t2| |
|
7913 |
||
7914 |
hiIndex := self size. |
|
7915 |
lowIndex := 1. |
|
7916 |
[lowIndex < hiIndex] whileTrue:[ |
|
7917 |
t1 := self at:lowIndex. t2 := self at:hiIndex. |
|
7918 |
self at:lowIndex put:t2. self at:hiIndex put:t1. |
|
7919 |
||
7920 |
lowIndex := lowIndex + 1. |
|
7921 |
hiIndex := hiIndex - 1 |
|
7922 |
] |
|
7923 |
||
7924 |
" |
|
7925 |
#(4 5 6 7 7) reverse |
|
7926 |
#(1 4 7 10 2 5) asOrderedCollection reverse |
|
7927 |
" |
|
7928 |
! |
|
7929 |
||
7930 |
reversed |
|
7931 |
"return a copy with elements in reverse order" |
|
7932 |
||
7933 |
^ self copy reverse |
|
7934 |
||
7935 |
" |
|
7936 |
#(4 5 6 7 7) reversed |
|
7937 |
#(1 4 7 10 2 5) asOrderedCollection reversed |
|
7938 |
" |
|
7939 |
! |
|
7940 |
||
7941 |
sort |
|
7942 |
"sort the collection inplace. The elements are compared using |
|
7943 |
'<' i.e. they should offer a magnitude-like protocol. |
|
7944 |
||
7945 |
The implementation uses the quicksort algorithm, which may not be |
|
7946 |
the best possible for all situations (quickSort has O-square worst |
|
7947 |
case behavior)." |
|
7948 |
||
7949 |
self quickSort |
|
7950 |
||
7951 |
" |
|
7952 |
#(1 16 7 98 3 19 4 0) sort |
|
7953 |
||
7954 |
|data| |
|
7955 |
data := Random new next:100000. |
|
7956 |
Transcript show:'sort random '; showCR:(Time millisecondsToRun:[data sort]). |
|
7957 |
Transcript show:'sort sorted '; showCR:(Time millisecondsToRun:[data sort]). |
|
7958 |
data reverse. |
|
7959 |
Transcript show:'sort reverse '; showCR:(Time millisecondsToRun:[data sort]). |
|
7960 |
" |
|
7961 |
||
7962 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
|
7963 |
! |
|
7964 |
||
7965 |
sort:sortBlock |
|
7966 |
"sort the collection inplace using the 2-arg block sortBlock |
|
7967 |
for comparison. This allows any sort criteria to be implemented. |
|
7968 |
||
7969 |
The implementation uses the quicksort algorithm, which may not be |
|
7970 |
the best possible for all situations (quickSort has O-square worst |
|
7971 |
case behavior)." |
|
7972 |
||
7973 |
self quickSort:sortBlock |
|
7974 |
||
7975 |
" |
|
7976 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
7977 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
7978 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
7979 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a abs < b abs] |
|
7980 |
" |
|
7981 |
||
7982 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
|
7983 |
! |
|
7984 |
||
7985 |
sort:sortBlock with:aCollection |
|
7986 |
"sort the collection inplace using the 2-arg block sortBlock |
|
7987 |
for comparison. Also reorder the elements in aCollection. |
|
7988 |
Use this, when you have a key collection to sort some other collection with. |
|
7989 |
||
7990 |
The implementation uses the quicksort algorithm, which may not be |
|
7991 |
the best possible for all situations (quickSort has O-square worst |
|
7992 |
case behavior)." |
|
7993 |
||
7994 |
self quickSort:sortBlock with:aCollection |
|
7995 |
||
7996 |
" |
|
7997 |
|c1 c2| |
|
7998 |
c1 := #(1 16 7 9). |
|
7999 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
|
8000 |
c1 sort:[:a :b | a > b] with:c2. |
|
8001 |
c1 printNL. |
|
8002 |
c2 printNL |
|
8003 |
" |
|
8004 |
||
8005 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
|
8006 |
! |
|
8007 |
||
8008 |
sortByApplying:aBlock |
|
8009 |
"Sort my contents inplace based on the value of what aBlock returns for each element. |
|
8010 |
Similar to, but even more flexible than sortBySelector." |
|
8011 |
||
8012 |
^ self sort:[:a :b | (aBlock value:a) < (aBlock value:b)] |
|
8013 |
||
8014 |
" |
|
8015 |
replace all uses of sort as in: |
|
8016 |
... sort:[:a :b | a foo < b foo] |
|
8017 |
by: |
|
8018 |
... sortByApplying:[:each | each foo] |
|
8019 |
" |
|
8020 |
! |
|
8021 |
||
8022 |
sortBySelector:aSelector |
|
8023 |
"Sort my contents inplace based on the value of what aSelector returns when sent to my |
|
8024 |
elements. Sorting by a selector is so common, that its worth a separate utility" |
|
8025 |
||
8026 |
^ self sort:[:a :b | (a perform:aSelector) < (b perform:aSelector)] |
|
8027 |
||
8028 |
" |
|
8029 |
replace all uses of sort as in: |
|
8030 |
... sort:[:a :b | a foo < b foo] |
|
8031 |
by: |
|
8032 |
... sortBySelector:#foo |
|
8033 |
||
8034 |
find these by searching for code matching (code-search in the browsers method list): |
|
8035 |
`@e sort:[:a :b | a `@msg < b `@msg ] |
|
8036 |
" |
|
8037 |
! |
|
8038 |
||
8039 |
sortWith:aCollection |
|
8040 |
"sort the receiver collection inplace, using '<' to compare elements. |
|
8041 |
Also, the elements of aCollection are reordered with it. |
|
8042 |
Use this, when you have a key collection to sort another collection with. |
|
8043 |
||
8044 |
The implementation uses the quicksort algorithm, which may not be |
|
8045 |
the best possible for all situations (quickSort has O-square worst |
|
8046 |
case behavior)." |
|
8047 |
||
8048 |
self quickSortWith:aCollection |
|
8049 |
||
8050 |
" |
|
8051 |
|indices names| |
|
8052 |
||
8053 |
names := #('nine' 'five' 'eight' 'one' 'four' 'two') copy. |
|
8054 |
indices := #(9 5 8 1 4 2) copy. |
|
8055 |
indices sortWith:names. |
|
8056 |
names. |
|
8057 |
indices |
|
8058 |
" |
|
8059 |
||
8060 |
"Modified (comment): / 18-01-2012 / 11:30:01 / cg" |
|
8061 |
! |
|
8062 |
||
8063 |
sortedBy:aBlock |
|
8064 |
"Create a copy that is sorted. Sort criteria is the block that accepts two arguments. |
|
8065 |
When the block is true, the first arg goes first ([:a :b | a > b] sorts in descending order)." |
|
8066 |
||
8067 |
^ (self asSortedCollection: aBlock) asOrderedCollection |
|
8068 |
||
8069 |
"Modified: / 22-10-2008 / 21:25:07 / cg" |
|
8070 |
! |
|
8071 |
||
8072 |
stableSort |
|
8073 |
"sort the collection inplace. The elements are compared using |
|
8074 |
'<=' i.e. they should offer a magnitude-like protocol. |
|
8075 |
||
8076 |
Use a stable sort algorithm - i.e. elements having an equal key will keep |
|
8077 |
their previous order." |
|
8078 |
||
8079 |
self mergeSort |
|
8080 |
||
8081 |
" |
|
8082 |
#(1 16 7 98 3 19 4 0) stableSort |
|
8083 |
||
8084 |
|data| |
|
8085 |
data := Random new next:100000. |
|
8086 |
Transcript show:'sort random '; showCR:(Time millisecondsToRun:[data stableSort]). |
|
8087 |
Transcript show:'sort sorted '; showCR:(Time millisecondsToRun:[data stableSort]). |
|
8088 |
data reverse. |
|
8089 |
Transcript show:'sort reverse '; showCR:(Time millisecondsToRun:[data stableSort]). |
|
8090 |
" |
|
8091 |
! |
|
8092 |
||
8093 |
stableSort:sortBlock |
|
8094 |
"sort the collection inplace using the 2-arg block sortBlock |
|
8095 |
for comparison. This allows any sort criteria to be implemented. |
|
8096 |
||
8097 |
Use a stable sort algorithm - i.e. elements having an equal key will keep |
|
8098 |
their previous order. |
|
8099 |
||
8100 |
NOTE: the sort algorithm will be stable, if the sortblock uses #< or #> for comparison!! |
|
8101 |
Do not use #<= of #>= if you want stable behavior." |
|
8102 |
||
8103 |
self mergeSort:sortBlock |
|
8104 |
||
8105 |
" |
|
8106 |
The 4@bla points keep their order: |
|
8107 |
{(4@1). (8@2). (4@2). (3@3). (4@3). (-1@4). (17@17). (19@19). |
|
8108 |
(12 @ 12). (13@13). (14@14). (15@15). (10@10). (8@8).} stableSort:[:a :b | a x < b x] |
|
8109 |
||
8110 |
But not with quickSort: |
|
8111 |
{(4@1). (8@2). (4@2). (3@3). (4@3). (-1@4). (17@17). (19@19). |
|
8112 |
(12 @ 12). (13@13). (14@14). (15@15). (10@10). (8@8).} sort:[:a :b | a x < b x] |
|
8113 |
" |
|
8114 |
! |
|
8115 |
||
8116 |
topologicalSort:sortBlock |
|
8117 |
"sort the collection inplace using a sloooow sort algorithm. |
|
8118 |
This algorithm has O-square runtime behavior and should be used only |
|
8119 |
in special situations. |
|
8120 |
It compares all elements, thus can be used when a>b, b>c does NOT imply |
|
8121 |
a>c (for example, to sort classes by inheritance) |
|
8122 |
||
8123 |
In all other situations, use #sort; which implements the quicksort algorithm. |
|
8124 |
||
8125 |
If there are cycles, this algorithm may loop endless!! |
|
8126 |
Note: consider using Collection>>#topologicalSort, which is faster and does not do endless loop. |
|
8127 |
" |
|
8128 |
||
8129 |
|smallestIndex "{ Class: SmallInteger }" |
|
8130 |
end "{ Class: SmallInteger }" |
|
8131 |
index2 "{ Class: SmallInteger }" |
|
8132 |
smallest thisOne| |
|
8133 |
||
8134 |
"this is just a q&d hack - there must be better implementations for this ;-)" |
|
8135 |
||
8136 |
end := self size. |
|
8137 |
1 to:end-1 do:[:index | |
|
8138 |
smallest := self at:index. |
|
8139 |
smallestIndex := index. |
|
8140 |
||
8141 |
index2 := index + 1. |
|
8142 |
[ |
|
8143 |
thisOne := self at:index2. |
|
8144 |
(index2 ~~ smallestIndex |
|
8145 |
and:[sortBlock value:thisOne value:smallest]) ifTrue:[ |
|
8146 |
smallestIndex := index2. |
|
8147 |
smallest := thisOne. |
|
8148 |
index2 := index + 1. |
|
8149 |
] ifFalse:[ |
|
8150 |
index2 := index2 + 1. |
|
8151 |
]. |
|
8152 |
] doWhile:[index2 <= end]. |
|
8153 |
||
8154 |
"/ (index + 1) to:end do:[:index2 | |
|
8155 |
"/ thisOne := self at:index2. |
|
8156 |
"/ (sortBlock value:thisOne value:smallest) ifTrue:[ |
|
8157 |
"/ smallestIndex := index2. |
|
8158 |
"/ smallest := thisOne |
|
8159 |
"/ ]. |
|
8160 |
"/ ]. |
|
8161 |
||
8162 |
(smallestIndex ~~ index) ifTrue:[ |
|
8163 |
thisOne := self at:index. |
|
8164 |
self at:index put:smallest. |
|
8165 |
self at:smallestIndex put:thisOne |
|
8166 |
]. |
|
8167 |
] |
|
8168 |
||
8169 |
" |
|
8170 |
#(1 16 7 98 3 19 4 0) topologicalSort:[:a :b | a < b] |
|
8171 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
|
8172 |
Smalltalk allClasses asArray topologicalSort:[:a :b | a == b superclass] |
|
8173 |
Smalltalk allClasses asArray topologicalSort:[:a :b | b isSubclassOf:a] |
|
8174 |
Smalltalk allClasses asArray sort:[:a :b | b isSubclassOf:a] |
|
8175 |
" |
|
8176 |
||
8177 |
"Modified: 21.8.1997 / 18:30:49 / cg" |
|
8178 |
! ! |
|
8179 |
||
8180 |
!SequenceableCollection methodsFor:'sorting algorithms'! |
|
8181 |
||
8182 |
heapSort |
|
8183 |
"sort the collection inplace. The elements are compared using |
|
8184 |
'<' i.e. they should offer a magnitude-like protocol. |
|
8185 |
||
8186 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
8187 |
don't keep their order. |
|
8188 |
||
8189 |
The implementation uses the heapsort algorithm, which has a complexity |
|
8190 |
of O(n*log(n)) for both average and worst case." |
|
8191 |
||
8192 |
self heapSortFrom:1 to:self size |
|
8193 |
||
8194 |
" |
|
8195 |
'franz jagt im komplett verwahrlosten taxi quer durch deutschland' copy heapSort |
|
8196 |
#( 1 2 3 4 5 6 7 8 9) heapSort |
|
8197 |
#( 1 2 3 4 5 6 7 8 9) reversed heapSort |
|
8198 |
||
8199 |
|data| |
|
8200 |
data := Random new next:500000. |
|
8201 |
Transcript show:'heap random '; showCR:(Time millisecondsToRun:[data heapSort]). |
|
8202 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
8203 |
Transcript show:'heap sorted '; showCR:(Time millisecondsToRun:[data heapSort]). |
|
8204 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
8205 |
data reverse. |
|
8206 |
Transcript show:'heap reverse '; showCR:(Time millisecondsToRun:[data heapSort]). |
|
8207 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
8208 |
" |
|
8209 |
! |
|
8210 |
||
8211 |
heapSort:sortBlock |
|
8212 |
"sort the collection inplace. The elements are compared using |
|
8213 |
sortBlock. |
|
8214 |
||
8215 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
8216 |
don't keep their order. |
|
8217 |
||
8218 |
The implementation uses the heapsort algorithm, which has a complexity |
|
8219 |
of O(n*log(n)) for both average and worst case." |
|
8220 |
||
8221 |
self heapSort:sortBlock from:1 to:self size |
|
8222 |
! |
|
8223 |
||
8224 |
heapSort:sortBlock from:begin to:end |
|
8225 |
"sort the collection inplace. The elements are compared using |
|
8226 |
the sortBlock. |
|
8227 |
||
8228 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
8229 |
don't keep their order. |
|
8230 |
||
8231 |
The implementation uses the heapsort algorithm, which has a complexity |
|
8232 |
of O(n*log(n)) for average and worst case." |
|
8233 |
||
8234 |
|cRun "{Class: SmallInteger}" |
|
8235 |
parent "{Class: SmallInteger}" |
|
8236 |
child "{Class: SmallInteger}" |
|
8237 |
eRun "{Class: SmallInteger}" |
|
8238 |
childE childERight temp done size| |
|
8239 |
||
8240 |
eRun := end. |
|
8241 |
size := eRun+1-begin. |
|
8242 |
size <= 1 ifTrue:[ |
|
8243 |
^ self. |
|
8244 |
]. |
|
8245 |
||
8246 |
cRun := size // 2 + 1. "position of the last parent node" |
|
8247 |
||
8248 |
[true] whileTrue:[ |
|
8249 |
cRun > begin ifTrue:[ "Phase 1: do the heap creation" |
|
8250 |
cRun := cRun - 1. |
|
8251 |
temp := self at:cRun. "parent value is in temp" |
|
8252 |
] ifFalse:[ "Phase 2: heap selection" |
|
8253 |
temp := self at:eRun. "save the last value on the heap" |
|
8254 |
self at:eRun put:(self at:begin). "put the largest value so far to the end of the heap" |
|
8255 |
eRun := eRun - 1. "make new heap smaller" |
|
8256 |
eRun == begin ifTrue:[ |
|
8257 |
"the heap is empty - we are done" |
|
8258 |
self at:begin put:temp. |
|
8259 |
^ self. |
|
8260 |
]. |
|
8261 |
]. |
|
8262 |
||
8263 |
parent := cRun. |
|
8264 |
child := parent * 2. "parents left child" |
|
8265 |
||
8266 |
"Sift operation - push temp down the heap" |
|
8267 |
done := false. |
|
8268 |
[done not and:[child <= eRun]] whileTrue:[ |
|
8269 |
childE := self at:child. |
|
8270 |
"choose the largest child" |
|
8271 |
(child < eRun and:[sortBlock value:childE value:(childERight := self at:child+1)]) ifTrue:[ |
|
8272 |
child := child + 1. "the right child is larger" |
|
8273 |
childE := childERight. |
|
8274 |
]. |
|
8275 |
(sortBlock value:temp value:childE) ifTrue:[ |
|
8276 |
self at:parent put:childE. "move the largest child up" |
|
8277 |
parent := child. "make child the new parent" |
|
8278 |
child := parent * 2. "get next child" |
|
8279 |
] ifFalse:[ |
|
8280 |
done := true. "now we know where to put temp" |
|
8281 |
]. |
|
8282 |
]. |
|
8283 |
self at:parent put:temp. "save temp in heap" |
|
8284 |
]. |
|
8285 |
! |
|
8286 |
||
8287 |
heapSortFrom:begin to:end |
|
8288 |
"sort the collection inplace. The elements are compared using |
|
8289 |
'<' i.e. they should offer a magnitude-like protocol. |
|
8290 |
||
8291 |
Heap sort is an unstable sorting algorithm, i.e. elements with the same sort key |
|
8292 |
don't keep their order. |
|
8293 |
||
8294 |
The implementation uses the heapsort algorithm, which has a complexity |
|
8295 |
of O(n*log(n)) for average and worst case." |
|
8296 |
||
8297 |
|cRun "{Class: SmallInteger}" |
|
8298 |
parent "{Class: SmallInteger}" |
|
8299 |
child "{Class: SmallInteger}" |
|
8300 |
eRun "{Class: SmallInteger}" |
|
8301 |
childE childERight temp done size| |
|
8302 |
||
8303 |
eRun := end. |
|
8304 |
size := eRun+1-begin. |
|
8305 |
size <= 1 ifTrue:[ |
|
8306 |
^ self. |
|
8307 |
]. |
|
8308 |
||
8309 |
cRun := size // 2 + 1. "position of the last parent node" |
|
8310 |
||
8311 |
[true] whileTrue:[ |
|
8312 |
cRun > begin ifTrue:[ "Phase 1: do the heap creation" |
|
8313 |
cRun := cRun - 1. |
|
8314 |
temp := self at:cRun. "parent value is in temp" |
|
8315 |
] ifFalse:[ "Phase 2: heap selection" |
|
8316 |
temp := self at:eRun. "save the last value on the heap" |
|
8317 |
self at:eRun put:(self at:begin). "put the largest value so far to the end of the heap" |
|
8318 |
eRun := eRun - 1. "make new heap smaller" |
|
8319 |
eRun == begin ifTrue:[ |
|
8320 |
"the heap is empty - we are done" |
|
8321 |
self at:begin put:temp. |
|
8322 |
^ self. |
|
8323 |
]. |
|
8324 |
]. |
|
8325 |
||
8326 |
parent := cRun. |
|
8327 |
child := parent * 2. "parents left child" |
|
8328 |
||
8329 |
"Sift operation - push temp down the heap" |
|
8330 |
done := false. |
|
8331 |
[done not and:[child <= eRun]] whileTrue:[ |
|
8332 |
childE := self at:child. |
|
8333 |
"choose the largest child" |
|
8334 |
(child < eRun and:[childE < (childERight := self at:child+1)]) ifTrue:[ |
|
8335 |
child := child + 1. "the right child is larger" |
|
8336 |
childE := childERight. |
|
8337 |
]. |
|
8338 |
temp < childE ifTrue:[ |
|
8339 |
self at:parent put:childE. "move the largest child up" |
|
8340 |
parent := child. "make child the new parent" |
|
8341 |
child := parent * 2. "get next child" |
|
8342 |
] ifFalse:[ |
|
8343 |
done := true. "now we know where to put temp" |
|
8344 |
]. |
|
8345 |
]. |
|
8346 |
self at:parent put:temp. "save temp in heap" |
|
8347 |
]. |
|
8348 |
||
8349 |
||
8350 |
" |
|
8351 |
'franz jagt im komplett verwahrlosten taxi quer durch deutschland' copy heapSort |
|
8352 |
#( 1 2 3 4 5 6 7 8 9) copy heapSort |
|
8353 |
#( 1 2 3 4 5 6 7 8 9) reversed heapSort |
|
8354 |
||
8355 |
|data| |
|
8356 |
data := Random new next:500000. |
|
8357 |
Transcript show:'heap random '; showCR:(Time millisecondsToRun:[data heapSortFrom:1 to:data size]). |
|
8358 |
Transcript show:'heap sorted '; showCR:(Time millisecondsToRun:[data heapSortFrom:1 to:data size]). |
|
8359 |
data reverse. |
|
8360 |
Transcript show:'heap reverse '; showCR:(Time millisecondsToRun:[data heapSortFrom:1 to:data size]). |
|
8361 |
" |
|
8362 |
! |
|
8363 |
||
17938 | 8364 |
insertionSort |
8365 |
"sort the collection using a insertionSort algorithm. |
|
8366 |
The elements are compared using'#<' |
|
8367 |
i.e. they should offer a magnitude-like protocol. |
|
8368 |
||
8369 |
Insertion sort sort is a stable sorting algorithm, i.e. elements with the same sort key |
|
8370 |
keep their order (if you use e.g. #<) for comparison. |
|
8371 |
||
8372 |
The implementation uses the insertionSort algorithm, |
|
8373 |
which is slow for large collections O(n*n), but good for small or |
|
8374 |
almost sorted collections O(N). |
|
8375 |
||
8376 |
See also #quickSort for other sort algorithms |
|
8377 |
with different worst- and average case behavior)" |
|
8378 |
||
8379 |
self insertionSort:[:a :b | a < b] |
|
8380 |
! |
|
8381 |
||
8382 |
insertionSort:sortBlock |
|
8383 |
|stop| |
|
8384 |
||
8385 |
stop := self size. |
|
8386 |
(stop > 1) ifTrue:[ |
|
8387 |
self insertionSort:sortBlock from:1 to:stop |
|
8388 |
]. |
|
8389 |
! |
|
8390 |
||
8391 |
insertionSort:sortBlock from:inBegin to:inEnd |
|
8392 |
"binary insertion sort. |
|
8393 |
The implementation uses the insertionSort algorithm, |
|
8394 |
which is slow for large collections O(n*n), but good for small or |
|
8395 |
almost sorted collections O(N)." |
|
8396 |
||
8397 |
|begin "{Class: SmallInteger}" |
|
8398 |
end "{Class: SmallInteger}" |
|
8399 |
prevIdx "{Class: SmallInteger}" |
|
8400 |
temp| |
|
8401 |
||
8402 |
begin := inBegin+1. |
|
8403 |
end := inEnd. |
|
8404 |
begin to:end do:[:idx| |
|
8405 |
temp := self at:idx. |
|
8406 |
prevIdx := idx-1. |
|
8407 |
"this is stable if #< or #> ist used for comparison (and not #<= or #>=)" |
|
8408 |
[prevIdx >= inBegin and:[sortBlock value:temp value:(self at:prevIdx)]] whileTrue:[ |
|
8409 |
self at:prevIdx+1 put:(self at:prevIdx). |
|
8410 |
prevIdx := prevIdx - 1. |
|
8411 |
]. |
|
8412 |
(prevIdx+1) ~~ idx ifTrue:[ |
|
8413 |
self at:prevIdx+1 put:temp. |
|
8414 |
]. |
|
8415 |
]. |
|
8416 |
||
8417 |
" |
|
8418 |
|data| |
|
8419 |
data := Random new next:1000. |
|
8420 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
8421 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
8422 |
data reverse. |
|
8423 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
8424 |
||
8425 |
data := Random new next:1000. |
|
8426 |
Transcript show:'insert random '; showCR:(Time millisecondsToRun:[data insertionSort]). |
|
8427 |
Transcript show:'insert sorted '; showCR:(Time millisecondsToRun:[data insertionSort]). |
|
8428 |
data reverse. |
|
8429 |
Transcript show:'insert reverse '; showCR:(Time millisecondsToRun:[data insertionSort]). |
|
8430 |
" |
|
8431 |
! |
|
8432 |
||
5358 | 8433 |
mergeSort |
8874 | 8434 |
"sort the collection using a mergeSort algorithm. |
17938 | 8435 |
The elements are compared using '#<' |
5358 | 8436 |
i.e. they should offer a magnitude-like protocol. |
8437 |
||
17938 | 8438 |
Merge sort is a stable sorting algorithm, i.e. elements with the same sort key |
8439 |
keep their order. |
|
8440 |
||
5358 | 8441 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 8442 |
the best possible for all situations |
17938 | 8443 |
See also #quickSort for other sort algorithms |
5358 | 8444 |
with different worst- and average case behavior)" |
8445 |
||
17763 | 8446 |
self mergeSort:[:a :b | a < b] |
5358 | 8447 |
|
8448 |
" |
|
8449 |
#(1 16 7 98 3 19 4 0) mergeSort |
|
17763 | 8450 |
#(1 16 7 98 7 3 19 4 0) mergeSort |
5358 | 8451 |
|
17938 | 8452 |
|random data| |
8453 |
||
8454 |
random := Random new next:500000. |
|
8455 |
||
8456 |
data := random copy. |
|
8457 |
Transcript show:'merge random '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
8458 |
Transcript show:'merge sorted '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
8459 |
data := data reverse. |
|
8460 |
Transcript show:'merge reverse '; showCR:(Time millisecondsToRun:[data mergeSort]). |
|
8461 |
||
8462 |
data := random copy. |
|
8463 |
Transcript show:'quick block random '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a < b]]). |
|
8464 |
Transcript show:'quick block sorted '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a < b]]). |
|
8465 |
data := data reverse. |
|
8466 |
Transcript show:'quick block reverse '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a < b]]). |
|
8467 |
||
8468 |
data := random copy. |
|
8469 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8470 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8471 |
data := data reverse. |
|
8472 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
5358 | 8473 |
" |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8474 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8475 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8476 |
mergeSort:sortBlock |
8874 | 8477 |
"sort the collection using a mergeSort algorithm. |
17938 | 8478 |
The elements are compared using sortBlock |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8479 |
i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8480 |
|
17938 | 8481 |
Merge sort is a stable sorting algorithm, i.e. elements with the same sort key |
8482 |
keep their order (if you use e.g. #< for comparison). |
|
8483 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8484 |
The implementation uses the mergesort algorithm, which may not be |
8874 | 8485 |
the best possible for all situations |
17938 | 8486 |
See also #quickSort for other sort algorithms |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8487 |
with different worst- and average case behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8488 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8489 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8490 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8491 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8492 |
(stop > 1) ifTrue:[ |
17938 | 8493 |
self mergeSort:sortBlock from:1 to:stop |
8494 |
]. |
|
8495 |
||
8496 |
" |
|
8497 |
|random data| |
|
8498 |
||
8499 |
random := Random new next:500000. |
|
8500 |
||
8501 |
data := random copy. |
|
8502 |
Transcript show:'merge block random '; showCR:(Time millisecondsToRun:[data mergeSort:[:a :b| a <= b]]). |
|
8503 |
Transcript show:'merge block sorted '; showCR:(Time millisecondsToRun:[data mergeSort:[:a :b| a <= b]]). |
|
8504 |
data := data reverse. |
|
8505 |
Transcript show:'merge block reverse '; showCR:(Time millisecondsToRun:[data mergeSort:[:a :b| a <= b]]). |
|
8506 |
||
8507 |
data := random copy. |
|
8508 |
Transcript show:'quick block random '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a <= b]]). |
|
8509 |
Transcript show:'quick block sorted '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a <= b]]). |
|
8510 |
data := data reverse. |
|
8511 |
Transcript show:'quick block reverse '; showCR:(Time millisecondsToRun:[data quickSort:[:a :b| a <= b]]). |
|
8512 |
" |
|
8513 |
! |
|
8514 |
||
8515 |
mergeSort:aBlock from:startIndex to:stopIndex |
|
8874 | 8516 |
"Sort the given range of indices using the mergesort algorithm. |
8517 |
Mergesort is a worst-case O(N log N) sorting algorithm that usually does only half |
|
17938 | 8518 |
as many comparisons as heapsort or quicksort. |
8519 |
||
8520 |
Merge sort is a stable sorting algorithm, i.e. elements with the same sort key |
|
8521 |
keep their order (if you use e.g. #< or #> for comparison)." |
|
5358 | 8522 |
|
8874 | 8523 |
"Details: recursively split the range to be sorted into two halves, |
8524 |
mergesort each half, then merge the two halves together. |
|
8525 |
An extra copy of the data is used as temporary storage and successive merge phases copy data back |
|
8526 |
and forth between the receiver and this copy. |
|
8527 |
The recursion is set up so that the final merge is performed into the receiver, |
|
5358 | 8528 |
resulting in the receiver being completely sorted." |
17938 | 8529 |
|
8530 |
|mySize| |
|
8531 |
||
8532 |
mySize := self size. |
|
8533 |
mySize <= 1 ifTrue:[ |
|
8534 |
^ self |
|
8535 |
]. |
|
8536 |
startIndex = stopIndex ifTrue:[ |
|
8537 |
^ self |
|
8538 |
]. |
|
8539 |
(startIndex >= 1 and:[ startIndex < stopIndex ]) ifFalse:[ |
|
8540 |
self error:'bad start index' |
|
8541 |
]. |
|
8542 |
stopIndex > mySize ifTrue:[ |
|
8543 |
self error:'bad stop index' |
|
8544 |
]. |
|
8545 |
self |
|
8546 |
mergeSortFrom:startIndex |
|
8547 |
to:stopIndex |
|
8548 |
by:aBlock. |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8549 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8550 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8551 |
quickSort |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8552 |
"sort the collection inplace. The elements are compared using |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8553 |
'<' i.e. they should offer a magnitude-like protocol. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8554 |
|
17938 | 8555 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
8556 |
don't keep their order. |
|
8557 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8558 |
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
|
8559 |
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
|
8560 |
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
|
8561 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8562 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8563 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8564 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8565 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8566 |
(stop > 1) ifTrue:[ |
17938 | 8567 |
self quickSortFrom:1 to:stop |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8568 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8569 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8570 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8571 |
#(1 16 7 98 3 19 4 0) sort |
5358 | 8572 |
|
8573 |
|data| |
|
17938 | 8574 |
data := Random new next:500001. |
8575 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8576 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
8577 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8874 | 8578 |
data reverse. |
17938 | 8579 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
8580 |
||
8581 |
|data rg| |
|
8582 |
rg := Random new. |
|
8583 |
data := Array new:500001. |
|
8584 |
1 to:data size do:[:i | |
|
8585 |
data at:i put:(rg nextIntegerBetween:1 and:100). |
|
8586 |
]. |
|
8587 |
Transcript show:'quick random '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8588 |
data inject:0 into:[:lastElement :each | lastElement > each ifTrue:[self halt]. each]. |
|
8589 |
Transcript show:'quick sorted '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
8590 |
data reverse. |
|
8591 |
Transcript show:'quick reverse '; showCR:(Time millisecondsToRun:[data quickSort]). |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8592 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8593 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8594 |
"Modified: 21.8.1997 / 18:31:52 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8595 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8596 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8597 |
quickSort:sortBlock |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8598 |
"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
|
8599 |
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
|
8600 |
|
17938 | 8601 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
8602 |
don't keep their order. |
|
8603 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8604 |
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
|
8605 |
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
|
8606 |
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
|
8607 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8608 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8609 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8610 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8611 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8612 |
(stop > 1) ifTrue:[ |
17938 | 8613 |
sortBlock numArgs == 3 ifTrue:[ |
8614 |
"/ TODO: pass a collating policy to aBlock |
|
8615 |
self quickSortFrom:1 to:stop sortBlock:sortBlock policy:(StringCollationPolicy new) |
|
8616 |
] ifFalse:[ |
|
8617 |
self quickSortFrom:1 to:stop sortBlock:sortBlock |
|
8618 |
] |
|
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8619 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8620 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8621 |
" |
8874 | 8622 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a < b] |
8623 |
#(1 16 7 98 3 19 4 0) sort:[:a :b | a > b] |
|
8624 |
#(1 -1 16 -16 7 -7 98 -98) sort:[:a :b | a < b] |
|
8625 |
#(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
|
8626 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8627 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8628 |
"Modified: / 27.10.1997 / 20:03:22 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8629 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8630 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8631 |
quickSort:sortBlock with:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8632 |
"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
|
8633 |
for comparison. Also reorder the elements in aCollection. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8634 |
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
|
8635 |
|
17938 | 8636 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
8637 |
don't keep their order. |
|
8638 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8639 |
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
|
8640 |
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
|
8641 |
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
|
8642 |
worstCase behavior (but worse average & bestCase behavior)" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8643 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8644 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8645 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8646 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8647 |
(stop > 1) ifTrue:[ |
17938 | 8648 |
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
|
8649 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8650 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8651 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8652 |
|c1 c2| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8653 |
c1 := #(1 16 7 9). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8654 |
c2 := #('one' 'sixteen' 'seven' 'nine'). |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8655 |
c1 sort:[:a :b | a > b] with:c2. |
17940 | 8656 |
Transcript showCR:c1; showCR:c2. |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8657 |
" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8658 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8659 |
"Modified: 21.8.1997 / 18:32:11 / cg" |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8660 |
! |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8661 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8662 |
quickSortWith:aCollection |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8663 |
"sort the receiver collection inplace, using '<' to compare elements. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8664 |
Also, the elements of aCollection are reordered with it. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8665 |
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
|
8666 |
|
17938 | 8667 |
Quick sort is an unstable sorting algorithm, i.e. elements with the same sort key |
8668 |
don't keep their order. |
|
8669 |
||
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8670 |
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
|
8671 |
the best possible for all situations (quickSort has O-square worst |
17938 | 8672 |
case behavior)." |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8673 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8674 |
|stop| |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8675 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8676 |
stop := self size. |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8677 |
(stop > 1) ifTrue:[ |
17938 | 8678 |
self quickSortFrom:1 to:stop with:aCollection |
7636
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8679 |
] |
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8680 |
|
6b5d572305bc
allow mergeSort with a sortBlock
Claus Gittinger <cg@exept.de>
parents:
7615
diff
changeset
|
8681 |
"Modified: 21.8.1997 / 18:32:21 / cg" |
1 | 8682 |
! ! |
602 | 8683 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8684 |
!SequenceableCollection methodsFor:'testing'! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8685 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8686 |
includesKey:anIndex |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8687 |
"return true, if anIndex is a valid key. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8688 |
NOTICE: in ST-80, this message is only defined for Dictionaries, |
8874 | 8689 |
however, having a common protocol with indexed collections |
8690 |
often simplifies things." |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8691 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8692 |
anIndex isInteger ifFalse:[^ false]. |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8693 |
^ (anIndex >= 1) and:[anIndex <= self size] |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8694 |
|
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8695 |
" |
8874 | 8696 |
#(1 2 3) includesKey:4 |
8697 |
#(1 2 3) includesKey:3 |
|
6744
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8698 |
" |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8699 |
! ! |
042f0606f7a6
includesKey: pushed to SeqColl
Claus Gittinger <cg@exept.de>
parents:
6736
diff
changeset
|
8700 |
|
8396
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8701 |
!SequenceableCollection methodsFor:'visiting'! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8702 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8703 |
acceptVisitor:aVisitor with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8704 |
|
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8705 |
^ aVisitor visitSequenceableCollection:self with:aParameter |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8706 |
! ! |
b13503dbc53c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8373
diff
changeset
|
8707 |
|
2069 | 8708 |
!SequenceableCollection class methodsFor:'documentation'! |
628 | 8709 |
|
17944 | 8710 |
version |
17966 | 8711 |
^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.325 2012/08/23 13:25:43 cg Exp $' |
17944 | 8712 |
! |
8713 |
||
17732
a1892eeca6c0
trunk merged into jv branch
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17728
diff
changeset
|
8714 |
version_CVS |
17966 | 8715 |
^ '§Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.325 2012/08/23 13:25:43 cg Exp §' |
17865 | 8716 |
! |
8717 |
||
8718 |
version_SVN |
|
17966 | 8719 |
^ '$Id: SequenceableCollection.st 10844 2012-09-07 16:24:32Z vranyj1 $' |
628 | 8720 |
! ! |
6820 | 8721 |
|
17846 | 8722 |
SequenceableCollection initialize! |